PDA

View Full Version : malloc() w/ array pointers


BWiz
10-27-2009, 12:44 AM
Just a quick question, if I used malloc() to assign enough space for 20 integer elements (so 20 * 4 = 80 bytes) to a pointer, if I add an extra integer, to the end, so I have 21 elements, why don't I get an error? Does the program continue to expand the memory allocation in the heap?

BrickInTheWall
10-27-2009, 01:32 AM
If you access data outside of what you allocated on the heap, then you are accessing data that you shouldn't be accessing. There is something else there that might even cause problems if you overwrite it.

If you have an array:

char* myCharArray = new char[20];

Doing something like myCharArray[20] = 23; would not be a good idea. If you want to expand the array size you'll have to copy it, delete it, create a new array with a bigger size (+1 for example) and put back what you coppied. But for this kind of work you can use the std::vector class in C++.

edit: And remember, if you're using malloc() in C++ then you're going to have to cast it's return type because it returns a pointer to void (void*). In C this wouldn't be necessary if I remember correctly.

oracleguy
10-27-2009, 04:32 PM
The reason it doesn't produce an error is that there is no bounds checking on allocated memory. If you go over an array, it could produce an error, but it might not. It depends what memory you overwrite. See this post I made for some more information with a code example: http://www.codingforums.com/showpost.php?p=880558&postcount=4

Making sure you don't go out of bounds is the responsibility of the programmer.

BWiz
10-28-2009, 01:58 AM
Well, I'm hoping that my math is correct so I avoid writing over other memory values. I have an array w/ maximum possible 20 elements. So I allocate 20 * sizeof(int) = 80 bytes. Then I retrieve the data from a file - and keeping track by using a counter, I enter all the elements into the array.

After that, I reallocate the memory using (counter + 1) * sizeof(int). The extra one I place the NULL terminator into.

Am I doing it right?

oesxyl
10-28-2009, 02:20 AM
Well, I'm hoping that my math is correct so I avoid writing over other memory values. I have an array w/ maximum possible 20 elements. So I allocate 20 * sizeof(int) = 80 bytes. Then I retrieve the data from a file - and keeping track by using a counter, I enter all the elements into the array.

After that, I reallocate the memory using (counter + 1) * sizeof(int). The extra one I place the NULL terminator into.

Am I doing it right?
in my opinion is useless to put a NULL there, NULL means 0.
I would use two variables
- one to store how many elements I have in the array
- one to store which is the first free place in the array.

best regards

oracleguy
10-28-2009, 04:27 PM
After that, I reallocate the memory using (counter + 1) * sizeof(int). The extra one I place the NULL terminator into.

Am I doing it right?

The null is only needed on the end of c-strings, there is no practical reason to use it in this case.