825 Posted Topics
Re: [QUOTE=sketchiii;1477792]... but is there a way to do it using C only?[/QUOTE] C programs execute in memory. Once that process' memory is unloaded (i.e. the program exits) the values stored there are no longer valid. You can do several things to create persistent memory - which you choose depends on … | |
Re: It is not [i]necessary[/i] to initialize variables. The problem you run into with memory that is not initialized is that if you try to [b]read[/b] it you could end up with strange behavior. As gerard4143 mentioned, this is doubly important with pointers as anything other than a NULL value could … | |
Re: Using select is a viable approach on some systems. There are other input query mechanisms on other platforms. What system are you operating in? | |
Re: Use [icode]#include <string>[/icode] and [icode]#include<iostream>[/icode] instead of the versions you have. | |
Re: If the only thing you are worried about is that the number of parenthesis are balanced then you dont need a stack - although if you decide to transform the equation (to RPN, for instance) or evaluate it you will. To ensure balance the following algorithm will work:[code]// pseudocode while … | |
Re: GDB is a very powerful tool. It has many interfaces (DDD, kdbg, ...) The usefulness of this tool - regardless of its interface - is going to be your level of involvement. The more you use it and explore it's facilities the more comfortable you will become with the tool. … | |
Re: Templates are designed to generate code at compile time. Due to this design, the only way they will ever contribute to the ultimate size of a binary is by making it larger. Think of templates as incomplete sections of code that get filled in at compile time. So if you … | |
Re: [code]#include <stdio.h> #include <stdlib.h> #include <time.h> int cmp (const void * a, const void *b) { return rand() & 0x1; } int main () { char alpha[] = "abcdefghijklmnopqrstuvwxyz"; srand(time(0)); qsort (alpha, 26, 1, cmp); return printf ("%s\n", alpha); }[/code] That will spit out a random string. Is that what … | |
Re: Variable sized arrays are not supported in ISO C++ (enabling warning high enough in g++ will produce this error as well). You can use the solution posted by caut_baia to get what you want or you can use an enum [code]enum { SIZE = 10 }; int foo () { … | |
Re: The modulus operator is an integer operation. It returns to you the remainder after [i]integer[/i] division. Dividing by a floating point number gives you an exact floating point value (no remainder) so the operation makes no sense. If you want a random floating point value in a particular range I'd … | |
Re: [icode]#include <iostream>[/icode] Drop the .h Oh, and use either[code]using std::cout;[/code]or[code]std::cout << "...";[/code] or you will get more errors related to sytax. | |
Re: I'd suggest you distill this a bit until you get things working. Then put the pieces together as they are verified. FOr instance, the fact that you have the following sequence of events[code]data = (char *) malloc(size ); data = (char *) arg ;[/code]means to me that you have be … | |
Re: What happens when [icode]std::numeric_limits<int>::max() == RAND_MAX[/icode]? You'll never actually enter any of your code segments since [icode]if(n > RAND_MAX)[/icode] can not ever be true. In order to be able to return a value larger than RAND_MAX (without worrying about overflow) you will need to return a long or long long … | |
Re: Pointers are interesting (as you've realized). There is no way to tell from a pointer whether it should be deleted or not. For instance consider the following[code]Object o, * optr = &o; // Some place later in your code std::vector<Object *> optrs; optrs.push_back(optr); // this should definitely *not* be deleted … | |
Re: To add to what arkoenig mentioned, you can also provide an initialization list to your array. For instance:[code]struct Foo { int x; Foo(int i) : x(i) {} }; // Now you can do Foo foos[3] = { 1, 2, 3 }; // initializes with Foo(1), Foo(2), Foo(3)[/code]This is limited in … | |
Re: It might be the case that a shipCost is represented as an int but it might not. The fact is that the types are not the same (regardless of the underlying representation) and should be treated that way. | |
Re: It might have something to do with the way you are determining your index. I'm sure sin implementations vary widely across platforms and depending on the chip support underneath, but if I run a simple test where I do the following:[code]for( int i = 0; i < 10000; ++i) for … | |
Re: The best I can tell you is that you should look at the source of something similar to what you want to mimic ([URL="http://www.virtualbox.org/wiki/Downloads"]virtualbox[/URL]). If this is a short-term school project you have a long road ahead of you as this is - in every situation - a non-trivial endeavor. | |
Re: This is generally platform specific. You haven't specified what platform you are using and whether or not you intend on using existing libraries or not. ![]() | |
Re: A simple example of when this is useful is when you are mixing output to stdout and stderr. stderr is not buffered and stdout is so it may be the case that printing to stdout will show up at unexpected locations if it is not flushed. [Edit] For example, on … | |
Re: You can use getchar as follows [code=c]#include <cstdio> #include <string> #include <iostream> int main () { char c = getchar (); std::string s(1,c); std::cout << "Entered: " << s << std::endl; return 0; }[/code] That [i]will[/i] leave the remainder of the input unused so if you intend to read from … | |
Re: It might behoove you to break that functionality into separate functions. For instance[code]case '1': do_stuff_1_here (); break; case '2': do_stuff_2_here (); break;[/code] It helps maintain readability and understanding. | |
Re: I'd suggest you convert your current structure to a [code]std::map< std::string, int >[/code] and use that to handle your duplicates. The logic would flow something like:[code]// pseudocode void convert(array, map) { for name,value in array { map[name] += convert_to_int(value) } }[/code] You can then output to file by iterating over … | |
Re: Tracer bullets are always useful in a case like this. Consider the following updates: [code=c]X (const X& right) : myID(++id) {cerr << "Copied " << id - 1 << endl;}[/code]and[code=c]X f (X x1, X& x2) { cerr << "Enter f" << endl; x1 = x2; cerr << "Exit f" << … | |
Re: You can look [URL="http://http://www.cplusplus.com/reference/iostream/streambuf/"]here[/URL] for streambuf and [URL="http://http://www.cplusplus.com/reference/iostream/filebuf/"]here[/URL] for filebuf. Please follow-up with pointed questions regarding the above. |
The End.