how to malloc memory properly?

Started by
21 comments, last by mrchrismnh 12 years, 9 months ago
Hi,

I have a problem with malloc function.
I am using mallog in the manner:

TMyClass *ClInstancePtr;

CInstancePtr=(TMyClass*) malloc(sizeof(TMyClass)*NumOfElements);

And it doesn't work properly. It behaves just lke I hadn't allocated anything.
F ex.:

ClInstance[2]=some values;
cout<<ClInstance<<endl;
output - some random data.

What am I doing wrong?

Thanks in advance for any help.
Regards,
Misery.

PS.: When I use new[ ]/delete [ ] it works fine.
Advertisement
Using malloc doesn't construct any object in the array, it only allocates the requested memory.

Why do you need to use malloc instead of new[]?

Why do you need to use malloc instead of new[]?


Because I want to have possibility to resize my data without copying it.
How to make an array in place of malloc?
You must copy the pointers but do not need to copy through objects referenced by them. New does not provide an equivalent to realloc unfortunately :-(

[quote name='vdaras' timestamp='1309616091' post='4830346']
Why do you need to use malloc instead of new[]?


Because I want to have possibility to resize my data without copying it.
How to make an array in place of malloc?
[/quote]

Use stl containers instead, it will save you a lot of bother. However, for the sake of explanation... you'll have to copy the data over anyway as malloc will allocate memory at a DIFFERENT address to the one you got when you first called malloc, thus you will lose the data. Not only that, but it will be leaked memory as you didn't free it before reallocating new memory. Anyway, use the new and delete operators as they are the "proper way of doing things", and for good reason. To allocate and delete arrays, you use new[] and delete[] operators respectively. If you wish to resize a dynamically created array, you would allocate a temporary dynamic array of existing size and then copy the contents of your existing array across. Then you would delete the contents of your existing array and reallocate a new array of the desired size, before transferring the contents of your temporary array across. Finally, delete you would the temporary array.

[quote name='vdaras' timestamp='1309616091' post='4830346']
Why do you need to use malloc instead of new[]?


Because I want to have possibility to resize my data without copying it.
How to make an array in place of malloc?
[/quote]

why not just use a vector or list ? (std::vector or std::list)

anyway as vdaras said malloc doesn't construct the object (the constructor is never called) it only allocates memory for it. (malloc is the C way of allocating memory, C doesn't support classes so trying to use a memory block allocator to create class instances will result in some major headaches)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Use std::vector and you are golden.
Why not stl?
Does something like vector<bool> tell You guys something?:cool:
Thats why not stl.

Does something like vector<bool> tell You guys something?:cool:



I'm not following. You can have containers of objects, no problem.

Why not stl?
Does something like vector<bool> tell You guys something?:cool:
Thats why not stl.



Don't use STL because it allows you to create a resizeable bool array?
"It's like naming him Asskicker Monstertrucktits O'Ninja" -Khaiy

This topic is closed to new replies.

Advertisement