realloc

Realloc and sscanf into a function

走远了吗. 提交于 2019-12-24 01:27:58
问题 I have a file who has num lines: every line contains one number. I want to save every number into a vector *vet . Why this code doesn't work? Segmentation fault (core dumped) I think that the error is sscanf in save_numbers function, but I don't know why. #include <stdio.h> #include <stdlib.h> /* This function allocate memory and save numbers into a vector */ int save_numbers (int **vet, int *num) { FILE *fin; int i = 0; char buff[10]; if ( !(fin = fopen("numbers.dat", "r")) ) return 1; while

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

北战南征 提交于 2019-12-23 19:12:16
问题 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 realloc() in C library may copy data from old address. So is there a function in any library like what I want? 回答1: realloc attempts do extend the buffer without copying, but can only do that if the extra space is free. In your case, you just

realloc variants

拟墨画扇 提交于 2019-12-23 18:09:55
问题 Just to situate the context: it's about a string pool, meaning a hash table with string keys (actually special strings that know their length, but I guess this detail is irrelevant here). The point is the resizing of the array of lists (used as table buckets) when the pool needs to grow. But --this is the core detail-- cells containing string actually in an array of cells, instead of being spread out in all corners of memory [1]. Thus, I don't need the lists anymore, they're just outdated

Dynamically adding to char array with unknown size

耗尽温柔 提交于 2019-12-23 05:29:16
问题 I am currently working on an assignment where I have to extract text one char at a time from each file and then concatenate them and print it out. I have found a way to extract my characters and save them in variable a-d. How do I add these char to my char array to print out to the end? I initially malloc it to the size of a char and at each if statement I realloc. All the guides I found online recommend I use strcat but I cannot pass in the a-d variables to append. How would I fix this?

Passing a dynamic array in to functions in C

☆樱花仙子☆ 提交于 2019-12-22 05:50:07
问题 I'm trying to create a function which takes an array as an argument, adds values to it (increasing its size if necessary) and returns the count of items. So far I have: int main(int argc, char** argv) { int mSize = 10; ent a[mSize]; int n; n = addValues(a,mSize); for(i=0;i<n;i++) { //Print values from a } } int addValues(ent *a, int mSize) { int size = mSize; i = 0; while(....) { //Loop to add items to array if(i>=size-1) { size = size*2; a = realloc(a, (size)*sizeof(ent)); } //Add to array i

Should I enforce realloc check if the new block size is smaller than the initial?

為{幸葍}努か 提交于 2019-12-22 04:44:12
问题 Can realloc fail in this case? int *a = NULL; a = calloc(100, sizeof(*a)); printf("1.ptr: %d\n", a); a = realloc(a, 50 * sizeof(*a)); printf("2.ptr: %d\n", a); if(a == NULL){ printf("Is it possible?\n"); } return (0); } The output in my case is: 1.ptr: 4072560 2.ptr: 4072560 So 'a' points to the same adress. So should I enforce realloc check? Later edit : Using MinGW compiler under Windows XP. Is the behaviour similar with gcc on Linux ? Later edit 2: Is it OK to check this way ? int *a =

Replacing realloc (C --> C++)

六眼飞鱼酱① 提交于 2019-12-22 03:42:44
问题 In an earlier question, I asked about typecasting pointers, but was directed to the better solution of using the C++ allocation system instead of mallocs. (I am converting some C code to C++) However, I still have an issue with a similar function: I changed: tmp = malloc(sizeof(char*) * mtmp); --> tmp = new char*[mtmp]; and free(tmp) --> delete [] tmp; However, what do I do with realloc in the following function: char* space_getRndPlanet (void) { int i,j; char **tmp; int ntmp; int mtmp; char

Realloc() does not correctly free memory in Windows

跟風遠走 提交于 2019-12-21 13:00:16
问题 I am attempting to use realloc() in a Windows application. I am allocating a large block of memory, then using realloc() to shrink it down later once I know the correct size. I am finding that although realloc() appears to work correctly (memory in Task Manager reflects what you would expect) the application eventually runs out of memory. From what I can tell, it's as though relloc() frees the memory but does not free the virtual address space associated with the memory. As a result, malloc()

How `realloc` work actually in the background?

时间秒杀一切 提交于 2019-12-21 06:58:11
问题 How realloc work actually in the background? If there is not enough memory available at old place does this one allocating two/many memory blocks and one pointer pointing to that and other are internally linked with each other or the old region copied into new place where enough memory is available and pointer is updating to new address and deleting the old memory? And is that realloc is Compiler/OS dependent or independent ? 回答1: realloc attempts to extend your available memory range if

glibc detected, realloc(): invalid pointer

安稳与你 提交于 2019-12-21 02:42:09
问题 I apologize for the lengthy code. I have a simple question, but I thought I include my code so it will be clear where I am coming from. I get a realloc corruption. I think the corruption is because I am not freeing correctly. In reality I am not sure what glibc even says or means. Can any one briefly explain that to me? Again sorry for the lengthy code. #include "draw2.h" #include "draw2a.h" #include "draw2b.h" const char Exec_c[] = "java -jar Sketchpad.jar"; void parseFile(FILE * fp, FILE