new, delete and arrays
I have a few questions about new and delete with arrays. Here they are:
1. To delete an array made with new, you have to do the following:
delete [] NewArray;
Is this what you have to do for multidimensional arrays also? Or do you add extra brackets?
2. If I do the following:
int * ArrayOfPointers[5][5];
for (int loop1 = 0; loop1 < 5; loop1++)
{
for (int loop2 = 0; loop2 < 5; loop2++)
{
ArrayOfPointers[loop1][loop2] = new int[5];
}
}
Can I delete all the "new" things at the same time by doing this:
delete ArrayOfPointers;
Or do I have to set up another loop to delete things individually.
3. Finally, If I had a structure like the following:
struct EXAMPLE
{
int * var1;
int * var2;
int * var3;
};
If I made the an instance of the structure using new, then made the things in the structure using new, by deleting the structure would all the memory allocated for the variables in the structure be deallocated?
Thanks for any help!
1. Nope. Your wasting memory
Here''s how you''d do it:
for (int index=0;index < (SIZE OF FIRST ARRAY);index++)
delete [] multid_array[index]; // delete each second array
delete multid_array; // delete first array
2. I think you can do what your doing now.
3. I''m not sure what your asking... But I think the answer''s yes
If you deallocate the structure''s array you also deallocate the other variables inside.
Not sure about that though...
The road to success is always under construction
Here''s how you''d do it:
for (int index=0;index < (SIZE OF FIRST ARRAY);index++)
delete [] multid_array[index]; // delete each second array
delete multid_array; // delete first array
2. I think you can do what your doing now.
3. I''m not sure what your asking... But I think the answer''s yes
If you deallocate the structure''s array you also deallocate the other variables inside.
Not sure about that though...
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
The answer to 3 is NO. Here is the reason:
If you allocate that struct with new it allocates space for 3 integer pointers which equates to 12 bytes (on a 32-bit machine).
If you use delete on the structure all you are doing is freeing the memory allocated for the structure and hence deleting the integer pointers. The memory that those integer pointers are referencing is not freed. Therefore, if you delete the struct before freeing the memory (with delete) that the integer pointers point to, you have a memory leak.
What I'd recommend if you don't want to use class accessor functions (like GetX, GetY etc) just change the struct into a class with a constructor and destructor...
That will ensure you have no memory leaks.
There is one problem above though. If you don't want shallow copying of p1, p2, p3 you must implement a copy constructor and assignment operator (operator =(const EXAMPLE&))
----------
The other way to do this is to just write a function:
void DeleteEXAMPLE(struct EXAMPLE& target)
{
if(target.var1)
delete var1;
if(target.var2)
delete var2;
if(target.var3)
delete var3;
}
I hope this helps,
- Dire Wolf
direwolf@digitalfiends.com
Edited by - Dire.Wolf on September 7, 2000 6:28:02 PM
If you allocate that struct with new it allocates space for 3 integer pointers which equates to 12 bytes (on a 32-bit machine).
If you use delete on the structure all you are doing is freeing the memory allocated for the structure and hence deleting the integer pointers. The memory that those integer pointers are referencing is not freed. Therefore, if you delete the struct before freeing the memory (with delete) that the integer pointers point to, you have a memory leak.
What I'd recommend if you don't want to use class accessor functions (like GetX, GetY etc) just change the struct into a class with a constructor and destructor...
class EXAMPLE{ public: EXAMPLE(int* x = 0, int* y = 0, int* z = 0) : p1(new int(x)), p2(new int(y)), p3(new int(z)) { } ~EXAMPLE() { if(p1) delete p1; if(p2) delete p2; if(p3) delete p3; } // public data members (could be made private) int *p1, *p2, *p3;};
That will ensure you have no memory leaks.
There is one problem above though. If you don't want shallow copying of p1, p2, p3 you must implement a copy constructor and assignment operator (operator =(const EXAMPLE&))
----------
The other way to do this is to just write a function:
void DeleteEXAMPLE(struct EXAMPLE& target)
{
if(target.var1)
delete var1;
if(target.var2)
delete var2;
if(target.var3)
delete var3;
}
I hope this helps,
- Dire Wolf
direwolf@digitalfiends.com
Edited by - Dire.Wolf on September 7, 2000 6:28:02 PM
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
SteveBrown, What I wrote before was a mistake, In question 1:
After you deallocate each second array (With the loop) you need to release to first array like so:
delete [] multid_array; // delete first array
Not how I wrote.
I didn''t notice it when I wrote the reply, sorry
After you deallocate each second array (With the loop) you need to release to first array like so:
delete [] multid_array; // delete first array
Not how I wrote.
I didn''t notice it when I wrote the reply, sorry
Goblineye EntertainmentThe road to success is always under construction
I just noticed a boo-boo.
The constructor is wrong it should be:
My original constructor was taking integer pointers as arguments instead of values that you want assigned. That would have been OK except for the fact that I tried to new an integer with the value of an integer pointer (memory address). Dumb
- Dire Wolf
direwolf@digitalfiends.com
class EXAMPLE{ public: EXAMPLE(int* x = 0, int* y = 0, int* z = 0) : p1(new int(x)), p2(new int(y)), p3(new int(z)) { }...(snip)...
The constructor is wrong it should be:
class EXAMPLE{ public: EXAMPLE(int x = 0, int y = 0, int z = 0) : p1(new int(x)), p2(new int(y)), p3(new int(z)) { }...(snip)...
My original constructor was taking integer pointers as arguments instead of values that you want assigned. That would have been OK except for the fact that I tried to new an integer with the value of an integer pointer (memory address). Dumb
- Dire Wolf
direwolf@digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
Dire.Wolf:
The dtor of EXAMPLE should prob be written like this:
Deleting a NULL pointer has no effect, so the if statements just add extra noise to the function. It might also be faster because there''s no branches in the function.
Also if you want to keep the struct for any reason, you can add a destructor to it and leave everything public.
The dtor of EXAMPLE should prob be written like this:
~EXAMPLE(){ delete p1; delete p2; delete p3;}
Deleting a NULL pointer has no effect, so the if statements just add extra noise to the function. It might also be faster because there''s no branches in the function.
Also if you want to keep the struct for any reason, you can add a destructor to it and leave everything public.
Good point Wilka.
I only chose to make it a class for consistency. I dislike seeing structs in my C++ code. Since structs are evaluated as classes with public data, why not make it explicit - just preference I guess
- Dire Wolf
direwolf@digitalfiends.com
I only chose to make it a class for consistency. I dislike seeing structs in my C++ code. Since structs are evaluated as classes with public data, why not make it explicit - just preference I guess
- Dire Wolf
direwolf@digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
I think this thread is apropriate to answer a question that I have. Using C, you have the realloc function, which takes a pointer and can resize the allocated memory(or malloc or even dispose the memory, depending on the arguments).
So my question is, what exactly am I doing when I write new[some_size] ? Does it alloc memory like malloc would do? Is there any way to resize the allocated memory(if I discover I need more space, for example).
What I''m trying to implement is some sort of dynamic array, but with the capacity of being resized, or at least expanded is I see fit. I don''t want to use realloc with C++ code, but I''ll do if it''s the only solution.
Thanks for any help.
So my question is, what exactly am I doing when I write new[some_size] ? Does it alloc memory like malloc would do? Is there any way to resize the allocated memory(if I discover I need more space, for example).
What I''m trying to implement is some sort of dynamic array, but with the capacity of being resized, or at least expanded is I see fit. I don''t want to use realloc with C++ code, but I''ll do if it''s the only solution.
Thanks for any help.
Gaiomard Dragon-===(UDIC)===-
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement