Reputation: 1420
I have an array of pointers
char *wordlist[9];
and then I malloc() a block of memory on every of this pointers
for(int i=0; i<9; i++)
wordList[i] = (char*)malloc(someLength);
Lets suppose that every time the someLength is different.
And the problem is now, that I want to realloc() ie. 4th elemet of wordList to a larger size than it is now.
wordList[3] = (char*) realloc(&wordList[3], someBiggerSize);
Since malloc allocates a consistent block of memory, is that operation even possible without colliding with wordList[4]?
Upvotes: 1
Views: 876
Reputation: 54551
Each of your pointers points to a separate and independent block of memory. Inside your array of pointers, each element is simply an address and overwriting one won't affect the others. So, what you are doing is fine and won't cause any problems with other elements of the array. As others mentioned, you should be passing wordList[3]
and not &wordList[3]
Upvotes: 0
Reputation: 123
malloc allocates a trunk of memory from heap and that trunk of memory can't be allocated for other malloc until you free them. In other words, malloc succeeds only if there are enough continuous free space in the heap. So this makes sure that the memory allocated would not collide with any others in your words.
Upvotes: 0
Reputation: 126787
Why should it be colliding? You have declared an array of pointers, each of which points to memory that is allocated elsewhere. When you reallocate you are just changing the size/position of this memory, the pointer returned by realloc
is as big as it was.
By the way, you shouldn't be using realloc
that way, since, if it fails, you'd be leaking memory; see e.g. here.
---edit---
And, as @asaelr noted, you should remove that &
, just realloc
ing the block pointed by wordList[3]
, not the memory of wordList
.
Upvotes: 1
Reputation: 59811
You have a misunderstanding about what realloc
does. It will return a whole new block of memory (if the new size is larger than the old size) instead of increasing the size of the block that was passed into it.
Upvotes: 0
Reputation: 5456
Just remove the &
. wordList[3] = (char*) realloc(wordList[3], someBiggerSize);
wordList[3]
is a pointer, and realloc expected to get a pointer that allocated by malloc
. not pointer to it.
About your last question: every time you call malloc
, it return a consistent block of memory. there is not guarantee that memory, allocated by two calls for malloc
, will be consistent. In other words, wordList[3]
and wordList[4]
are not must be consistent, and you can do whatever you want two one of them (as long as you care about the buffers size) without thinking about the other.
Upvotes: 1
Reputation: 612964
There's nothing to worry about this in principle. You just have an array of pointers and each element of the array points to a distinct memory block. Each element of the array, each pointer, can be therefore be reallocated independent of the other elements.
Now, I say in principle because your code does have an error. You should pass wordList[3]
rather than &wordList[3]
to the realloc
.
Upvotes: 1