Questions tagged [realloc]

C library function for reallocating a dynamically allocated memory region.

man page

Wikipedia

References

Related Tags

1717 questions
4
votes
1 answer

Using realloc in clang to shrink memory

I've run into an issue with our code under clang involving realloc. This code works fine under gcc and visual studio, so I'm interested in understanding clang's behavior. Our code does something similar to this (error handling etc elided): //…
Colen
  • 13,428
  • 21
  • 78
  • 107
4
votes
1 answer

Points returned by memory allocation functions

I am learning C from the book by K.N.King and am going through the programming projects for chapter 17. I came across a self inflicted bug while I was completing the first project. It was undefined behaviour of the program since I was passing…
Zeta-Squared
  • 257
  • 2
  • 7
4
votes
1 answer

What is the point of using a temporary realloc variable, if the program will exit?

Many people recommend doing the following to reallocate memory: int *temp=realloc(previousVar,newSize); if(temp==NULL){ printf("error\n"); exit(-1); } previousVar=temp; I understand that, if no new variable was created, the previous pointer would…
user12184817
4
votes
2 answers

Realloc an array of Structs

I am trying to dynamically reallocate memory for an array of structs (actually an array each of 2 structs but 1 included here for simplicity) that is being read from/to a file or inputted by the user. typedef Struct { char surname[21]; char…
thelionroars1337
  • 188
  • 1
  • 3
  • 10
4
votes
2 answers

Why does `realloc` not re-allocate in-place when possible?

From c99: The realloc function deallocates the old object pointed to by ptr and returns a pointer to a new object that has the size specified by size. The contents of the new object shall be the same as that of the old object prior to deallocation,…
Remi.b
  • 17,389
  • 28
  • 87
  • 168
4
votes
2 answers

Behavior of `realloc()` when the memory is shrunk

The man page of realloc() says: The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the…
S. Sharma
  • 203
  • 1
  • 11
4
votes
3 answers

how can I append a char to a string allocating memory dynamically in C?

I wrote this code, but inserts garbage in the start of string: void append(char *s, char c) { int len = strlen(s); s[len] = c; s[len + 1] = '\0'; } int main(void) { char c, *s; int i = 0; s = malloc(sizeof(char)); while…
Glicério
  • 75
  • 1
  • 8
4
votes
2 answers

Can we safely say that the result of realloc does not alias with the original pointer?

Say we have: char *a = malloc(sizeof(char*)); char *b = realloc(a,sizeof(char*)); Can we safely say that b does not alias with a? The realloc reference page says that The original pointer ptr is invalidated and any access to it is undefined…
MIA
  • 130
  • 7
4
votes
1 answer

realloc crashes when pointer has an offset

I have below a simplified C program that demonstrates the problem. When I try to call realloc using a pointer, it works fine, but if I try to add an offset to the pointer(i.e. to start from a later element in an array), it fails. # include…
user2649681
  • 750
  • 1
  • 6
  • 23
4
votes
4 answers

Confusion about realloc function

I read about dynamic memory allocation in C using this reference. That document Say's : realloc() should only be used for dynamically allocated memory. If the memory is not dynamically allocated, then behavior is undefined. If we use realloc()…
Jayesh
  • 4,755
  • 9
  • 32
  • 62
4
votes
1 answer

What are the advantages and disadvantage of using jemalloc vs malloc vs calloc and other common alternatives?

Reading the Rust subreddit today I came across comments that: jemalloc is optimized for (multithreaded) speed, not memory usage After doing more research I found that there are even more alternatives (such as calloc). I would like to understand…
Greg
  • 8,175
  • 16
  • 72
  • 125
4
votes
2 answers

Can I realloc from a position of the array?

Say I declared an array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. Later I want it to be array[8] = {2, 3, 4, 5, 6, 7, 8, 9}. Dismiss the first 2 elements. So it would start on array[2]. Reallocing array to array[2]. I tried: int…
user7308845
4
votes
1 answer

glibc detected, realloc(): invalid pointer

I apologize for the lengthy code. I have a simple question, but I thought I include my code so it will be clear where I am coming from. I get a realloc corruption. I think the corruption is because I am not freeing correctly. In reality I am not…
Paul Kar.
  • 1,293
  • 2
  • 21
  • 32
4
votes
2 answers

File to dynamic array in c

DISCLAIMER: I'm new to C. What is the best way to convert every line in a .txt file (can be other file types too) to a dinamic calloc() array, and the other way around? In my file I have to following: 1 Hello 2 18 3 World 4 15 etc... I want…
user5805283
4
votes
3 answers

Realloc returning NULL

int main() { struct lottery *array; array = (struct lottery *)malloc(3000 * sizeof(struct lottery)); int opt, counter; menu1(); scanf("%d", &opt); if (opt == 1) Load(array, &counter); else …
Edward
  • 47
  • 1
  • 9