realloc

realloc: reversed pointer array?

Hi, I've written a rather simple(ish) stack implementation that would automatically grow its internal array buffer if needed. For that, I'd naturally use realloc - It works, however, all array elements are ordered reverse after the realloc() call. The code in question: Header: include/pd/stack.h Source: src/stack/stack.c This examp...

simple c malloc

While there are lots of different sophisticated implementations of malloc / free for C/C++, I'm looking for a really simple and (especially) small one that works on a fixed-size buffer and supports realloc. Thread-safety etc. are not needed and my objects are small and do not vary much in size. Is there any implementation that you could ...

Shrinking with realloc

I encountered this small piece of code in this question, & wanted to know, Can the realloc() function ever move a memory block to another location, when the memory space pointed to is shrinked? int * a = malloc( 10*sizeof(int) ); int * b = realloc( a, 5*sizeof(int) ); If possible, under what conditions, can I expect b to have an addr...

Can realloc shrink my array on the left side (C only)?

I want to move a large chunk of data i've got in my memory. Unfortunately this data is saved as an array, and i cannot change that. I can't use circular arrays, because the same memory is also used by a couple of fortran methods i do not want to change. On top of it, the arrays are accessed very very often in between the movement. So i c...

Does realloc overwrite old contents?

When we reallocate memory via realloc(), are the previous contents over-written? I am trying to make a program which reallocates memory each time we enter the data into it. Please tell me about memory allocation via realloc, is it compiler dependent for example? ...

C Realloc error - "Assertion `ptr == alloc_last_block' failed!"

I'm writing a function in C that takes in a linked list and a predicate and returns an array containing all values of the linked list satisfying this condition. Here's the function: void **get_all_that(list_t *l, int (*pred)(const void *)) { void **vals = NULL; int i = 0; // Number of matches found const size_t vps = sizeof(...

Replacing realloc (C --> C++)

In an earlier question, I asked about typecasting pointers, but was directed to the better solution of using the C++ allocation system instead of mallocs. (I am converting some C code to C++) However, I still have an issue with a similar function: I changed: tmp = malloc(sizeof(char*) * mtmp); --> tmp = new char*[mtmp]; and free(tm...

One large malloc versus multiple smaller reallocs

Hello. Sorry if this has been asked before, I haven't been able to find just what I am looking for. I am reading fields from a list and writing them to a block of memory. I could Walk the whole list, find the total needed size, do one malloc and THEN walk the list again and copy each field; Walk the whole list and realloc the block of...

How do I allocate more space for my array of C structs?

I'm trying to add 10 more elements to my struct that has been already malloc with a fixed sized of 20. This is the way I have my struct defined: #include <stdio.h> #include <stdlib.h> #include <string.h> struct st_temp { char *prod; }; int main () { struct st_temp **temp_struct; size_t j; temp_struct = malloc (sizeof *tem...