Valhalla Legends Forums Archive | C/C++ Programming | [C++] Memory allocation and deallocation questions

AuthorMessageTime
bethra
Ok, I found a pretty good webpage that explained which method of deallocation should be used with the different methods of allocation (new/delete and malloc()/free()).

A quote from the page says:
[quote]
Note on mixing source code containing new/delete and malloc()/free(). This is not a problem with the GNU C++ compiler but this is not guarenteed for all C++ compilers.
[/quote]

Ok, so, here are my questions:

1.  Even though I'm using VC++ 6.0 and it doesn't seem mind me mixing new/delete and malloc()/free(), should I still refrain from doing it?

2.  Should I stick with one or the other (new/delete or malloc()/free())?

3.  If I were to stick with only using new/delete, can I still dynamically reallocate an existing block of memory that has already been allocated using new?

4.  Is it alright to use new/delete for some things and malloc()/free() for another in the same source file?

Thanks.
December 14, 2005, 5:31 PM
Yoni
[quote author=Sorc.Polgara link=topic=13516.msg137615#msg137615 date=1134581504]
1. Even though I'm using VC++ 6.0 and it doesn't seem mind me mixing new/delete and malloc()/free(), should I still refrain from doing it?
[/quote]
YES!

[quote author=Sorc.Polgara link=topic=13516.msg137615#msg137615 date=1134581504]
2. Should I stick with one or the other (new/delete or malloc()/free())?
[/quote]
It is good coding style to use only one of the possibilities. You can mix them if you're sure you can manage where each is used and you're sure you'll never mix them. I recommend you use only one, though, unless you already have a lot of code written and think it's a waste of time to rewrite the allocations.

[quote author=Sorc.Polgara link=topic=13516.msg137615#msg137615 date=1134581504]
3. If I were to stick with only using new/delete, can I still dynamically resize an existing block of memory that has already been allocated using new?
[/quote]
You will need to implement realloc() yourself: Make a new block, memcpy the relevant data, delete[] the old block, return the new one. (That's probably how realloc() is internally implemented, anyway.)

[quote author=Sorc.Polgara link=topic=13516.msg137615#msg137615 date=1134581504]
4. Is it alright to use new/delete for some things and malloc()/free() for another in the same source file?
[/quote]
Same as answer to #2.
December 14, 2005, 7:05 PM
bethra
[quote author=Yoni link=topic=13516.msg137637#msg137637 date=1134587133]
[quote author=Sorc.Polgara link=topic=13516.msg137615#msg137615 date=1134581504]
1. Even though I'm using VC++ 6.0 and it doesn't seem mind me mixing new/delete and malloc()/free(), should I still refrain from doing it?
[/quote]
YES!

[quote author=Sorc.Polgara link=topic=13516.msg137615#msg137615 date=1134581504]
2. Should I stick with one or the other (new/delete or malloc()/free())?
[/quote]
It is good coding style to use only one of the possibilities. You can mix them if you're sure you can manage where each is used and you're sure you'll never mix them. I recommend you use only one, though, unless you already have a lot of code written and think it's a waste of time to rewrite the allocations.

[quote author=Sorc.Polgara link=topic=13516.msg137615#msg137615 date=1134581504]
3. If I were to stick with only using new/delete, can I still dynamically resize an existing block of memory that has already been allocated using new?
[/quote]
You will need to implement realloc() yourself: Make a new block, memcpy the relevant data, delete[] the old block, return the new one. (That's probably how realloc() is internally implemented, anyway.)

[quote author=Sorc.Polgara link=topic=13516.msg137615#msg137615 date=1134581504]
4. Is it alright to use new/delete for some things and malloc()/free() for another in the same source file?
[/quote]
Same as answer to #2.
[/quote]
Ok, thanks.

One last question, If I have a class with a private integer (not an array of integers) variable and I want delete it/remove from memory in the destructor, would I just set its value to NULL?
December 14, 2005, 7:50 PM
Kp
[quote author=Sorc.Polgara link=topic=13516.msg137647#msg137647 date=1134589849]One last question, If I have a class with a private integer (not an array of integers) variable and I want delete it/remove from memory in the destructor, would I just set its value to NULL?[/quote]

No.  Setting a pointer to NULL never frees it automatically (though you can create objects that behave mostly like pointers and react specially to being reassigned, so that reassignment has an implicit free...).  Show us the declaration to be sure.  If you declared it as int x;, it's just a member of the structure and it will go away when the structure does.  Ditto if it's declared as int x[10]; -- it's 10 integers, one after another, all inside the structure.  When the structure goes, so do they.  If you declared it as int *x;, then it's a pointer that you'll need to free.  If you initialized it as x = new int;, then use delete x; to get rid of it.  If you initialized it as x = new int[10];, then use delete [] x; to get rid of it.

If you have some places doing x = new int; and others doing x = new int[10];, that's really bad.  Don't do that. :)

Also be sure that it's initialized before you try to free it.  One way to ensure this is to assign it a value in the constructor.  It's safe to free/delete the NULL pointer, so if you don't want to allocate storage at construction time, just set x to NULL.  Finally, why're you allocating storage for a single int?  Just make it a member of the structure!
December 14, 2005, 8:04 PM

Search