Reputation: 7
I am trying to increase the amount of memory I have allocated using malloc. Is there a way to increase the size from 128 bytes to 256 bytes by allocating an additional 128 bytes next to the original block so I can access it as a continuous block of memory?
I am using g++.exe (MinGW.org GCC-6.3.0-1) 6.3.0. Should I use vectors or just get a new allocation and copy over the data with memcpy if the above is not possible?
Vector seems to work and other way is just copying over the to new location. For vector I don't much about how they are implemented but do sometimes they have certain optimization for doing allocating the memory next to the orginal block so copying is not required.
Upvotes: -2
Views: 158
Reputation: 63
For your scenario C library has ‘realloc’, which tries to resize the memory block to the new size(like in your case it will try to allocate contiguous bytes after 128 bytes to make is 256 bytes block) and will preserve the content of the block as well. If it fails to allocate the block in-place, it will allocate a new memory block of requested size(in your case 256 bytes) and copy the content and delete the previous block.
int* block = (int*)malloc(128);
if (!block) {
std::cout << "Allocation failed"<<std::endl;
return;
}
int* newBlock = (int*)realloc(block, 256);
if (!newBlock) {
std::cout << "Reallocation failed"<<std:endl
free(block);
return;
}
As mentioned above post, VirtualAlloc is a Window API function which allocates a region of memory in virtual address space of the process. It can be used as:
void* newBlock = VirtualAlloc(block, 256, MEM_COMMIT, PAGE_READWRITE);
VirtualAlloc being window specific, it can't be used on other operating systems.
Upvotes: 0
Reputation: 11178
One possible solution also since you are in Windows is to use VirtualAlloc with MEM_RESERVE for the entire memory and then use with MEM_COMMIT to actually allocate in steps, while leaving available address space for reallocation without memory moving.
Upvotes: 0
Reputation: 67835
C
I am trying to increase the amount of memory I have allocated using malloc.
Use realloc
which will do all the hard work for you. There is no other way.
For vector I don't much about how they are implemented but do sometimes they have certain optimization for doing allocating the memory next to the orginal block so copying is not required.
Often implementations allocate more memory than required to avoid reallocations. But if this memory has been used it has to be allocated/reallocated (depending on implementation). Vectors (C++) are not arrays and they do not have to be a continuous chunk of memory.
Upvotes: 1
Reputation: 386541
To extend a block allocated using malloc
, one uses realloc
.
realloc
usually extends the block if possible, but it may allocate a new block, copy the data over and deleted the old block. This is unavoidable. Since you never reserved (allocated) the space after the original block, there's no guarantee it's available to extend into.
The only way to guarantee you can extend an 128 byte block into a 256 one is to allocated 256 bytes in the first place.
Upvotes: 3