malloc and realloc

This topic is 4877 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm trying to make the data in my program dynamic. I fiddled around with new and delete for a while, but decided to go with malloc and free instead. The realloc function looked nice to me, since if I grew or shrunk an array, I wouldn't have to explicitly copy my old data over. However, realloc only seems to work once. The second time I try to realloc (the second time I have to grow the array) I get access violations. So, right now, every time I need to grow the array, I'm malloc-ing a new array, copying the old data over, and then pointint to the new array. Any ideas as to why realloc is failing the 2nd time I call it?

Share on other sites
A bit of code would help. Also, realloc won't always return the same memory address, so make sure you are setting the new address to the old address.
ptr = realloc(ptr,newsize);

Share on other sites
Actually you need something like this:

ptr2 = realloc(ptr, newsize);
if (!ptr2)
return failure;
ptr = ptr2;

realloc is a bug waiting to happen IMHO.

Share on other sites
Quote:
 Original post by Anon MikeActually you need something like this:ptr2 = realloc(ptr, newsize);if (!ptr2) return failure;ptr = ptr2;realloc is a bug waiting to happen IMHO.

Sounds like someone is quoting Code Complete:)

CHeers
Chris

Share on other sites
I was doing just that. I don't have the code handy, but it's something along the lines of:
float* Array;int size = 3;...Array = malloc(sizeof(float) * size);...size *= 2;Array = realloc(Array, sizeof(float) * size);...size *= 2;Array = realloc(Array, sizeof(float) * size);

On the second realloc, it would bomb.

Share on other sites
You know, if you have access to new and delete, then you also have access to std::vector, which will do all of this stuff for you - correctly and efficiently.

Share on other sites
Are you using C++? :p

There are multiple ways to create a dynamicly sized array... either buy using new/delete or one of the STL containers.
char *array = new char[20]; // declare an array of 20 charactersarray[2]; // access character 2delete [] array; // frees up memoryarray = new char[100]; // now the array is a different size

OR

vector<int> a;a.push_back(10); // Can continue indefinatlya.erase(a.begin(), a.end()); // Clear out the vectora.size(); // the number of elements in the vectorfor(int i = 0; i < a.size(); i++){  cout << data.at(i); // One of many ways to access data}

New and delete are better because they can be used on user-defined objects with constructers and malloc/free cannot. AND EVEN BETTER: NEW AND DELTE CAN BE OVERLOADED!!!! Now why would you possibly want to use malloc/free? :p

Share on other sites
Well, for one thing, it looks like you can't truly shrink the size of a std::vector.
http://www.codeguru.com/Cpp/Cpp/cpp_mfc/stl/article.php/c4027/
It says calling .resize does not reduce the the capacity of the vector. From the article, "resize() will never shrink capacity(). ", and I never came across another method that will cause capacity to shrink. So if I want to shrink the vector, I'd have to re-create it by hand anyways.

Share on other sites
I hate to break this to you but many implmentations of realloc don't either. Most are optimized for the case where you need to grow but not for the case where it shinks. In the shinking case sometimes the "removed" space is not put in the the available to alloc list.

If this behavior is that important to you I recommend that you write your own function that creates the new size, copies the data that you want from your old array, deletes the old array, and returns the new pointer.

With that said, you may be micro-optimizing. I don't know the particulars of what you're trying to do Step back and take a look at what you're doing and make certain that you're not creating extra work, potential for bugs, and/or maintainance issues without a measurable tangible benefit before you go forward on this task. My experience in over 20 years of programming in C (damn I'm getting old) is that realloc can cause real problems and usually is not worth it.

Share on other sites
Quote:
 Original post by MauManWith that said, you may be micro-optimizing. I don't know the particulars of what you're trying to do Step back and take a look at what you're doing and make certain that you're not creating extra work, potential for bugs, and/or maintainance issues without a measurable tangible benefit before you go forward on this task. My experience in over 20 years of programming in C (damn I'm getting old) is that realloc can cause real problems and usually is not worth it.

[cool] ++ratings

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• Forum Statistics

• Total Topics
631708
• Total Posts
3001840
×