realloc

What happens if I re-alloc and the new size is 0. Is this equivalent with a free ?

Given the following code: int *a = NULL; a = calloc(1, sizeof(*a)); printf("%d\n", a); a = realloc(a, 0); printf("%d\n", a); return (0); It returns: 4078904 0 Is this realloc equivalent to a free ? NOTE: I am using MinGW under WindowsXP. ...

realloc()ing memory for a buffer used in recv()

I need to recv() data from a socket and store it into a buffer, but I need to make sure get all of the data so I have things in a loop. So to makes sure I don't run out of room in my buffer, I'm trying to use realloc to resize the memory allocated to the buffer. So far I have: // receive response int i = 0; int amntRecvd = 0; char *...

realloc returns NULL after some time while allocating small (<500Kb) data block; there is enoug memory

Hi! The short question is: what can be the problem? The overall memory usage of my program (shown by task manager) is almost the same all the time (near 40 minutes) it's running, and I have near 2G more free memory. Running on win2003r2. Memory allocation/freeing is high enough - I need to interact with other software, preparing dat...

C++ casted realloc causing memory leak

I'm using a function I found here to save a webpage to memory with cURL: struct WebpageData { char *pageData; size_t size; }; size_t storePage(void *input, size_t size, size_t nmemb, void *output) { size_t realsize = size * nmemb; struct WebpageData *page = (struct WebpageData *)output; page->pageData = (char *)re...

callin' c from lua crashs while reallocating

hi folks, i got a crazy error within that for-loop matr=realloc(matr, newmax*sizeof(matr*)); for (i=0; i<newmax; i++){ matr[i]=realloc(matr[i], newmax*sizeof(int)); } matr is a multi-dimension array: int **matr. i need to resize column and row. first line resizes column and the for-loop resizes every row. it worked fine in c. n...

Strange realloc behaviour

Hi guys, i'm developing an array structure just for fun. This structure, generalized by a template parameter, pre allocates a given number of items at startup, then, if "busy" items are more than available ones, a function will realloc the inner buffer . The testing code is : #include <stdio.h> #include <stdlib.h> #include <string.h> t...

How to create extensible dynamic array in Java without using pre-made classes?

Yeah, it's a homework question, so givemetehkodezplsthx! :) Anyway, here's what I need to do: I need to have a class which will have among its attributes array of objects of another class. The proper way to do this in my opinion would be to use something like LinkedList, Vector or similar. Unfortunately, last time I did that, I got fire...

realloc - converting int to char

I'm converting an array of integers into a char by iterating through the whole array, and then I'm adding the resulting string to ncurses's method new_item. For some reason I'm doing something wrong the way I reallocate memory, thus I get the the first column as: -4 Choice 1 0 Choice 1 4 Choice 2 1 Choic...

realloc(): invalid next size

I'm having a problem with the realloc function. I'm using C only (so no vector) with LibCurl. The problem I'm having is that I'm getting the following error (realloc(): invalid next size) on the 12th iteration of the write_data function (the function I pass to Curl as a callback, it is called each time libcurl has some data to pass back ...

multiple calls to realloc() seems to cause a heap corruption..

What's the problem with this code? It crashes every time. One time it's a failed assertion "_ASSERTE(_CrtIsValidHeapPointer(pUserData));", other times it is just a "heap corrpuption" error. Changing the buffer size affects this issue in some strange ways - sometimes it crashes on the "realloc", and other times on the "free". I have de...

Why is there no reallocation functionality in C++ allocators?

In C the standard memory handling functions are malloc(), realloc() and free(). However, C++ stdlib allocators only parallel two of them: there is no reallocation function. Of course, it would not be possible to do exactly the same as realloc(), because simply copying memory is not appropriate for non-aggregate types. But would there ...

Problem with realloc() in C. Always hangs but compiles fine...

Hi there, I'm having some trouble with a program that is intended to be a String buffer, specifically this function is intended to reset the buffer with the string cstr. If cstr is null then the content needs to be reset to an empty char '\0'. It always hangs at the second set of realloc where it's resizing buf->contents I have no clue w...

Efficient memory reallocation question

Let's say I have a program(C++, for example) that allocates multiple objects, never bigger than a given size(let's call it MAX_OBJECT_SIZE). I also have a region(I'll call it a "page") on the heap(allocated with, say, malloc(REGION_SIZE), where REGION_SIZE >= MAX_OBJECT_SIZE). I keep reserving space in that page until the filled space e...

Reallocate a 2d char array

I have following code int wordLenght = 256, arrayLength = 2, i = 0, counter = 0; char **stringArray = NULL; stringArray = calloc(arrayLength, sizeof(*stringArray)); for(counter; counter<wordLenght; counter++) stringArray[counter] = calloc(wordLenght, sizeof(stringArray)); while(1) { printf("Input: "); fgets(stringAr...

realloc and free causes "double free or corruption"

Bear with me. I have not coded in c in 8 years and am totally baffled why my string manipulation is not working. I am writing a program that loops forever. In the loop I initialize two char pointers each is passed to a function that add text to the char pointer (array). When the functions are done I print the char pointer and free the tw...

c realloc struct - g_hash_table

I'm doing something similar to the following code. I have already gone through AddtoStructFunction() filling mystruct once. Now, what I would like to do is to append every new entry directly to the mystruct without having to free mystruct and iterate over again the whole g_hash_table containing the new key(s) to insert them into mystruct...

How can I make sure that a caller passes a malloc'd pointer?

I have a function which reallocs a pointer given as an argument to a new size. Now, the problem is that - according to the man page - realloc needs a pointer which has been returned by malloc or calloc before. How can I make sure that the caller passes a pointer that meets those requirements? There seem to be no build-in C mechanics (li...

What is the correct usage of realloc()?

Can anyone summarize what is the correct usage of realloc()? What do you do when realloc() fails? From what I have seen so far, it seems that if realloc() fails, you have to free() old pointer. Is that true? Here is an example: 1. char *ptr = malloc(sizeof(*ptr) * 50); 2. ... 3. char *new_ptr = realloc(ptr, sizeof(*new_pt...

How does realloc know how much to copy?

Hey, how does realloc know the size of original data? void *realloc(void *ptr, size_t size); So, if the implementation is like this: temp = malloc(size); memcpy(.. // How much to copy? free(ptr); return temp; I realize this is not the original implementation, and realloc doesn't always do free, but when it does, how much does...

How do you `realloc` in C++?

Any ideas how to realloc in C++? It seems to be the missing language language - there is new and delete but not resize! I need it because as my program reads more data, I need to reallocate the buffer to hold it. I don't think deleteing the old pointer and newing a new, bigger one, is the right option. Please enlighten me. Thanks, B...