|
I have a question about fflush(stdin) in C programming. It's supposed to clear some input buffer that holds input data. However I have yet to come across a situation that requires fflush for stdin. If possible, could someone explain a situation or give an example when fflush(stdin) or even stdout would be required?
|
# ? Apr 11, 2008 16:20 |
|
|
# ? Apr 18, 2024 02:02 |
|
Bush is a QT posted:I have a question about fflush(stdin) in C programming. Always flush shared file handles in multi-threaded applications. I can't for the life of my think of a time when multiple threads would be accessing stdin, but it happens all the time with stdout. If you have several threads that all want to print poo poo to the screen you have to make them stand in line to do so or they'll end up printing over the top of each other. So you make them acquire a mutex, then write to stdout. You want to flush stdout at that point to ensure that everything has been written before you release the mutex. Fake edit: just thought of when you'd want to fflush(stdin)- when your application will be used in a pipe, e.g. 'cat somefile|yourapp'.
|
# ? Apr 11, 2008 16:48 |
|
thank you
|
# ? Apr 11, 2008 16:54 |
|
I am pretty sure that fflushing input streams does not do anything.
|
# ? Apr 11, 2008 16:54 |
|
Vanadium posted:I am pretty sure that fflushing input streams does not do anything. From the man page: code:
|
# ? Apr 11, 2008 18:50 |
|
Ugg boots posted:
fflush(stdin) is undefined behavior in ANSI C. Some compilers implement it, some don't.
|
# ? Apr 11, 2008 19:09 |
|
Ugg boots posted:From the man page: It does not say that in my manpage. Are you sure you are not missing some "conforming to" part?
|
# ? Apr 11, 2008 19:26 |
|
The OS X 10.5 manpage for fflush makes no mention of input streams; it only says the function will fail if the stream is "not open for writing".
|
# ? Apr 11, 2008 19:36 |
|
I've got a template function in which I'm doing a comparison like:code:
../shared/number_stuff.h:102: warning: comparison of unsigned expression < 0 is always false Is there anyway I can reasonably make these warnings go away? I tried enclosing it in an if (numeric_limits<T>::is_signed) block, but the compiler still complains.
|
# ? Apr 11, 2008 19:45 |
|
6174 posted:I've got a template function in which I'm doing a comparison like: Since numeric_limits<T>::is_signed is a compile-time constant, you should be able to make use of SFINAE to make one specialization for signed types and another for unsigned types. Otherwise, you can just turn off that warning with a #pragma around that block or for that file on the command line, but I'm pretty sure SFINAE is the proper way to do it. Concepts would make this easier.
|
# ? Apr 11, 2008 19:55 |
|
Try boost::enable_if
|
# ? Apr 11, 2008 19:57 |
|
Is it possible to use a struct or class in a templated function? I've already made a working, templated, double-linked list and now I'm trying to use it in a call center simulation. I made the struct: code:
When I compile, I get this: code:
code:
|
# ? Apr 13, 2008 01:52 |
|
You can use objects in templates, that's the point. You're trying to call insertFront with a reference as the parameter and not a pointer. The types need to match up.
|
# ? Apr 13, 2008 02:10 |
|
I'm trying to get myself back into C but things are going a bit slow... fwrite requires a pointer to a string as the first argument so how would I properly cast a character constant to a pointer? ie: // Wrong fwrite('\n', sizeof(char), 1, fd); // Wrong fwrite((char *) '\n', sizeof(char), 1, fd);
|
# ? Apr 14, 2008 17:39 |
|
Don't bother with casts, just do fwrite("\n",1,1,fd);
|
# ? Apr 14, 2008 17:56 |
|
lhunc posted:I'm trying to get myself back into C but things are going a bit slow... code:
|
# ? Apr 14, 2008 18:04 |
|
thanks guys, guess I'm just over thinking it.
|
# ? Apr 14, 2008 18:15 |
|
Oh, I forgot to mention, another option would be to use fputc('\n',fd); if you're only writing single characters.
|
# ? Apr 14, 2008 18:22 |
|
HB posted:The OS X 10.5 manpage for fflush makes no mention of input streams; it only says the function will fail if the stream is "not open for writing". I found it on this site http://www.hmug.org/man/3/fflush.php but I guess that's only for ISO C90?
|
# ? Apr 14, 2008 19:33 |
|
I'm wondering if it's at all possible to further specialize a nested class of a base class in its subclass(es)? What I want is something that works theoretically like this (ignore the actual types used, they're not important): code:
|
# ? Apr 14, 2008 20:23 |
|
The Red Baron posted:I'm wondering if it's at all possible to further specialize a nested class of a base class in its subclass(es)? Not in standard C++, however the VC++ compilers have a little hacky way to make it work which is actually what is used to provide VC++ with native support for Boost.Typeof.
|
# ? Apr 14, 2008 20:32 |
|
Even if it did work, wouldn't you end up indirectly specializing base::x instead of making a specialized version just for derived::x? Also, I'm pretty sure explicit specializations aren't allowed inside a class. The following is probably closer to what you want (compiles under GCC, Comeau, and VC++2005): code:
|
# ? Apr 14, 2008 20:43 |
|
Avenging Dentist posted:Even if it did work, wouldn't you end up indirectly specializing base::x instead of making a specialized version just for derived::x? Quite possible. Hadn't thought about that, to be honest. quote:The following is probably closer to what you want (compiles under GCC, Comeau, and VC++2005):
|
# ? Apr 14, 2008 20:56 |
|
I've got a C program that I've got to insert some sorting operations into. I've got several arrays, say a, b, and c. I need to sort a into non-decreasing order, however the order of b and c must change in the same manner as a does. This is easy enough to do by swapping the elements of b and c in the same manner as a when sorting a. The problem comes into when later I've got a similar situation with arrays x and y. I don't want to have to be coding up 4-5 slight variants of the same sorting algorithm (and the number of "dependent" arrays varies from 1 to 4). How can I reasonably make only one sorting algorithm deal with this situation? Would a function pointer to a swap routine be reasonable? If needed I can restructure things to make this easier.
|
# ? Apr 14, 2008 22:11 |
|
Another C newb question, this time regarding the passing of structures to functions. I've got a structure like this: code:
code:
code:
|
# ? Apr 14, 2008 22:20 |
|
6174 posted:Would a function pointer to a swap routine be reasonable? If needed I can restructure things to make this easier. This would be quite reasonable. You'd have the indices as its parameters and just have it perform the same swap on any of an arbitrary set of arrays. quote:Can anyone point me in the right direction? You need the struct keyword before the type of the parameter (or a typedef statement for the structure definition). haveblue fucked around with this message at 22:24 on Apr 14, 2008 |
# ? Apr 14, 2008 22:22 |
|
HB posted:This would be quite reasonable. You'd have the indices as its parameters and just have it perform the same swap on any of an arbitrary set of arrays. That is what I figured, I just wanted to make sure I wasn't missing something stupid.
|
# ? Apr 14, 2008 22:24 |
|
lhunc posted:Another C newb question, this time regarding the passing of structures to functions. If you're doing pure C, you need either struct note in place of note, or the following: code:
|
# ? Apr 14, 2008 22:25 |
|
lhunc posted:Another C newb question, this time regarding the passing of structures to functions. As a general principle, you're probably better off splitting this into a declaration of a struct type, and a declaration of an array of that type. code:
quote:and then a function which takes an array of note structures, which I'm sure is wrong... This is one of the areas where C and C++ differ. In C, it's code:
Since this is really annoying, generally you take that structure and typedef it to an easier name. code:
code:
code:
|
# ? Apr 14, 2008 22:31 |
|
Avenging Dentist posted:If you're doing pure C, you need either struct note in place of note, or the following: Thanks, I've modified it to read: code:
code:
|
# ? Apr 14, 2008 22:34 |
|
Could you just post your code on https://www.pastebin.com or something?
|
# ? Apr 14, 2008 22:38 |
|
Did you declare that struct in a header file? Are you sure you're not including an older version of the header or something?
|
# ? Apr 14, 2008 22:40 |
|
edit: thanks guys, I got it - I had the structures defined after the function prototypes, switching them did the trick.
lhunc fucked around with this message at 02:22 on Apr 15, 2008 |
# ? Apr 15, 2008 02:19 |
|
Can someone tell me why this doesn't work?code:
code:
|
# ? Apr 15, 2008 21:31 |
|
almostkorean posted:Can someone tell me why this doesn't work? Well, first, atof expects a string, not a single char (and it expects a null terminator, so don't just pass it &operand). Second, once you get it to run, it's going to crash because you didn't allocate any memory for number to point to.
|
# ? Apr 15, 2008 21:35 |
|
atof takes a const char*, you're giving it a char. Beaten of course, I meant to hit preview and not post.
|
# ? Apr 15, 2008 21:35 |
|
operand is a single character and should be a char*. I'm not sure why you're using atof to parse a single character though... Also, you aren't allocating any memory for *number. EDIT: haha
|
# ? Apr 15, 2008 21:35 |
|
almostkorean posted:Can someone tell me why this doesn't work? operand is a char, atof takes a char *. You want something more like char operand[] = "123";.
|
# ? Apr 15, 2008 21:37 |
|
Also, operand is uninitialized and therefore this snippet won't do anything useful (and may possibly have undefined behavior).
|
# ? Apr 15, 2008 21:38 |
|
|
# ? Apr 18, 2024 02:02 |
|
Hopefully that's just a dumbed down snippet of code and the real code allocates memory to number and populates operand... at least, that's what I assumed.
|
# ? Apr 15, 2008 21:40 |