realloc

Passing a dynamic array in to functions in C

I'm trying to create a function which takes an array as an argument, adds values to it (increasing its size if necessary) and returns the count of items. So far I have: int main(int argc, char** argv) { int mSize = 10; ent a[mSize]; int n; n = addValues(a,mSize); for(i=0;i<n;i++) { //Print values from a }...

Simple C code, with vexing "incompatible types in assignment" error

Just a simple program to get used to pointers. The program is supposed to put the contents of a portion of my memory into a character array in reverse order of how the memory is read. I.E. looking at descending memory address, and I want to store it in descending order in a character array. My compiler keeps telling me: "error incompa...

Question on using realloc implementation in C++ code

Friends In our C++ , Iam current using realloc method to resize the memory allocated by malloc. realloc() usage is done as below my_Struct *strPtr =(my_struct*)malloc(sizeof(my_Struct)); /* an later */ strPtr = (my_struct*)realloc(strPtr,sizeof(my_Struct)*NBR); now wikipeadia (_http://en.wikipedia.org/wiki/Malloc)says that If in...

Is STL vector a better version of realloc ?

In C++, I believe, a better way of dealing with reallocation is to use a STL vectors, as it guarantees the contiguous storage locations. I have couple question to understand the difference: Is there any scenario in which I need to prefer realloc over vector ? Is there anything else ( apart from vector ) which is equivalent to realloc...

resizing buffer using realloc

If the area pointed to was moved, a free(ptr) is done. Can you please explain the above line about realloc()? This line is from a man page for calloc, malloc, realloc and free. ...

realloc crashing in previously stable function

Apparently this function in SDL_Mixer keeps dying, and I'm not sure why. Does anyone have any ideas? According to visual studio, the crash is caused by Windows triggering a breakpoint somewhere in the realloc() line. The code in question is from the SVN version of SDL_Mixer specifically, if that makes a difference. static void add_musi...

Problem with realloc of **struct

Hi Mates, I have a problem with realloc function in C. I'm passing you a code bellow: typedef struct _Pool Pool; typedef struct _Item Item; struct _Pool { Item ** items; unsigned int itemsCount; unsigned int poolSize; unsigned int poolStep; }; struct _Item { char * file; unsigned int lenOfFilePath; unsign...

Realloc Problem

Thank you for looking, please ignore - all sorts of shenanigans are happening and I am trying to debug more. ===================================== Can anyone explain this behavior of realloc? Output: before realloc start: testing%20encryp before realloc app: ' ' realloc size: 27 after realloc: testing%20e strlen(newstr): 11 ...

Best way to implement a dynamic stack type? or Am i abusing realloc?

I'm using an obscure language that doesn't have a native stack type so I've implemented my own. Now, reading on the net i've found a few different approaches to do this. This is my implementation (psuedo code) //push method function Push(int) { Increase (realloc) stack by 4 bytes; Push int into the new memory area; } //pop met...

in-place realloc with gcc/linux

Is there such a thing? I mean some function that would reallocate memory without moving it if possible or do nothing if not possible. In Visual C there is _expand which does what I want. Does anybody know about equivalents for other platforms, gcc/linux in particular? I'm mostly interested in shrinking memory in-place when possible (and...

Realloc implementation

I'm writing a simple linked list based memory manager in the form: ...Header|Block|Header|Block... with a used and free list. If the realloc() function was asked to reduce the size of a block, is it okay to overwrite some of the trailing bytes with the header for the newly created block? The documentation I've read suggests this is 'un...

Does realloc free the former buffer if it fails?

If realloc fails and returns NULL is the former buffer free'd or it is kept intact? I didn't found that particular piece of information in the man page and I'm quite unsure what to do. If memory is freed then double-free could be risky. If not then the leakage would occur. ...

can realloc move pointer if new size smaller?

I am wondering whether the C or C++ standard guarantees that a pointer is not changed when realloc is called with a smaller (nonzero) size: size_t n=1000; T*ptr=(T*)malloc(n*sizeof(T)); //<--do something useful (that won't touch/reallocate ptr of course) size_t n2=100;//or any value in [1,n-1] T*ptr2=(T*)realloc(ptr,n2*sizeof(T)); //<--...

Why Win32 HeapReAlloc() changes values?

Hi there! I'm writing an app in C using win32 API. When I try to enlarge the size of my array, using the HeapRealloc() function, it changes my current values in the array, instead of copying them. The code I use to reallocate memory: BOOL ChangeFeedArraySize(UINT newSize) { char tempChar[20] = ""; PFEED tempArr; if (newSi...

How to handle realloc when it fails due to memory?

Question says it all but here is an example: typedef struct mutable_t{ int count, max; void **data; } mutable_t; void pushMutable(mutable_t *m, void *object) { if(m->count == m->max){ m->max *= 2; m->data = realloc(m->data, m->max * sizeof(void*)); } // how to handle oom?? m->data[m->count++] = ...

C stdlib realloc issue

The realloc reference says: The function may move the memory block to a new location, in which case the new location is returned. Does it mean that if I do this: void foo() { void* ptr = malloc( 1024 ); unsigned char* cptr = ( unsigned char* )ptr+256; ptr = realloc( ptr, 4096 ); } then cptr may bec...

Freeing a character pointer

I have a function which is called multiple times during the program's execution. In said function, I have a dynamic character pointer which I resize numerous times. My question is: do I need to free this pointer before the end of the function? void functionName() { char *variable = (char *) malloc(0); //variable is resized with re...

gdb gives an error, but program runs fine

I have a simple C program which has a pointer to a character array. To initiate it, I use malloc, and resize then set it x number of times later on in the program. When I resize it once with realloc, gdb doesn't show any errors, however, if I try calling the resize function again, gdb shows the following error: warning: Invalid Address...

What header should I include for memcpy & realloc in a C++ iPhone program?

Hello, I am porting a project to the iPhone and it uses realloc and memcpy which are not found. What is the header to include? it's a projet mixing objc and c++ and I am starting to be lost. Thanks in advance for your help! ...

Should I enforce realloc check if the new block size is smaller than the initial?

Can realloc fail in this case? int *a = NULL; a = calloc(100, sizeof(*a)); printf("1.ptr: %d\n", a); a = realloc(a, 50 * sizeof(*a)); printf("2.ptr: %d\n", a); if(a == NULL){ printf("Is it possible?\n"); } return (0); } The output in my case is: 1.ptr: 4072560 2.ptr: 4072560 So 'a' points to the same adress. So should I en...