return c arrays

Started by
12 comments, last by Brother Bob 16 years ago
Don't use malloc in C++, it does not call constructors.
Don't use malloc if you aren't going to handle out-of-memory errors
Don't use malloc to handle local arrays of known length.

Obviously the extra function isn't required, it is to illustrate the problem.

edit: some of these points applied to a source snippet that was removed.
Advertisement
Quote:Original post by MartAlt
In C or C++, I find it's better to use "malloc" when dynamically allocating pointers. It's more stable. Also, before the end of your program, use the "free()" function to free your pointer. Otherwise, you will have a memory leak.
How is malloc() more stable than new? And what exactly do you mean by 'stability' in this context?
I was more concerned on stressing the 'impossible' thing rather than memory leaks or efficiency , or elegance.
Quote:Original post by openglaussie
I was more concerned on stressing the 'impossible' thing rather than memory leaks or efficiency , or elegance.

And at the same time, you introduced, possibly to a beginner, how to allocate memory and failed to mention you had to release it aswell. Effectively showing them how to allocate a buffer and then leak it. Such an important thing as introducing how to allocate a memory buffer should alywas introduce how to release it aswell.

This topic is closed to new replies.

Advertisement