I am using following function to allocate memory:
int qmem_alloc(unsigned int num_bytes, void ** rslt){
void** temp;
if(rslt == NULL)
return -1;
temp = (void **)malloc(num_bytes);
if(temp == NULL)
return -2;
else
rslt = temp;
return 0;
}
And following function to reallocate memory:
int qmem_allocz(unsigned num_bytes, void ** rslt){
void** temp;
void *test = (void *)malloc(10);
if(rslt == NULL)
return -1;
temp = (void **)realloc(rslt, num_bytes);
printf("here");
if(temp == NULL)
return -2;
else
// free(rslt)
return 0;
}
Here is my main function:
struct qbuf { int idx; char data[256]; };
void main(){
struct qbuf * p = NULL;
printf("%d\n",qmem_alloc(sizeof(struct qbuf), (void **)&p));
printf("%d\n",qmem_allocz(100*sizeof(struct qbuf), (void **)&p));
}
The program can get memory allocated but it crashes when reallocation is done. Here is the error:
malloc.c:2868: mremap_chunk: Assertion `((size + offset) & (GLRO (dl_pagesize) - 1)) == 0' failed.
Why is this happening? How can i fix it?
*rlst = temp;before return in the alloc function.rlst = temp;assigns the pointer value to temp, you need to assign the value to which the pointer points to temp.(void **), the result ofmalloc().?void *test = (void *)malloc(10);? It allocates memory and then code forgets about it.