realloc

realloc invalid old size

六月ゝ 毕业季﹏ 提交于 2019-11-28 08:36:41
问题 Disclaimer: This is homework. I am attempting it and do not expect or want anyone to do it for me. Just a few pointers (hehe) where I'm going wrong would be appreciated. The homework requires me to create an int* array that holds 10 elements, and then attempt to insert a million ints into it. Each insertion checks if the array needs to be resized, and if it does, I increase it's size so it can hold one more element. When I insert 10,000 elements, it works fine, but if I try 100,000 elements,

Using realloc on a 2D array c

旧城冷巷雨未停 提交于 2019-11-28 06:43:38
问题 I'm kinda new to C sorry if my questions is somewhat vague; I need to use realloc on a 2D array without losing it's previous data, I have this function in my program to do it: void modifyMatrix(int **iMat, int iRow, int iRow2, int iCol) { int i; iMat = (int**)realloc(iMat, (iRow2)*sizeof(int*)); for(i=iRow; i<iRow2; i++) { iMat[i]=NULL; } for(i=0; i<iRow2; i++) { iMat[i]=(int*)realloc(iMat[i], (iCol)*sizeof(int)); } } Where iRow is the original size and iRow 2 & iCol are the new size and are

Using realloc to shrink the allocated memory

守給你的承諾、 提交于 2019-11-28 04:09:53
Simple question about the realloc function in C: If I use realloc to shrink the memory block that a pointer is pointing to, does the "extra" memory get freed? Or does it need to be freed manually somehow? For example, if I do int *myPointer = malloc(100*sizeof(int)); myPointer = realloc(myPointer,50*sizeof(int)); free(myPointer); Will I have a memory leak? No, you won't have a memory leak. realloc will simply mark the rest "available" for future malloc operations. But you still have to free myPointer later on. As an aside, if you use 0 as the size in realloc , it will have the same effect as

How to read unlimited characters in C

时光总嘲笑我的痴心妄想 提交于 2019-11-27 19:24:24
问题 How to read unlimited characters into a char* variable without specifying the size? For example, say I want to read the address of an employee that may also take multiple lines. 回答1: You have to start by "guessing" the size that you expect, then allocate a buffer that big using malloc . If that turns out to be too small, you use realloc to resize the buffer to be a bit bigger. Sample code: char *buffer; size_t num_read; size_t buffer_size; buffer_size = 100; buffer = malloc(buffer_size); num

Is it safe to use realloc?

只谈情不闲聊 提交于 2019-11-27 14:23:24
Some time ago a friend of mine told me not to use realloc because it's unsafe, but he couldn't tell me why, so I made some research on the subject and the nearest references to my doubt were: https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/809-BSI.html http://www.iso-9899.info/wiki/Why_not_realloc I want to know if I can continue to use realloc in my code or if it's unsafe is there any other way to reallocate memory? Thanks for your attention. The first of the two linked article raises two complaints above and beyond the "check the call succeeded" points already raised here.

two-dimensional dynamic array (realloc in c)

巧了我就是萌 提交于 2019-11-27 14:13:58
问题 I am trying to load two double numbers from input to two-dimensional array dynamicaly realocated by every user input. #include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { int count; double number1, number2, **numbers; while (scanf("%lf,%lf", number1, number2) != EOF) { count++; numbers = (double**) realloc(numbers, count * 2 * sizeof (double)); if (numbers == NULL) { exit(1); } numbers[count][0] = number1; numbers[count][1] = number2; } return 0; } Program fails every time

Is it safe to realloc memory allocated with new?

元气小坏坏 提交于 2019-11-27 11:47:53
From what is written here , new allocates in free store while malloc uses heap and the two terms often mean the same thing. From what is written here , realloc may move the memory block to a new location. If free store and heap are two different memory spaces, does it mean any problem then? Specifically I'd like to know if it is safe to use int* data = new int[3]; // ... int* mydata = (int*)realloc(data,6*sizeof(int)); If not, is there any other way to realloc memory allocated with new safely? I could allocate new area and memcpy the contents, but from what I understand realloc may use the

How do realloc and memcpy work?

风格不统一 提交于 2019-11-27 11:42:22
I have two questions. Do realloc() and memcpy() copy the entries in an array to another in a way faster than just iterating on each element O(N) ? If the answer is yes then what do you think is its complexity ? If the size allocated is smaller than the original size, does realloc() copy the entries to somewhere else or just leave them as they are decreasing the size of the array ? 1 - No. They copy a block at a time. See http://www.embedded.com/design/configurable-systems/4024961/Optimizing-Memcpy-improves-speed for a pretty good analysis. 2 - This is implementation dependent. See http://www

Aligned memory management?

房东的猫 提交于 2019-11-27 10:48:56
问题 I have a few related questions about managing aligned memory blocks. Cross-platform answers would be ideal. However, as I'm pretty sure a cross-platform solution does not exist, I'm mainly interested in Windows and Linux and to a (much) lesser extent Mac OS and FreeBSD. What's the best way of getting a chunk of memory aligned on 16-byte boundaries? (I'm aware of the trivial method of using malloc() , allocating a little extra space and then bumping the pointer up to a properly aligned value.

How does realloc know how much to copy?

亡梦爱人 提交于 2019-11-27 07:45:33
问题 how does realloc know the size of original data? void *realloc(void *ptr, size_t size); So, if the implementation is like this: temp = malloc(size); memcpy(.. // How much to copy? free(ptr); return temp; I realize this is not the original implementation, and realloc doesn't always do free, but when it does, how much does it copy? Edit: Thanks for the answers. But how can I then implement realloc in my code with malloc/free/..? 回答1: It knows because malloc recorded that information when you