realloc

What if NULL and size 0 are passed to realloc()?

依然范特西╮ 提交于 2021-02-19 01:18:51
问题 Is the behavior implementation defined? If NULL and size == 0 are passed to realloc() : int main(void) { int *ptr = NULL; ptr = realloc(ptr, 0); if(ptr == NULL) { printf("realloc fails.\n"); goto Exit; } printf("Happy Scenario.\n"); Exit: printf("Inside goto.\n"); return 0; } The above code should print "realloc fails", right? But it is not? I've read somewhere that this call to realloc may return NULL also. When does that happen? 回答1: This behavior is implementation defined. From the C

delete partial memory of the pointer

爱⌒轻易说出口 提交于 2021-02-05 11:58:48
问题 Is there any way i can delete the partial memory of the pointer.? for example char *c = new char[1000]; sprintf(c,"this is it"); As it can be seen a lot of memory is getting wasted here. can I free the memory more than the required.? 回答1: Not directly. The best you can do in C++ is to make a new copy that's the right size and delete the old one. There's no analog of C's realloc . 回答2: Unless your system is a RAM-restricted embedded system, why bother? Just use ginormous buffers and include a

realloc without freeing old memory

耗尽温柔 提交于 2021-02-04 13:37:25
问题 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 and if it is not possible, fallback to a different method using the original pointer - but realloc has already destroyed that! Is there a way to try to increase malloc'ed memory without destroying (as realloc does) the old pointer if it is not

Extending and shrinking array using realloc

怎甘沉沦 提交于 2021-01-28 15:52:22
问题 I'm trying to write a program which first dynamically initialises a queue array for 100 int elements. Whenever the queue is full and another element is supposed to be queued, the original array is supposed to double it's size so new elements can be inserted. In case elements are dequeued, and the amount of elements the queue consists of falls below half of its actual size, the queue size is supposed to be cut in half. However, its size should never fall below 10. I'm trying to expand and

Extending and shrinking array using realloc

喜夏-厌秋 提交于 2021-01-28 15:50:55
问题 I'm trying to write a program which first dynamically initialises a queue array for 100 int elements. Whenever the queue is full and another element is supposed to be queued, the original array is supposed to double it's size so new elements can be inserted. In case elements are dequeued, and the amount of elements the queue consists of falls below half of its actual size, the queue size is supposed to be cut in half. However, its size should never fall below 10. I'm trying to expand and

Extending and shrinking array using realloc

被刻印的时光 ゝ 提交于 2021-01-28 15:42:29
问题 I'm trying to write a program which first dynamically initialises a queue array for 100 int elements. Whenever the queue is full and another element is supposed to be queued, the original array is supposed to double it's size so new elements can be inserted. In case elements are dequeued, and the amount of elements the queue consists of falls below half of its actual size, the queue size is supposed to be cut in half. However, its size should never fall below 10. I'm trying to expand and

Extending and shrinking array using realloc

岁酱吖の 提交于 2021-01-28 15:40:09
问题 I'm trying to write a program which first dynamically initialises a queue array for 100 int elements. Whenever the queue is full and another element is supposed to be queued, the original array is supposed to double it's size so new elements can be inserted. In case elements are dequeued, and the amount of elements the queue consists of falls below half of its actual size, the queue size is supposed to be cut in half. However, its size should never fall below 10. I'm trying to expand and

realloc vs malloc in C

不羁的心 提交于 2021-01-28 14:23:06
问题 I'm fairly new to C, and just now starting to venture into the realm of dynamically allocated arrays. I think i've got mostly malloc down, but had some questions on realloc : Can realloc be used for anything else besides adding memory space to pointers? Does the size variable always have to be an int ? Would something like the below work? float *L = NULL; int task_count = 5; L = (float*) realloc (L, task_count * sizeof(float)); If I wanted to increase that space further (by one in this case),

what will realloc do to the old pointer [duplicate]

不问归期 提交于 2020-08-23 04:56:50
问题 This question already has answers here : Using realloc to shrink the allocated memory (5 answers) Closed last month . I have a question about the realloc function. Will the content of old pointer be changed after apply realloc function? The code is main () { int *a, *b, i; a = calloc(5, sizeof(int)); for (i = 0; i < 5; i++) a[i] = 1; for (i = 0; i < 5; i++) printf("%d", a[i]); printf("\n%p\n", a); b = realloc(a, 200000 * sizeof(int)); if(b == NULL) printf("error\n"); for (i = 0; i < 5; i++)

Is it safe to assume that realloc()ing to a smaller size will always succeed? [duplicate]

非 Y 不嫁゛ 提交于 2020-07-09 17:10:18
问题 This question already has answers here : Can realloc fail (return NULL) when trimming? (7 answers) can realloc move pointer if new size smaller? (6 answers) Closed yesterday . There is no reason for realloc() ing to a smaller size will fail. It's freeing up the remainder. I see no reason at all for it to fail. This being said, is it safe to assume that realloc() ing to a smaller size will never fail? 回答1: TL;DR No, you cannot assume that. There is no reason for realloc()ing to a smaller size