|
Okay, I've been using a lot of STL lately, and it scares me a lot because I don't really understand how something like an stl string manages its memory. If I declare a regular std::string foo, it would automatically allocate itself on the heap I'm assuming? Does it use reference counting or something to determine when to automatically delete itself? What about string's c_str? Where is this memory? I assume there are no guarantees using this memory outside the immediate scope of the string?
|
# ? Jul 22, 2008 04:12 |
|
|
# ? May 21, 2024 15:54 |
|
Sabotaged posted:Okay, I've been using a lot of STL lately, and it scares me a lot because I don't really understand how something like an stl string manages its memory. I am probably wrong, but I think string allocates itself on stack if you allocate it on stack and just wraps char * inside of it. It deletes itself when it is popped from stack or when you delete it yourself assuming you put it into dynamic memory.
|
# ? Jul 22, 2008 04:23 |
|
Entheogen posted:I am probably wrong, but I think string allocates itself on stack if you allocate it on stack and just wraps char * inside of it. It deletes itself when it is popped from stack or when you delete it yourself assuming you put it into dynamic memory. Lololololololololololololonononononononononononono! Just imagine that a string is the same thing (internally) as a std::vector<char> -- that's a close enough approximation. sarehu fucked around with this message at 04:28 on Jul 22, 2008 |
# ? Jul 22, 2008 04:26 |
|
Sabotaged posted:If I declare a regular std::string foo, it would automatically allocate itself on the heap I'm assuming? Does it use reference counting or something to determine when to automatically delete itself? The string object is on the stack. Then it goes and allocates a bunch of memory on the heap for the string data. This memory will be deallocated when the string object itself goes out of scope. This is achieved by the string object's destructor. The c_str() memory is on the heap, too. There are indeed no guarantees about using that memory outside the immediate scope of the string. You are not even guaranteed that it is usable if you edit the string after calling c_str(), I think.
|
# ? Jul 22, 2008 04:27 |
|
That makes sense, but what about vectors? Is it the same deal? I could swear I remember seeing some code where a vector was declared within a method, but then returned as the result.
|
# ? Jul 22, 2008 04:34 |
|
Sabotaged posted:That makes sense, but what about vectors? Is it the same deal? I could swear I remember seeing some code where a vector was declared within a method, but then returned as the result. That makes a copy of the vector using its copy constructor.* * Does not apply to C++0x
|
# ? Jul 22, 2008 04:37 |
|
Avenging Dentist posted:That makes a copy of the vector using its copy constructor.* Ah, I see. Does this apply to other stl containers? What about string?
|
# ? Jul 22, 2008 04:45 |
|
Yes.code:
|
# ? Jul 22, 2008 04:51 |
|
Avenging Dentist posted:Yes. http://msdn.microsoft.com/en-us/library/ms364057(VS.80).aspx
|
# ? Jul 22, 2008 09:48 |
|
What, precisely, is the difference between a reference to a variable and a pointer to it? I've always thought of a reference as being a pointer with nicer syntax but apparently this is very very wrong.
|
# ? Jul 22, 2008 15:10 |
|
Big Mark posted:What, precisely, is the difference between a reference to a variable and a pointer to it? I've always thought of a reference as being a pointer with nicer syntax but apparently this is very very wrong. It's a pointer with nicer syntax that is guaranteed never to be NULL. That second bit is important, because it means you can never declare a reference variable and just leave it with nothing meaningful in it until you do some more calculation. (That means no "Object &some_object; if (cond) { ... initialize one way ... } else { ... initialize another way ... }") And you can't do pointer arithmetic on it, or do funny casts. Which (I think) means the compiler's better able to optimize it.
|
# ? Jul 22, 2008 15:47 |
|
Big Mark posted:What, precisely, is the difference between a reference to a variable and a pointer to it? I've always thought of a reference as being a pointer with nicer syntax but apparently this is very very wrong. code:
code:
|
# ? Jul 22, 2008 15:52 |
|
JoeNotCharles posted:It's a pointer with nicer syntax that is guaranteed never to be NULL. I am sorry to say this completely wrong. C++ lets you do terrible terrible things sometimes: code:
litghost fucked around with this message at 16:16 on Jul 22, 2008 |
# ? Jul 22, 2008 16:13 |
|
litghost posted:I am sorry to say this completely wrong. C++ lets you do terrible terrible things sometimes: No, your code is what's wrong. ref_bob's initialization has undefined behavior as does trying to access ref_bob2 after bob2 was deleted. There is no way to legally work with a null reference and it is safe for your compiler to rely on that fact. Null pointers on the other hand are perfectly fine and can legally be formed.
|
# ? Jul 22, 2008 16:26 |
|
I hate C++ so much.
|
# ? Jul 22, 2008 16:26 |
|
litghost posted:I am sorry to say this completely wrong. C++ lets you do terrible terrible things sometimes: Before you even touch ref_bob, you dereference of a null pointer, which is undefined behavior. You can't blame the language for anything that happens after you do something undefined. If your compiler happens to allow the operation to proceed and segfault on a reference later, that's fine, but that doesn't have anything to do with how references are defined to work in C++. Edit: And as far as the heap corruption trick, references are guaranteed not to be null; they're not guaranteed to always be valid (try returning a reference to something on the stack for a similar effect). Smackbilly fucked around with this message at 16:33 on Jul 22, 2008 |
# ? Jul 22, 2008 16:26 |
|
Smackbilly posted:Before you even touch ref_bob, you dereference of a null pointer, which is undefined behavior. You can't blame the language for anything that happens after you do something undefined. If your compiler happens to allow the operation to proceed and segfault on a reference later, that's fine, but that doesn't have anything to do with how references are defined to work in C++. But the code compiles and runs (and dies horribly). My point was not that you should be able to do these thing, but that they are possible. You CAN have a reference to a NULL pointer, you can hold a reference to de-allocated memory. In short: JoeNotCharles posted:I hate C++ so much. :edit: I tested on both VC++ 2005 express and gcc version 3.4.4 (cygming special, gdc 0.12, using dmd 0.125). They both allow the NULL pointer to enter the reference. Seg fault occurs when you try to access the data. litghost fucked around with this message at 16:46 on Jul 22, 2008 |
# ? Jul 22, 2008 16:33 |
|
litghost posted:But the code compiles and runs (and dies horribly). My point was not that you should be able to do these thing, but that they are possible. You CAN have a reference to a NULL pointer, you can hold a reference to de-allocated memory. In short: Just because your code compiles and runs doesn't mean the code is valid C++. The C++ standard leaves a number of things "undefined", and if you do any of those things your code is no longer valid C++ and all bets are off. The canonical admonition is "undefined behavior can do anything, including doing nothing, crashing your program, or erasing your hard drive." If anything your complaint is that your compiler is not smart enough to detect that what you have written is not C++ and yet attempts to compile it as C++ anyway.
|
# ? Jul 22, 2008 16:39 |
|
Smackbilly posted:Just because your code compiles and runs doesn't mean the code is valid C++. The C++ standard leaves a number of things "undefined", and if you do any of those things your code is no longer valid C++ and all bets are off. The canonical admonition is "undefined behavior can do anything, including doing nothing, crashing your program, or erasing your hard drive." WOOOSH. You have completely missed the point. My point is references are not guaranteed to be anything, just like pointers. They are not some run time protected concept or compile time protected either, they are syntactical sugar over raw memory access (maybe). Substituting references with pointers will only improve the syntax (good) not improve you ability to avoid memory faults (do not think this). Avoiding undefined behavior is hard work that must be achieved with your vigilance, not the compilers. Which is frankly dumb. So, again, my point is: JoeNotCharles posted:I hate C++ so much.
|
# ? Jul 22, 2008 16:54 |
|
litghost posted:WOOOSH. You have completely missed the point. My point is references are not guaranteed to be anything, just like pointers. And our point is that, yes, they are. That is what the standard dictates. If your compiler doesn't adhere to the standard, then you need to be careful of it; that's an issue with considerably more things than just null-references. Besides that, the initial point was that references are semantically non-null. If you are using null references, you are being evil and deserve whatever bad things happen to you.
|
# ? Jul 22, 2008 17:05 |
|
Avenging Dentist posted:And our point is that, yes, they are. That is what the standard dictates. If your compiler doesn't adhere to the standard, -->then you need to be careful of it<--; that's an issue with considerably more things than just null-references. This, right here, is my point. You need to be careful of your compiler. It will not protect you from doing stupid things, with a pointer or a reference. Or any other C++ caveat that is not obvious. In practice, with probably any compiler you can find, null references can exist. If you need a function that is absolutely bullet-proof, you cannot assume anything. I am not advocating creating null references, only that such a thing can exist. FYI C++ spec 8.3.2: quote:[Note:in particular,a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior.] litghost fucked around with this message at 18:03 on Jul 22, 2008 |
# ? Jul 22, 2008 17:50 |
|
It's the C++ thread, not the your-C++-compiler thread.
|
# ? Jul 22, 2008 18:02 |
|
Vanadium posted:It's the C++ thread, not the your-C++-compiler thread. Fair, but the realities of C++ mean that you will encounter bugs that are "against" the spec, but practically can exist. Putting you head in the ground and saying that they are impossible does not change the fact they can exist. Honestly I think a discussion of the caveats of the C++ spec would be enlightening. I am sure other people have run into fun things like virtual functions in constructors. I'll be quiet now, sorry for the derail.
|
# ? Jul 22, 2008 18:09 |
|
litghost posted:WOOOSH. You have completely missed the point. My point is references are not guaranteed to be anything, just like pointers. They are not some run time protected concept or compile time protected either, they are syntactical sugar over raw memory access (maybe). Substituting references with pointers will only improve the syntax (good) not improve you ability to avoid memory faults (do not think this). Avoiding undefined behavior is hard work that must be achieved with your vigilance, not the compilers. Which is frankly dumb. So, again, my point is: The point is that while code:
code:
|
# ? Jul 22, 2008 18:57 |
|
more falafel please posted:The point is that while Isn't "well-formed" a compile-time concept? So how can it depend on the runtime value of p?
|
# ? Jul 22, 2008 19:27 |
|
JoeNotCharles posted:Isn't "well-formed" a compile-time concept? So how can it depend on the runtime value of p?
|
# ? Jul 22, 2008 20:12 |
|
litghost posted:Fair, but the realities of C++ mean that you will encounter bugs that are "against" the spec, but practically can exist. Putting you head in the ground and saying that they are impossible does not change the fact they can exist. I just get a bit annoyed, for some reason, when some people blame C++ if their compiles do not detect the mistakes in their code. Now, for something, completely different, what is up with enum foo { a = 1, b = 2 }; foo f = a; f |= b; not being legal? Am I supposed to roll my own "flags" class?
|
# ? Jul 22, 2008 22:35 |
|
Vanadium posted:
code:
|
# ? Jul 22, 2008 22:51 |
|
May as well define operator '|' while you're at it.code:
Mustach fucked around with this message at 23:19 on Jul 22, 2008 |
# ? Jul 22, 2008 23:03 |
|
I prefer the following syntax for the flag declarations:code:
Importantly, don't forget to cast the flags to integer types within the operator| and operator& definitions, otherwise you'll end up with an infinite recursive function.
|
# ? Jul 23, 2008 00:06 |
|
I really need help with this programming problem. The program I have to code for my class is one that will read the numbers from a file called "numbers.txt" then write only the even numbers to a new sequential access file named evenNumbers.txt. The only hint I had was to use the % modulus operator. This is the contents of the numbers.txt file: 100 23 45 78 95 92 10 7 5 4 2 8 Then this is the contents of the c++ file I have created so far that I'm having trouble with: code:
1 1 1 1 1 to the file. Any help would be appreciated.
|
# ? Jul 23, 2008 02:06 |
|
you need to store your numbers in an array not an int. int is for just one number that is represented with 4bytes usually. Also you are assigning evennumbers to store numbers % 2 before you even read store numbers from file. The reason it outputs only 1s is because you are only printing out one variable which never changes, and got set to 1 the last time you modified it. That is because the last number to be assigned to evennumber is 2 % 2 == 0 which is true or 1. Also don't do while ( !outFile.eof() ) to print stuff to that file stream. You only need to check for that when you are reading it in. oh and to catch errors, like whether files were succefully opened or not, you can just surround the whole thing with try catch block and catch std::exception & exc. I am not sure if my code takes care of new line and carriage return characters for input. If you run into problems you can use .ignore function, but the way you should use it is code:
just do something like this. I use vector here which is STL class for dynamic arrays (they can be resized easily). code:
Entheogen fucked around with this message at 02:23 on Jul 23, 2008 |
# ? Jul 23, 2008 02:13 |
|
Is there a way to do this without vectors? We haven't gotten that far so do you know of a way strictly using arrays?
|
# ? Jul 23, 2008 02:34 |
|
Alternatively, you could just loop until EOF and if the current number % 2 is 0, add it to the output file. That way there is no need for an array or knowing how many numbers you should be prepared to deal with.
|
# ? Jul 23, 2008 02:35 |
|
TheSleeper posted:Alternatively, you could just loop until EOF and if the current number % 2 is 0, add it to the output file. That way there is no need for an array or knowing how many numbers you should be prepared to deal with. This is the correct way. Open both files, iterate through the input and drop the number into the output if it's even. I would recommend asking for some tutoring from a TA or another student. Programming is very hard to grasp at first, and getting help from someone during the process can get you on the right path. It can also help clear up any misunderstandings so that they don't follow you any longer than they have to. blorpy fucked around with this message at 02:43 on Jul 23, 2008 |
# ? Jul 23, 2008 02:39 |
|
Can you give me a sample of what you two just said? My book isn't very clear and I have a very very limited time to finish this one program. I appreciate all of the help.
|
# ? Jul 23, 2008 02:48 |
|
code:
|
# ? Jul 23, 2008 02:51 |
|
Doctor Chef posted:Can you give me a sample of what you two just said? I actually have no qualms with writing homework problems, since the burden of learning is still on the student. I'm going to modify your source a bit to basically do what I was saying. I'm not going to use anything you haven't, though. Just a bit of copy/paste. code:
|
# ? Jul 23, 2008 03:05 |
|
^^^ Thank you all very much for your help. Again, it's very much appreciated. I think I see where the problem was now.
|
# ? Jul 23, 2008 04:16 |
|
|
# ? May 21, 2024 15:54 |
|
Chain Chomp posted:I actually have no qualms with writing homework problems, since the burden of learning is still on the student. No offense, but isn't it a whole lot better to at most provide a pseudocode solution? Also, it doesn't really matter much in this case, but with your solution if, for some reason, it fails to open the input file but opens the output file just fine it will never close the output file. The best solution would be to open the output file after testing to see if the input file was open.
|
# ? Jul 23, 2008 05:38 |