If you have an array lets say.....
char doh [100];
will that take up 100 bytes of memory??
or will it take up just what the character's amount to.....
If you have an array lets say.....
char doh [100];
will that take up 100 bytes of memory??
or will it take up just what the character's amount to.....
That would take up 100 bytes of memory. For information on dynamic memory, lookup the operators new amd delete.
like the post below said it will take up 100bytes of memory. This is why variable reuse is a good idea especially for temporary variables such as incremental counters ("i" is often used for that) etc. Dynamic memory allocation is also good and it uses and frees up memory efficently.Originally posted by doh!
If you have an array lets say.....
char doh [100];
will that take up 100 bytes of memory??
or will it take up just what the character's amount to.....
Uhhhh... only if you make proper use of delete...Originally posted by prophet
Dynamic memory allocation is also good and it uses and frees up memory efficently.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
wanna see something messed up that me and a friend discovered? In g++ and bcc i know this error occurs...but in VC++ it does not.Originally posted by Magos
Uhhhh... only if you make proper use of delete...
Kinda makes you think how efficent delete[] really is -=)Code:void main() { int *arr = new int[10]; arr[5] = 4; cout << "Before: " << arr[5] << endl; delete[] arr; cout << "After: " << arr[5]; }
[edit]
What i mean is that both the cout statements outputs the 4. Only VC++ outputs the 4 in the first part and on the second part it outputs garbage values like one would assume. I was just wondering is this normal(for g++ NOT to return garbage value for the second cout?) I was under the impression that it should be instantaneous...
[/edit]
Last edited by prophet; 04-04-2002 at 08:36 AM.
When you call delete[], the allocated memory is freed, but that doesn't mean that the data will be erased. The 4 will stay in the memory until another variable will be allocated over that exact spot. When you print arr[5] after delete, you still referense to that spot, and since no new variable has been allocated there, the 4 will be printed...Originally posted by prophet
Kinda makes you think how efficent delete[] really is -=)
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
But since the array is deleted, how does the program know what area in memory is referred to by arr[5]? At this point, shouldn't it be an undeclared variable or something?
>But since the array is deleted, how does the program know
>what area in memory is referred to by arr[5]?
arr[5] still refers to the same spot in memory, your program just doesn't own it anymore. While you can still access the memory address, it's not guaranteed that the value will still be the same, which is why it's a good practice to assign NULL to pointers you just freed the memory for.
-Prelude
My best code is written with the delete key.
is there really a difference between using delete[] and delete?
>is there really a difference between using delete[] and delete?
Yes, delete will free memory allocated as a single object and delete [] will free memory allocated as an array of objects. So if you use
char *p = new char[20];
and
delete p;
Then you have a memory leak, only the first element was freed. If you used delete [] p; then the entire array would be freed.
-Prelude
My best code is written with the delete key.
I guess I'm just dense. I understand that until it's overwritten, spot FFF070D, or wherever, in memory still contains the value 4, and that if somewhere you reference that spot, you'll get 4.
What I don't get is why arr[5] still refers to that spot, or any spot, since if arr is delete[]'d, arr[5] shouldn't exist.
It would be similar (in my muddled thinking) to using a variable out of scope. If x is declared and assigned in an inner block, it can't be used outside of that block. When the program hits the closing brace of the inner block, x is destroyed (at least that's how I learned it).
So shouldn't arr be destroyed as well? Or does delete[] only free memory - break the lock arr[5] has on that location, but arr[5] still exists? Thanks.
Last edited by salvelinus; 04-04-2002 at 12:04 PM.
>I guess I'm just dense.
Hardly, it takes a bit of thought to fully understand how memory addressing work in computers.
>So shouldn't arr be destroyed as well?
Not quite, arr is a variable that holds an address, that address is the location that the computer picked for arr[0] when you allocated memory for it. When you free the memory for arr, the variable still exists and still points to the same location in memory. The difference is that the computer is free to take that memory and use it for something else, it's not yours anymore. Since arr still points to the same address, adding 5 to the address will take you to the address where the value of arr[5] resided.
That value may still be there and it may not since the computer can do whatever it wants with the address. Pointers can point to any arbitrary address and view the value that is there. You don't have to own the address the point to it. This is a part of pointers that people find confusing.
-Prelude
My best code is written with the delete key.
is it compiler or OS dependent?Then you have a memory leak, only the first element was freed. If you used delete [] p; then the entire array would be freed.
this didn't increase the swap file (win98) on my computer except when i ran multiple copies, and then only by ~50megs for each one that required the swap file (because RAM was used up).Code:long y = 50000000L; char* x; x = new char [y]; while(x) { delete x; x = new char[y]; }
But:
constantly increased my swap file as expected. I had over a 2gig swap file when i finally terminated the second code.Code:long y = 5000L; char* x; x = new char [y]; while(x) { x = new char[y]; }
where was the leek in the first code? wouldn't it constantly increase by 50meg-1byte?
You delete (deallocate) the first element of x, not the rest. Use delete[] to delete it all.Originally posted by Syneris
where was the leek in the first code?