|
an skeleton posted:Ahhh, I see. So the dereferencer is like a trick to make it work? Pronouns are amazing. If by "it" you mean "doing a weird trick to make a float have an integer's byte pattern", then yes. an skeleton posted:I'm not certain what giving it the same "byte pattern" means. Integers and floats have to be stored in some binary format the computer can natively understand. Integers are typically stored in "two's complement little-endian" if you're on an x86 or x86-64 processor, and floats are typically stored in "IEEE 754 Floating Point" format. The same number would be represented in both two different ways if we look at the raw bits that are stored in memory. What Volte's code does is make some assumptions about what the raw bits of floating point numbers and integers look like on your system, play around with the bits of the integer you passed in (that's the + 127 and << 21 and stuff), and then tell the compiler "see all these bits I was turning around, and told you they were an integer? They're actually the bits that make up an IEEE 754 Floating Point Number" through some mischievous ways.
|
# ? Oct 31, 2012 08:06 |
|
|
# ? Jun 8, 2024 08:45 |
|
Suspicious Dish posted:Pronouns are amazing. If by "it" you mean "doing a weird trick to make a float have an integer's byte pattern", then yes. Yes by "it" I was saying, that without that first asterisk, would the casting work at all or does it require the dereferencing symbol? anyway, makes (some) sense, thanks for the explanation.
|
# ? Oct 31, 2012 08:10 |
|
The Gripper posted:If it makes you feel any better I know what pointers, casts and references are and still have no clue what it's supposed to be doing since I don't know the math behind it. The math itself isn't too hard. The + 127 offsets the exponent bias, and the << 23 is stuffing the value into the exponent field. It's relying on the fact that floating point number is stored in terms of powers of two, so all we do two get a power of two is stuff the power we want into the exponent field.
|
# ? Oct 31, 2012 08:14 |
|
an skeleton posted:Yes by "it" I was saying, that without that first asterisk, would the casting work at all or does it require the dereferencing symbol? Suspicious Dish posted:The math itself isn't too hard. The + 127 offsets the exponent bias, and the << 23 is stuffing the value into the exponent field. It's relying on the fact that floating point number is stored in terms of powers of two, so all we do two get a power of two is stuff the power we want into the exponent field.
|
# ? Oct 31, 2012 08:18 |
|
Combat Pretzel posted:Forgot to mention, the exponents are floating point and can be negative. Sorry for the undefined behaviour. For some reason I thought that setting a union's value through one member and then accessing it with a different member was equally undefined, so I did it the simpler way. My mistake. vv
|
# ? Oct 31, 2012 09:49 |
|
Volte posted:For some reason I thought that setting a union's value through one member and then accessing it with a different member was equally undefined, so I did it the simpler way. My mistake. vv
|
# ? Oct 31, 2012 10:17 |
|
Volte posted:Ah, well in that case you can still use exp(x * log(2)). The math library might even have an exp2 function in it depending on the platform. For the specialized case, the LUT is still faster, tho. Since I know roughly what values I'll be dealing with, this is rather easy to generate. code:
|
# ? Oct 31, 2012 15:15 |
|
floWenoL posted:This is undefined behavior.
|
# ? Oct 31, 2012 15:29 |
|
I'm trying to figure out a heap overflow vulnerability in some old software. I can't actually debug it right now because ~hurricanes~ Comparing the old vulnerable source to the patched source led me here: code:
|
# ? Oct 31, 2012 22:00 |
|
I'm not 100% sure about your vulnerability problem, but you should always check the return value of malloc. edit: Also, where does str come from?
|
# ? Oct 31, 2012 22:06 |
Illusive gently caress Man posted:
I'm pretty sure that should be equivalent to a strdup() call, except that strdup() will actually check the return value of malloc() before copying the data. Some other things to consider: 1. Try to use string functions that take a length parameter as much as possible. 2. If the input string has a known length from somewhere (other than the implicit length from the hopefully-there \0 terminator), try to pass that along and use it. 3. If you use a bounded-length string function to copy a string you don't know if is properly terminated, make sure there's room to manually put in a \0 after it. 4. Why are you not using C++ and a safer string class.
|
|
# ? Oct 31, 2012 22:16 |
|
nielsm posted:I'm pretty sure that should be equivalent to a strdup() call, except that strdup() will actually check the return value of malloc() before copying the data. Yeah, it was actually in a macro like #define app_pool_strdup ~thatline~ nielsm posted:Some other things to consider: This isn't actually my code. I'm trying to find a specific exploit in old version of an open source app from ~2002. astr0man posted:edit: Also, where does str come from? The program reads a line of input from a remote user, splits it up by inserting a few null characters, then copies parts of it (using that line) into a new struct.
|
# ? Oct 31, 2012 22:29 |
|
Volte posted:Ah, well in that case you can still use exp(x * log(2)). The math library might even have an exp2 function in it depending on the platform. The exp2 function should be available anywhere pow/exp/etc are. C99 added float and long double versions (exp2f/exp2l)
|
# ? Nov 1, 2012 21:39 |
|
mjau posted:The exp2 function should be available anywhere pow/exp/etc are. C99 added float and long double versions (exp2f/exp2l)
|
# ? Nov 2, 2012 02:59 |
|
Volte posted:I think exp2 was only added to the standard in C99. I would think it would be available pretty much everywhere, but it's not a guarantee. Pretty sure it is C99, and it doesn't seem to be available in Visual Studio, as of 2010 at least. Microsoft doesn't seem to care at all about C99, which can be a bit annoying.
|
# ? Nov 2, 2012 03:44 |
|
I'm using VS2012 and it's not there either. Well, kind of. Their AMP headers come with it.
|
# ? Nov 2, 2012 03:50 |
|
Volte posted:I think exp2 was only added to the standard in C99. Ah, you're right, it's C99. I was so sure it predated that
|
# ? Nov 2, 2012 06:37 |
|
Combat Pretzel posted:I'm using VS2012 and it's not there either. Well, kind of. Their AMP headers come with it. That's because MS is dumb and doesn't ship anything newer than C89.
|
# ? Nov 2, 2012 08:59 |
|
I thought MSVC does C11?
|
# ? Nov 2, 2012 13:35 |
|
It does (kinda) do C++11, but that's C++, not C.
|
# ? Nov 2, 2012 13:38 |
|
Alright so I have this program that I coded and right now it's using no functions. I need to create a few functions, starting with one called char get_menu_choice() and I'm not sure how I would begin to do that. Any ideas? Thanks! http://pastebin.com/c9uNwfsz
|
# ? Nov 2, 2012 16:35 |
|
KaneTW posted:It does (kinda) do C++11, but that's C++, not C. Yea. "C++ 11" but they have kept the C compiler at C89 for a long time now. The thing is though, they don't even support half the features of C++ 11 either in VS2k12. All their dev time went into their proprietary extensions called C++/CX and C++ AMP. And, aside from this year with the introduction to in IDE graphics debugging (which, lets be honest, just took PIX and integrated it), there hasn't been any real advancements in the IDE for C++ in 12 years. Most of the effort has been in the .NET realm. Xcode is a much better development environment for C and C++ developers nowadays. xgalaxy fucked around with this message at 16:55 on Nov 2, 2012 |
# ? Nov 2, 2012 16:47 |
|
The Gnome posted:Alright so I have this program that I coded and right now it's using no functions. I need to create a few functions, starting with one called char get_menu_choice() and I'm not sure how I would begin to do that. Any ideas? Thanks! code:
|
# ? Nov 2, 2012 17:03 |
|
Announcements coming today (November 2, 2012) at 12:45 PM PST:Beman Dawes posted:Herb Sutter, the Convenor of the ISO C++ Standards Committee, will be Edit: C++ Standard stuff: Standards meeting attendence has increased. Standardization process now includes SGs for concurrency, modules, filesystem, networking, transactional memory, numerics, reflection, concepts, ranges, feature test, and databases. Next C++ standard in 2014 (minor). Expecting to include polymorphic lambdas (lambdas that are templates), return type deduction from normal functions, a C++ version of VLAs, static if, and preliminary concepts support. Standard library literal support, including support for binary literals (no more need to hate on me for BOOST_BINARY :p), reader-writes locks, thread-safety for streams, possibly concurrent queue and hashtables. Next major C++ standard is targeted for 2017. TS (formerly knows as TR) based on Boost.Filesystem coming in 2013. Similarly, a networking specification is targeted for 2013, expecting revisions each year. Transactional memory TS in 2014. New website to keep people up-to-date with C++ standardization: http://www.isocpp.org Standard C++ Foundation -- a non-profit not owned by any company, founded and funded by over a dozen companies including Microsoft, Google, Intel, etc. aimed in promoting understanding and use of C++. Some MS-specific stuff: VC++ 2012 CTP, downloadable now, supports explicit conversion operators, raw string literals, delegating constructors, function template default arguments, uniform initialization, and variadic templates (finally). That Turkey Story fucked around with this message at 21:38 on Nov 2, 2012 |
# ? Nov 2, 2012 18:58 |
|
Combat Pretzel posted:If I had to guess, such a get_menu_choice() function would just wrap cin >> selection and return the value. If you were to make it some more complex, you'd move the 'parsing' into it and return a well defined value to handle in the main body or pass it to another function. http://pastebin.com/c9uNwfsz Here is an update. As you guys can see, I've used functions correctly, however, I'd like to be able to use these functions without having the variables declared globally and would rather have them use inputs and such. Any help? Thanks!
|
# ? Nov 2, 2012 19:07 |
|
xgalaxy posted:Yea. "C++ 11" but they have kept the C compiler at C89 for a long time now. There is literally only one developer maintaining the STL in VS 2012 right now.
|
# ? Nov 2, 2012 19:08 |
|
xgalaxy posted:Yea. "C++ 11" but they have kept the C compiler at C89 for a long time now. There's a few more parts rammed into VS2012 for graphics work. Yes, the GPUDebugging is basically Pix but integrated, it feels a lot easier to work with and far less of a pain in the rear end. The part I'm most confused about though, is the addition of a model viewer/editor. I mean, I can kind of see some minor benefits of having something lightweight to check your model assets. But having it built into your code IDE seems a bit off, especially when you consider that it's supported formats are Obj, SDKMesh (urgh) and FBX which has about a million model/scene viewers around already.
|
# ? Nov 2, 2012 20:24 |
That Turkey Story posted:Announcements coming today (November 2, 2012) at 12:45 PM PST: Thanks for the heads-up, watching it now. Quite interesting.
|
|
# ? Nov 2, 2012 21:08 |
|
That Turkey Story posted:C++ Standard stuff: Thanks, you just saved me 90 minutes. Some of that stuff's pretty cool.
|
# ? Nov 2, 2012 23:54 |
|
Combat Pretzel posted:I thought MSVC does C11?
|
# ? Nov 4, 2012 04:41 |
|
Taking an online course on compsci and am learning basic C programming. Having trouble on an assignment that has us take user input to create a half pyramid. It's suppose to look like a staircase going left to right (opposite of what's shown)...user just inputs height. ## ### #### ##### etc.... Below is my code thus far...the user input goes in correctly but nothing is drawn. What am I doing wrong? There is apparently suppose to be 3 loops used in this exercise. Any help is appreciated. #include <stdio.h> #include <cs50.h> #include <math.h> int main (void) { // User inputs pyramid height using 1-23. Other numbers prompt retry. int height; do { printf ("Enter pyramid height Mario! (1-23): "); height = GetInt(); } while (height < 1 || height > 23); // height transfers into next loop where it's calculated to create the pyramid. int space = 0; int hash = 0; while (height > 0); { (space = (height - 1), hash = ((height + 1) - space)); printf("%d ", space); printf("%d##", hash); printf("\n"); height--; } return 0; }
|
# ? Nov 5, 2012 05:30 |
|
while (height > 0); This line means "as long as height > 0, keep checking whether height > 0". It is probably helpful to start using compiler warning messages from early on because I'm pretty sure most compilers catch this. And don't worry about it, everyone has problems with terminating statements with semicolons at first (unless they use a language which doesn't require it in the first place). In this case, remove the semicolon to make it actually run the following block.
|
# ? Nov 5, 2012 06:15 |
|
Thank you, that helped partially! More updates to come.
|
# ? Nov 5, 2012 06:40 |
|
When you post code post it in code tags.
|
# ? Nov 5, 2012 07:19 |
|
Can anyone suggest a good C++ library for linear algebra? I'm trying to do a project for my linear algebra glass with game theory, while in an intro to C++ class. I Google for linear algebra libraries and get a few back, but I don't know enough about C++ to discern which would be the best. I don't need a lot of functionality for it, just some basic matrix operations would probably do fine, but simplicity in terms of C++ code would help.
|
# ? Nov 5, 2012 16:22 |
|
Karl Sharks posted:Can anyone suggest a good C++ library for linear algebra? I'm trying to do a project for my linear algebra glass with game theory, while in an intro to C++ class. I Google for linear algebra libraries and get a few back, but I don't know enough about C++ to discern which would be the best. I don't need a lot of functionality for it, just some basic matrix operations would probably do fine, but simplicity in terms of C++ code would help. There are a ton, but the current flavor of the month tends to be Eigen, although it is far from "simple" C++ code as it uses a lot of advanced, modern techniques.
|
# ? Nov 5, 2012 16:49 |
|
Karl Sharks posted:Can anyone suggest a good C++ library for linear algebra? I'm trying to do a project for my linear algebra glass with game theory, while in an intro to C++ class. I Google for linear algebra libraries and get a few back, but I don't know enough about C++ to discern which would be the best. I don't need a lot of functionality for it, just some basic matrix operations would probably do fine, but simplicity in terms of C++ code would help. Also, you could build your own little functions for those basic matrix operations that take 2D array arguments. Are you doing anything beyond simple add, multiply, scalar multiply? Depending on the number of operations you need and how generalized you make the functions (that is to say, whether you're writing them for general cases or the hyper-specific cases you know you'll be working with), you could whip them up yourself and just continue on your way. Granted, I half suspect that your asking the thread cause it's due in a day or two and have over stepped your bounds a little, but if you have the time, this'd be a nice little side project for you to build up your own linear algebra library and will be a boost to your C++ (and general) programming skills.
|
# ? Nov 5, 2012 23:49 |
|
I am working on making my own shell for one of my cs classes. Anyway I have to make a process run in the back if the user adds '&' at the end of the command. I simply check if one of the args was an '&', if so I set runBG to equal 1. Then I fork the child and run normally, and if runBG equals 1, I do a waitpid(-1, &status, WNOHANG). Otherwise, I do a waitpid(-1, &status, 0). It seems to work fine except if I try to run commands after a background process finishes the output gets messed up. Normal: After I tell a command to run in the background: Any help on what I'm missing would be great.
|
# ? Nov 6, 2012 01:15 |
|
It looks like you're not setting that flag back to zero for the next command.
|
# ? Nov 6, 2012 01:30 |
|
|
# ? Jun 8, 2024 08:45 |
|
Jabor posted:It looks like you're not setting that flag back to zero for the next command. I do set it to zero after the execution of the command. If it's easier to see my terrible code: http://pastebin.com/pfcpJBbF
|
# ? Nov 6, 2012 03:37 |