Hello all, I have a question about dynamic memory allocation (if I'm in the wrong forum please let me know!). I wrote a small practice program to try to understand why the dynamic allocation operator (as my book calls it) 'delete' is necessary. This program just accepts a string and copies it to the 'ast[y]' array:
I purposedly made so that the program displays more elements than were defined for the array.
My question is, if it is possible to redefine a variable during, at least this program's run, what is the advantage in using pointers and the allocation operators 'new' and 'delete' to create and delete a new variable/array/object.
Also, on another practice program, I deleted a variable but could still access the data that was in that variable before using 'delete', so how exactly does 'delete' work??
This is not standard c++ code -> char ast[y]; (because y is not a constant). Maybe your compiler allows it but not all compilers do, so it's not a good idea. Use char * ast = newchar[y]; instead.
EDIT:
rdangelo wrote:
I deleted a variable but could still access the data that was in that variable before using 'delete', so how exactly does 'delete' work??
This is a very rough description but think of new and delete operators as markers of memory areas. When you reserve memory with new you effectively tell your program/OS that you want that memory to be modified only by you. When you delete that memory you effectively remove that restriction. Of course you can access that address and view its contents but nobody knows what is stored there. Maybe it's as you left it, maybe it's not...
Thanks for the reply! now it became crystal clear how 'new' and 'delete' works, thanks!
Now, going back to the first question, seems like your answer hits the heart of it, but I'm still on my way to fully understand. From my understanding, at compile time, the complier does the same as 'new' and 'delete', but the restriction remains for the lifetime of the vairable/program, and we are not allowed to remove that restriction manually (with 'delete') during the program's run. But the program that I wrote seems to do just that (I'm using Ubuntu Lynux 9.04 and gedit)! So from your answer, that's not normal because the compiler cannot reserve a fixed amount of storage space for ast[y] before the program is run (y is not a constant!). So I'm not sure how my program is compiled, but from what I've read so far, the advantage of using pointers is that it allows a programmer to "manage" computer memory manually during a program's run by reserving memory spaces and removing them at will.
I hope that's correct!
Why not use a std::vector<char> or std::deque<char> and avoid the worry of using new and delete for your dynamic array needs? You are already using std::string so you might as well take the next step and learn to use the other containers. vector and deque are the first two on the list of container references. http://cplusplus.com/reference/stl/
That was a lot of new material, I'm still going to look at all of it more carefully (and maybe even post about it!).
I see that m4ster's program is like mine in that it defines a variable length array during the program's run. The terms 'stack' and 'heap' are still relatively new to me so I have some more reading to do on those, and maybe even find out how my computer is compiling a variable length array.
'vector' and 'deque' are actually new terms to me, my book doesn't seem to go that far so I'll be doing some advanced reading soon.
I'm afraid not. That link describes C99, however C and C++ are not compatible in this regard. The code you pasted is illegal in C++ - it did not compile on 2 standards conforming compilers which I tried it on.
I gather that you didn't read the whole topic. Because if you did, you would see that I already mentioned that in my first reply. The link is a list of the gcc extensions to the language.