Questions tagged [realloc]

C library function for reallocating a dynamically allocated memory region.

man page

Wikipedia

References

Related Tags

1717 questions
9
votes
5 answers

realloc() invalid old size

I am doing an exercise for fun from KandR C programming book. The program is for finding the longest line from a set of lines entered by the user and then prints it. Here is what I have written (partially, some part is taken from the book…
user720694
  • 2,035
  • 6
  • 35
  • 57
9
votes
1 answer

Can std::vector avoid copying if allocator provides realloc semantics?

I have a memory interface that separates out acquiring address space from attaching backing store. (Under Linux the pool of address space managed by the interface is mmap'ed MAP_ANONYMOUS and MAP_NORESERVE, madvise'ed MADV_DONTNEED and mprotect'ed…
John Yates
  • 1,027
  • 1
  • 7
  • 18
9
votes
4 answers

Is it fair to always recommend std::vector over realloc?

From Bjarne Stroustrup's FAQ: If you feel the need for realloc() - and many do - then consider using a standard library vector. I'll preface my question by agreeing that std::vector is better for many reasons, and I personally would always…
Ben Hymers
  • 25,586
  • 16
  • 59
  • 84
8
votes
5 answers

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…
aJ.
  • 34,624
  • 22
  • 86
  • 128
8
votes
3 answers

Why is there no function in standard C library like realloc() without data copying?

For example, I want such a function: char *dst = (char*)malloc(512); char *src = (char*)malloc(1024); ... dst = (char*)realloc(dst, 1024); memcpy(dst, src, 1024); As you see, I just want the function realloc() to extend the size of buffer, but the…
legendlee
  • 568
  • 4
  • 12
8
votes
2 answers

Can the storage of trivially copyable objects be safely reallocated with realloc?

I know that trivially copyable objects can safely be copied my malloc into an appropriate storage location1 and that the destination object will have the same value as the source. Is this also possible with realloc? That is, if realloc some storage…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
8
votes
2 answers

How do I allocate more space for my array of C structs?

I'm trying to add 10 more elements to my struct that has been already malloc with a fixed sized of 20. This is the way I have my struct defined: #include #include #include struct st_temp { char *prod; }; int main…
Josh
  • 83
  • 1
  • 1
  • 4
8
votes
2 answers

Dynamic arrays: using realloc() without memory leaks

I use realloc to resize the memory allocated: char **get_channel_name(void) { char **result; int n; result = (char **) 0; for (elem = snd_mixer_first_elem(handle), n = 0; elem; elem = snd_mixer_elem_next(elem)) { if…
Cecylia
  • 349
  • 4
  • 12
8
votes
4 answers

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.
Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118
8
votes
3 answers

How to properly reallocate a two-dimensional array in C?

I am trying to load two double numbers from input into a two-dimensional array that is dynamically reallocated by each user input. #include #include int main(int argc, char** argv) { int count; double number1,…
Pastx
  • 687
  • 3
  • 8
  • 23
8
votes
7 answers

realloc without freeing old memory

I want to use realloc to increase memory size while keeping the pointer unchanged (because the callers uses it). realloc does not always do that; sometimes it returns a different pointer and frees the old one. I would like to "try" to realloc memory…
queen3
  • 15,333
  • 8
  • 64
  • 119
8
votes
3 answers

realloc but only first few bytes is meaningful

Assume I have used ptr = malloc(old_size); to allocate a memory block with old_size bytes. Only the first header_size bytes is meaningful. I'm going to increase the size to new_size. new_size is greater than old_size and old_size is greater than…
lqs
  • 1,434
  • 11
  • 20
7
votes
4 answers

If destination and source are the same, what does memmove do?

If destination and source are the same, does memmove still "move" the data (or does it return directly)? What about realloc; what if the new size is the same as the old size?
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
7
votes
4 answers

Does memcpy() uses realloc()?

#inlcude #inlcude #inlcude int main() { char *buff = (char*)malloc(sizeof(char) * 5); char *str = "abcdefghijklmnopqrstuvwxyz"; memcpy (buff, str, strlen(str)); while(*buff) { printf("%c" ,…
shan
  • 1,164
  • 4
  • 14
  • 30
7
votes
3 answers

Realloc Vs Linked List Scanning

I have to read from a file an unknown number of rows and save them in to a structure (I would like to avoid a prepocessing to count the total number of elements). After the reading phase I have to make some computations on each of the elements of…
Beppe
  • 381
  • 2
  • 7
  • 15