Questions tagged [realloc]

C library function for reallocating a dynamically allocated memory region.

man page

Wikipedia

References

Related Tags

1717 questions
4
votes
3 answers

Are multiple realloc more expensive than a huge malloc?

I am using a dynamic array to represent a min-heap. There is a loop that removes minimum, and add random elements to the min-heap until some condition occur. Although I don't know how the length of the heap will change during run-time (there is a…
Legendre
  • 3,108
  • 7
  • 31
  • 46
4
votes
2 answers

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,…
lukaszcz
4
votes
2 answers

pointer being realloc'd was not allocated

#include #include "stdlib.h" int main(int argc, const char * argv[]) { int i,j; char takimlar[4][8]= { "TAKIM A ","TAKIM B","TAKIM C","TAKIM D"}; char *var = malloc(sizeof(char)*1); int sonuclar[4][4] = { …
saidozcan
  • 2,115
  • 9
  • 27
  • 39
4
votes
3 answers

Implementation of Realloc in C

int getmin(int a, int b) { return a
Luv
  • 5,381
  • 9
  • 48
  • 61
4
votes
5 answers

Resizing a char* on the fly - why does this code *work*?

I'm experimenting with malloc & realloc and came up with some code for the following problem: I want to create a string of unknown size, without setting any limit. I could ask the user for a nr of chars, but I rather resize the str as the user …
MyName
  • 2,136
  • 5
  • 26
  • 37
4
votes
5 answers

How to detect if memory is dynamic or static, from a callee perspective?

Note: when I say "static string" here I mean memory that can not be handled by realloc. Hi, I have written a procedure that takes a char * argument and I would like to create a duplicate IF the memory is not relocatable/resizable via realloc. As is,…
user735796
3
votes
3 answers

Reallocating memory of a C++ array.

Possible Duplicate: How do you realloc in C++? I know that C++ arrays can be reallocated (expanded) using realloc() if memory has been allocated via malloc() or calloc(). My question is, how can I expand an array in C++ whose memory has been…
Amal Antony
  • 6,477
  • 14
  • 53
  • 76
3
votes
2 answers

Realloc x86 x86_64

I have a function void *srealloc(void * ptr , int size){ void *tmp = realloc(ptr , size); if(tmp == NULL){ fprintf(stderr,"realloc of %u bytes failed", size); exit(1); } return tmp; } My code that calls this runs…
LeeG
  • 265
  • 1
  • 4
  • 11
3
votes
3 answers

Dynamic Stack Memory Reallocation

I'm fairly new to C++ and new to pointers as well. I'm currently working on a stack and was trying to reallocate the memory for the stack as the size of the stack reaches the top however, I'm running into issues. I've already done a lot of research…
Paul
  • 429
  • 1
  • 5
  • 15
3
votes
2 answers

How does realloc treat null bytes in strings?

Relatively new C programmer here. I am reviewing the following code for a tutorial for a side project I am working on to practice C. The point of the abuf struct is to create a string that can be appended to. Here is the code: #include…
3
votes
1 answer

realloc() C-language change value in int array

I'm trying to use realloc() every loop, so I only use necessary memory for my int array in C, but the output values are changed. Nevertheless, when using Valgrind on my code, I have the right values. I'm doing the first day of Advent of Code…
aisezc
  • 33
  • 5
3
votes
1 answer

If realloc returns NULL do I have to free old memory?

So I know that realloc() can follow 2 ways. The first way is allocating memory in a new address and freeing old memory. Second way is allocating memory in the same address so that you will only need to free that address. So in both ways, you only…
Elrisas
  • 49
  • 5
3
votes
4 answers

realloc a struct, c

I have the next struct struct Board { int width; int height; char **board; } And I would like to expand the **board, meaning I need more memory and thus the call to realloc(). So my question is how do I do that - should I call realloc()…
yotamoo
  • 5,334
  • 13
  • 49
  • 61
3
votes
1 answer

Double an array dynamically

I am trying to solve the following exercise in C: "Consider a function dobuleArray() that takes in input an array and a pointer to its size and gives as output the array doubled. Create an array of size 1 to 4 as initial size. Then, start filling…
PwNzDust
  • 271
  • 2
  • 9
3
votes
2 answers

Declare string in C without giving size

I want to concatenate in a string multiple sentences. At the moment my buffer is with fixed size 100, but I do not know the total count of the sentences to concatenate and this size can be not enough in the future. How can I define a string without…
Enchantres
  • 853
  • 2
  • 9
  • 22