Questions tagged [realloc]

C library function for reallocating a dynamically allocated memory region.

man page

Wikipedia

References

Related Tags

1717 questions
0
votes
1 answer

Unintended behaviour after realloc() with dynamic array

I have an assignment where I want to implement a dynamically growing array, but I seem to be having some issues with realloc(). My code works as long as I do not actually get to the realloc() part, where for some reason only some specific values are…
filpa
  • 3,651
  • 8
  • 52
  • 91
0
votes
1 answer

Freeing realloc causing error

I am trying to free my memory allocated in the following program: include #include void main(int argc, char** argv) { int* x = (int *) malloc(4 * sizeof(int)); int i; for(i = 0; i < 4; i++) { x[i] = i; …
jg943
  • 309
  • 3
  • 21
0
votes
3 answers

realloc() in c.. printing junk value

i have uploaded all of the code.. this is work in progress.. please check for realloc() because if i dont reach the condition for realloc() everything works fine...Thanks evry1.. // contactinfo.h-- header file #ifndef _ELEMENT_H #define…
KungFu_Panda
  • 111
  • 11
0
votes
1 answer

realloc() on array of function ptrs leads to SIGABRT

On my current project, I do some function-ptr collecting before running the main part of the program. Some code: typedef void(*ShutdownFunctionPtr)(void); static ShutdownFunctionPtr *shutdown_functions; static unsigned int …
musicmatze
  • 4,124
  • 7
  • 33
  • 48
0
votes
3 answers

realloc and the ghosts of mallocs past

I know that realloc will free memory when necessary, and I know the third rule of C - "for every malloc there must be an equal and opposite free"... but how do these two work together? The situation is best described in code: int main() { …
0
votes
1 answer

realloc a triple pointer

Hi I have a triple pointer that I want to realloc when my original array is filled up. For some reason, the way I'm using realloc gives me a seg fault. Anybody have a sense why? double ***matrixn; matrixn=(double***) calloc(1,sizeof(double…
JupiterOrange
  • 339
  • 2
  • 6
  • 18
0
votes
3 answers

realloc not working for 2-D pointer array

In the following code, I ask the user to give me some strings. I make a 2-D pointer char array, so that I read the input with pointer string which points to the start of a string of length 50. My problem is that I keep crashing after the input of…
alex777
  • 167
  • 5
  • 14
0
votes
1 answer

adding an element to an array using realloc

I'm trying to use realloc to add an element to an array after inputting characters. Here is my code: #include #include int main(void) { int i, j, k; int a = 1; int* array = (int*)…
user1680212
0
votes
4 answers

Better method to increase the size of an array in C++ dynamically

Suppose I have a character array allocated with new.Now, if I want to modify the size of the array then which of the following two is the best method ? 1. using realloc function OR 2.Allocate a new array, copy the data from old array to the new…
jairaj
  • 1,789
  • 5
  • 19
  • 32
0
votes
1 answer

C - sYSMALLOc: Assertion failed (realloc)

I am trying to write a function that searches for all occurrences of a pattern and returns an array of offsets in the file that match the pattern. I want to use realloc to dynamically grow my returned array, but I am getting a sYSMALLOC Assertion…
raidzero
  • 299
  • 1
  • 4
  • 13
0
votes
1 answer

(C) realloc array modifies data _pointed_ by items

(C) realloc array modifies data pointed by items Hello, A nice weird bug I feel like sharing ;-) Requires some preliminary explanations: First, I have a type of strings PString which hold their size (and a hash value), followed by a flexible array…
denis 63
  • 79
  • 7
0
votes
1 answer

Dynamic pointer array realloc has failed

I have problem with realloc. This is my function which reads words from output and is terminated if EOF is detected. The function makes memory leaks and the following program throws SIGSEGV or SIGABORT. What's a problem ? int inx=0; char…
user1890078
  • 207
  • 1
  • 2
  • 7
0
votes
1 answer

realloc() is screwing up my data... what's going on?

I'm writing a variable list implementation. In my insertion step, I check if the array's size is maxed out, and if so I double the maximum capacity and call realloc() to allocate me some new memory. Going from size 2 to 4, 4 to 8, and 8 to 16 works…
limp_chimp
  • 13,475
  • 17
  • 66
  • 105
0
votes
4 answers

Why won't realloc work in this example?

My professor gave us an "assignment" to find why realloc() won't work in this specific example. I tried searching this site and I think that it won't work because there is no real way to determine the size of a memory block allocated with malloc()…
0
votes
1 answer

Pointers getting junk values outside of functions, but regular values inside them

Full disclosure: This is my first time doing any significant programming in C, and my first post on Stack Overflow. I'm working on code that will eventually be used with Bison to implement a small subset of the Scheme/Racket language. All of this…
thevoiceless
  • 93
  • 3
  • 4