realloc

two-dimensional dynamic array (realloc in c)

为君一笑 提交于 2019-11-28 22:08:59
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 i try to save value into array (probably memory problem). It is compiled without problems. Can anyone

Can I assume that calling realloc with a smaller size will free the remainder?

北战南征 提交于 2019-11-28 20:19:57
Let’s consider this very short snippet of code: #include <stdlib.h> int main() { char* a = malloc(20000); char* b = realloc(a, 5); free(b); return 0; } After reading the man page for realloc, I was not entirely sure that the second line would cause the 19995 extra bytes to be freed. To quote the man page: The realloc() function changes the size of the memory block pointed to by ptr to size bytes. , but from that definition, can I be sure the rest will be freed? I mean, the block pointed by b certainly contains 5 free bytes, so would it be enough for a lazy complying allocator to just not do

Aligned memory management?

*爱你&永不变心* 提交于 2019-11-28 17:34:46
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. I'm hoping for something a little less kludge-y, though. Also, see below for additional issues.) If I

Dynamic array in C — Is my understanding of malloc and realloc correct?

孤街醉人 提交于 2019-11-28 16:13:48
I am learning how to create dynamic 1D arrays in C. The code below tries to do the following: Using malloc , create a dynamic array of length 10 , that holds values of type double . Set each entry of the array to j/100 for j = 0, 1,..., 9 . Then print it out. Add an additional empty entry to the end of the array using realloc . Set the new entry to j/100 and print out each entry again. Testing: double* data = (double*)malloc(10*sizeof(double)); for (j=0;j<10;j++) { data[j]= ((double)j)/100; printf("%g, ",data[j]); } printf("\n"); data = (double*)realloc(data,11*sizeof(double)); for (j=0;j<11;j

What header should I include for memcpy and realloc?

本秂侑毒 提交于 2019-11-28 13:15:30
I am porting a project to the iPhone and it uses realloc and memcpy which are not found. What is the header to include? It's a project mixing Objective C and C++ and I am starting to be lost. Thanks in advance for your help! In C: #include <string.h> // memcpy #include <stdlib.h> //realloc In C++, remove the .h and prefix with a c . In C++, they will be placed in the std namespace, but are also global. In C++ it's more idiomatic to use std::copy than C's memcpy , although the latter does work just as well. To get std::copy , you need to #include <algorithm> . There's not a direct C++

Correct use of Realloc

时光总嘲笑我的痴心妄想 提交于 2019-11-28 12:51:17
This is the way I've been taught to use realloc() : int *a = malloc(10); a = realloc(a, 100); // Why do we do "a = .... ?" if(a == NULL) //Deal with problem..... Isn't that redundant? Can't i just do something like this? : if(realloc(a, 100) == NULL) //Deal with the problem Same for other realloc examples i've found around, for example: int *oldPtr = malloc(10); int * newPtr = realloc(oldPtr, 100); if(newPtr == NULL) //deal with problems else oldPtr = newPtr; Can't i just do this instead? : int *oldPtr = malloc(10); if(realloc(oldPtr, 100) == NULL) //deal with problems //else not necessary,

Using Realloc in C

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 12:23:05
Its really a post for some advice in terms of the use of realloc, more specifically, if I could make use of it to simplify my existing code. Essentially, what the below does, it dynamically allocate some memory, if i goes over 256, then the array needs to be increased in size, so I malloc a temp array, with 2x the size, memcpy etc. ( see below ). I was just wondering if realloc could be used in the below code, to simplify it, any advice, sample code, or even hints on how to implement it is much appreciated! Cheers. void reverse(char *s) { char p; switch(toupper(s[0])) { case 'A': case 'E':

How to update other pointers when realloc moves the memory block?

我是研究僧i 提交于 2019-11-28 11:55:35
The realloc reference says: The function may move the memory block to a new location, in which case the new location is returned. Does it mean that if I do this: void foo() { void* ptr = malloc( 1024 ); unsigned char* cptr = ( unsigned char* )ptr+256; ptr = realloc( ptr, 4096 ); } then cptr may become invalid if realloc moves the block? If yes, then does realloc signal in any way, that it will move the block, so that I can do something to prevent cptr from becoming invalid? t0mm13b Yes, cptr will become invalid as realloc moves the block! And no, there is no mention of signalling to you to

realloc(): invalid next size [duplicate]

吃可爱长大的小学妹 提交于 2019-11-28 10:39:03
问题 This question already has answers here : Facing an error “*** glibc detected *** free(): invalid next size (fast)” (2 answers) Closed 5 years ago . I'm having a problem with the realloc function. I'm using C only (so no vector) with LibCurl. The problem I'm having is that I'm getting the following error (realloc(): invalid next size) on the 12th iteration of the write_data function (the function I pass to Curl as a callback, it is called each time libcurl has some data to pass back (data is

Using realloc (X, 0) instead of free() and using malloc with length of a string +1

妖精的绣舞 提交于 2019-11-28 10:20:50
So I don't really know how to put the title this time. First of all I'd like to say that I've seen several comments on this page about warning if the question is related to "homework". Mine is, but it's also completed and I just want to further understand what is going on with the code. I have also read posts and books for some time, but I think I am still missing things. I have 2 lines of code I don't quite understand in the code I worked with. The work is about getting whatever file is used as argument (if it's 0 files, it read from stdin), and print it on the standard output backwards. All