|
More a curiosity than a real question but, why is it that the following doesn't give the same results in VS and GCC?C++ code:
code:
|
# ? Sep 3, 2014 13:52 |
|
|
# ? Jun 8, 2024 22:39 |
|
code:
|
# ? Sep 3, 2014 17:56 |
|
That style is eyeball-bleedingly bad but the particular thing that's tripping you up is that function bodies should be surrounded by braces, not parentheses.
|
# ? Sep 3, 2014 18:01 |
|
Ok thanks. Im going off of a video tutorial and its hard to tell the difference sometimes.
|
# ? Sep 3, 2014 18:02 |
|
Falcorum posted:Whereas in GCC, the 4 are exactly the same. Is this due to VS's lack of two phase lookup or something? Nothing to do with two-phase lookup; just a plain old buggy compiler. MSVC has a ton of bugs with l-values and pointers of function type, basically because they model them completely wrong internally — IIRC, they do something like always give functions function-pointer type and then have a ton of hacks to try to clean things up later.
|
# ? Sep 3, 2014 21:41 |
|
Hey guys. I'm racking my brain over something in my C programming class and I'd be very grateful for even a hint on what I'm doing wrong here. The assignment is centered around pointer arithmetic, but I'm getting a warning on the File I/O that I'd like to go away. It consists of a hw1.c file that was given to us and we aren't allowed to modify. We're graded on the hw1_functions.c file. The relevant code from hw1.c is this bit: code:
code:
When I change the hw1_functions.c code to make a new FILE pointer with: code:
Any advice on how to make this warning go away? Why is "infile" suddenly changing to an integer?
|
# ? Sep 4, 2014 18:07 |
|
Adiabatic posted:Any advice on how to make this warning go away? Why is "infile" suddenly changing to an integer? Because you didn't give it a type when you defined the function (you need to repeat the parameter type declaration in both the declaration of the function and its definition).
|
# ? Sep 4, 2014 18:14 |
|
Adiabatic posted:my C programming class Surprised GCC doesn't warn about it.
|
# ? Sep 4, 2014 19:25 |
|
Mr.Radar posted:Because you didn't give it a type when you defined the function (you need to repeat the parameter type declaration in both the declaration of the function and its definition). Holy poo poo thank you that got it. Skuto posted:
That's the professor's code too!
|
# ? Sep 4, 2014 19:58 |
|
Yawn, who cares, main returning void never hurt anybody.
|
# ? Sep 4, 2014 21:46 |
|
Skuto posted:
-Wmain (it's in -Wall)
|
# ? Sep 4, 2014 22:08 |
|
So let's say I want to read 4 individual bytes 1 at a time and see if each hex value corresponds to a particular byte I'm looking for, is it possible to check for all 4 bytes at once? Is it written ascode:
code:
|
# ? Sep 5, 2014 07:42 |
|
durtan posted:So let's say I want to read 4 individual bytes 1 at a time and see if each hex value corresponds to a particular byte I'm looking for, is it possible to check for all 4 bytes at once? Is it written as What does "corresponds" mean? Will you have a lookup table or function telling you what values correspond to what? Can you invert this mapping and just check for equality? Also you're saying you want to check for all 4 bytes at once. But you just said you were checking for a particular byte that each hex value corresponds to. Did you mean to say that you wanted to check all 4 bytes at once? If so, whether you can do that depends on what it means for hex values to correspond to one another.
|
# ? Sep 5, 2014 08:12 |
|
I'm embarrassed to be asking this, but... I have a function that takes an object as a parameter and puts it in a container. To avoid wasting time and memory on useless copies, I declare the argument as a rvalue reference, and std::move() it to the container's insert(). Won't this "steal" a caller's object if it happens to be an lvalue instead of a temporary? How do I avoid this? My guess is an overload that takes a const reference, but I'm not terribly sure. Little help?
|
# ? Sep 5, 2014 17:33 |
|
That is the motivation behind std::forward, which preserves the lvalueness/rvalueness of the expression.
|
# ? Sep 5, 2014 18:17 |
|
hackbunny posted:I have a function that takes an object as a parameter and puts it in a container. To avoid wasting time and memory on useless copies, I declare the argument as a rvalue reference, and std::move() it to the container's insert(). Won't this "steal" a caller's object if it happens to be an lvalue instead of a temporary? How do I avoid this? My guess is an overload that takes a const reference, but I'm not terribly sure. Little help? You can't convert an l-value to an r-value reference, so the implicit error can't happen. If you want be able to accept either an l-value or an r-value, you have three options. You can make the function take the argument as a value instead of a reference (and then move out of it unconditionally); this requires more formal objects, but as long as move-construction really is cheap, it shouldn't be much of a problem. You can provide a second overload that takes a const &, which means repeating some code but should generate optimal behavior. Finally, you can make the function templated, declare it as taking a T&&, and then std::forward<T> the argument over to the insert function.
|
# ? Sep 5, 2014 18:53 |
|
sarehu posted:What does "corresponds" mean? Will you have a lookup table or function telling you what values correspond to what? Can you invert this mapping and just check for equality? I might've figured it out, but I thought I'd answer your question. So I am working on a program which takes a faw file and finds .jpg images in it for a class. I am looking for 4 hex values which indicate the start of the jpg and I'm unsure how to check for them. The raw file mimics a CF card and apparently uses 512 byte blocks at a time, so I only need to check the first four bytes of each block. However, when I try to write something like code:
code:
code:
|
# ? Sep 5, 2014 19:42 |
|
Just check the values of each of the first four bytes individually.
|
# ? Sep 5, 2014 19:49 |
|
I thought of doing that, but I figured there'd be a way to check all four values at once and save myself a bunch of if statements. Is one at a time the only way to do it? What would be a good way to go about that?
|
# ? Sep 5, 2014 20:04 |
|
durtan posted:I thought of doing that, but I figured there'd be a way to check all four values at once and save myself a bunch of if statements. Is one at a time the only way to do it? What would be a good way to go about that? memcmp
|
# ? Sep 5, 2014 20:08 |
|
So if I call memcmp four times, will it read whatever size n of bytes consecutively?
|
# ? Sep 5, 2014 20:31 |
|
You could do this:C++ code:
C++ code:
The important thing is that there is no way to get there from 0xffd8ffe0 without making endianness assumptions, which you should be learning to avoid in babby's first C project.
|
# ? Sep 5, 2014 20:43 |
|
rjmccall posted:The important thing is that there is no way to get there from 0xffd8ffe0 without making endianness assumptions, which you should be learning to avoid in babby's first C project. arpa/inet.h ?
|
# ? Sep 5, 2014 20:50 |
|
Blotto Skorzany posted:arpa/inet.h ? My point is that somebody new to C who is legitimately wondering whether memcmp will statefully advance their buffer pointer should be writing the simple, obvious code that does not need to reason about endianness at all.
|
# ? Sep 5, 2014 20:55 |
|
Is there a decent-size C/C++ project that would make a good subject for a build speed benchmark? Something that would preferably do OS X and Linux, but not have to build a bunch of other OS-dependent stuff that would really throw it off. I used to use stuff like nmap but it builds in 15 seconds, and I'd use the Linux kernel if it would build on the Mac...
|
# ? Sep 6, 2014 02:12 |
|
Bob Morales posted:Is there a decent-size C/C++ project that would make a good subject for a build speed benchmark? The biggest thing I've built recently is the clang source.
|
# ? Sep 6, 2014 03:31 |
|
Firefox is pretty big, or Chrome.
|
# ? Sep 6, 2014 04:10 |
|
Phoronix typically uses the Linux kernel or Open Office.
|
# ? Sep 6, 2014 04:22 |
|
LLVM/clang would not be a totally fair benchmark since, of course, it is something that we actively track our compile time on and other compilers presumably don't. Firefox is a reasonable choice as long as you're comparing builds for the same target platform. If you become interested in benchmarking whole-system cross-platform compile performance (e.g. comparing Linux vs. Mac OS X installs on the same/comparable hardware), GUI projects aren't a good choice because of course the GUI layers will use different toolkits and hence different source code.
|
# ? Sep 6, 2014 04:51 |
|
Dicky B posted:That is the motivation behind std::forward, which preserves the lvalueness/rvalueness of the expression. rjmccall posted:You can't convert an l-value to an r-value reference, so the implicit error can't happen. If you want be able to accept either an l-value or an r-value, you have three options. You can make the function take the argument as a value instead of a reference (and then move out of it unconditionally); this requires more formal objects, but as long as move-construction really is cheap, it shouldn't be much of a problem. You can provide a second overload that takes a const &, which means repeating some code but should generate optimal behavior. Finally, you can make the function templated, declare it as taking a T&&, and then std::forward<T> the argument over to the insert function. Thank you both, I think I get it now, sort of. The point is moot anyway, because I found out I'm limited C++03
|
# ? Sep 8, 2014 09:00 |
|
I'm trying something simpler. I am in the middle of a tictactoe game but can't figure out where my error checking is going wrong. It works fine when I try to put a number in that is too big, but non-numerical entry needs to be taken intoi account as well, e.g. if you enter a string instead, it should say "Please Enter a number". It doesn't though it just gets trapped in an infinite loop of switching between players rapidly and I have no idea why. Any help? I have tried to comment and format it better this time. Also if there are any areas where I am going about it completely the wrong way do let me know:code:
|
# ? Sep 8, 2014 10:36 |
|
BioEnchanted posted:I'm trying something simpler. I am in the middle of a tictactoe game but can't figure out where my error checking is going wrong. It works fine when I try to put a number in that is too big, but non-numerical entry needs to be taken intoi account as well, e.g. if you enter a string instead, it should say "Please Enter a number". It doesn't though it just gets trapped in an infinite loop of switching between players rapidly and I have no idea why. Any help? I have tried to comment and format it better this time. Also if there are any areas where I am going about it completely the wrong way do let me know: An istream goes into a fail state if it can't turn what it read into a value of the type you passed to operator>>. After that it won't do any more reads until you clear the fail state with clear(). You're going into an infinite loop because cin is just blowing off all reads. You should be testing cin after reading with fail() or just using the overloaded operator! or implicit conversion to bool, e.g.: code:
Lime fucked around with this message at 12:01 on Sep 8, 2014 |
# ? Sep 8, 2014 11:28 |
|
You also need to actually read the non-numeric data out of the stream after clearing, or the next attempt to read an int will return immediately and put the stream into the same failure state again, making no progress.
|
# ? Sep 8, 2014 12:28 |
|
As for some other strange things going on in your code, your while loop in main will never end, so you'll want to look into that. In addition, if you've learned about void function returns, then you should use that for any function where you don't actually use the return value, rather than returning something that you never use.
|
# ? Sep 8, 2014 12:45 |
|
I'll try to implement the necessary things, including the void functions, but I have a question about the point on the while loop. I thought that the loop would end after the win condition had been satisfied and the player had said no to continuing. Why is this not true, and how can I resolve the issue?
|
# ? Sep 8, 2014 14:56 |
|
C++ code:
Switch statements should generally look like: C++ code:
edit: actually looking at it again I guess might not be what you intended to do with the break there. I can't tell at a quick glance if winner is being set properly, don't use global variables like that it is generally a bad practice and I think it would probably be better to have something like: C++ code:
astr0man fucked around with this message at 15:09 on Sep 8, 2014 |
# ? Sep 8, 2014 15:01 |
|
astr0man posted:edit: actually looking at it again I guess might not be what you intended to do with the break there. I can't tell at a quick glance if winner is being set properly, don't use global variables like that it is generally a bad practice and I think it would probably be better to have something like: Yeah, this is what I was getting at. You don't use the return from tictac.check_winner() in any way, so the variable winner never gets changed.
|
# ? Sep 8, 2014 15:31 |
|
He sets the global bool winner in tictac.check_winner() so I guess maybe it does work? But yeah as I said before this is terrible code please don't write it this way
|
# ? Sep 8, 2014 15:47 |
|
Is there some way to reliably convert a Visual Studio project to CMake? I want to try the new JetBrains IDE CLion and I have no experience with CMake.
|
# ? Sep 9, 2014 09:37 |
|
|
# ? Jun 8, 2024 22:39 |
|
I'm trying to build Clang and LLVM by following this guide: http://stackoverflow.com/questions/9427356/how-to-compile-clang-on-windows But when I get to step 11, the compiler gives me an error: code:
|
# ? Sep 10, 2014 15:59 |