realloc

How do realloc and memcpy work?

旧街凉风 提交于 2019-12-28 03:35:08
问题 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: 1 - No. They copy a block at a time. See http://www.embedded.com/design/configurable-systems/4024961

Reallocating memory and adding a string at the reallocated memory space in C

那年仲夏 提交于 2019-12-25 11:57:05
问题 I am having trouble adding "records" at the end of a dynamically allocated string array. Before reallocating more memory for the records to be added, everything works fine, and then I basically replicate what I initially did but now with realloc. And after I am done inputting the added Records I get an error and I don't know how to go about adding records. NOTE* The posed code is really stripped down from the original. I've tried many things but to no avail, thanks for all the help in advance

realloc(): invalid next size (core dumped)

偶尔善良 提交于 2019-12-25 07:29:46
问题 I have a simple function that reallocs buffer every time I want to add one char, It works till I want to realloc 24th time. Then realloc(): invalid next size appears. Here is the code: char * addDataChunk(char * data,char c) { char * p; if(data==NULL) { data=(char*)malloc(sizeof(char)*2); data[0]=c; data[1]='\0'; return data; } else { if(p = (char*)realloc(data,((strlen(data)+1)*sizeof(char)))) { data = p; } else { printf("realloc error\n"); } data[strlen(data)] = c; data[strlen(data)+1] = '

realloc(): invalid next size (core dumped)

只愿长相守 提交于 2019-12-25 07:28:08
问题 I have a simple function that reallocs buffer every time I want to add one char, It works till I want to realloc 24th time. Then realloc(): invalid next size appears. Here is the code: char * addDataChunk(char * data,char c) { char * p; if(data==NULL) { data=(char*)malloc(sizeof(char)*2); data[0]=c; data[1]='\0'; return data; } else { if(p = (char*)realloc(data,((strlen(data)+1)*sizeof(char)))) { data = p; } else { printf("realloc error\n"); } data[strlen(data)] = c; data[strlen(data)+1] = '

Using realloc for array of structs

巧了我就是萌 提交于 2019-12-25 04:10:53
问题 I'm using realloc to increase the amount of memory for an array of structs and it doesn't seem to be increasing at all. I've read all of the posts relating to this and I can't figure out what I'm doing wrong. Here is my code: struct fileInfo { char name[MAXNAMLEN]; struct stat stbuf; }; ... struct fileInfo * newFileInfoPtr; struct fileInfo * fileInfoPtr = (struct fileInfo *) realloc (NULL, sizeof (struct fileInfo)); // loop through directory to find all filenames while ((direntPtr = readdir

C: array of char pointers not working as expected dynamically

旧城冷巷雨未停 提交于 2019-12-24 12:14:15
问题 I have the below snippets from my code where I am trying to use a dynamically allocated char * array to hold strings coming from stdin. char **reference reference = calloc(CHUNK, sizeof(char *)); I am using a temporary static array to first store the string from stdin , then based on a certain condition copy it to the array of char * . I am allocating memory to individual char * in runtime. reference[no_of_ref]=malloc(strlen(temp_in) + 1); reference[no_of_ref++]=temp_in; // printf(" in temp :

Dynamically realloc 2 dim array in callback function of sqlite

邮差的信 提交于 2019-12-24 08:31:58
问题 I am working on a sqlite-.dll for educational purpose. I am trying to dynamically add a row in my 2 dimensional array for each time the callback function is called with a new row from the database. (e.g. SELECT * FROM CUSTOMER). The data stored in this array should then be returned as a C-Interface. SQLCONTROL_API char** sql_execQuery(char *dbName, char *sqlStatement) { char **a = 0; /*Some sqlite stuff*/ int rc = sqlite3_exec(db, sqlStatement, callback, &a, &zErrMsg); return a; } With the

C realloc inside a function

╄→尐↘猪︶ㄣ 提交于 2019-12-24 07:15:22
问题 Here is my code: #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <string.h> #include <sys/stat.h> void mp3files(char** result, int* count, const char* path) { struct dirent *entry; DIR *dp; dp = opendir(path); if (dp == NULL) { printf("Error, directory or file \"%s\" not found.\n", path); return; } while ((entry = readdir(dp))) { if ((result = (char**) realloc(result, sizeof (char*) * ((*count) + 1))) == NULL) { printf("error"); return; } result[*count] = entry->d_name; (

Disadvantages of calling realloc in a loop

雨燕双飞 提交于 2019-12-24 04:02:32
问题 I'm trying to implement some math algorithms in C on Windows 7, and I need to repeatedly increase size of my array. Sometimes it fails because realloc can't allocate memory. But if I allocate a lot of memory at once in the beginning it works fine. Is it a problem with the memory manager? Can anyone explain me this please? 回答1: When you allocate/deallocate memory many times, it may create fragmentation in the memory and you may not get big contiguous chunk of the memory. When you do a realloc,

Realloc double 2D array in C

大兔子大兔子 提交于 2019-12-24 02:17:26
问题 I wrote an function which should realloc the array for each input. I think that function works well, but when I'm trying to work with the array I got segmentation fault. Input: [1,2] [6,3] [2.5,3.5] I have to check if the user enters input in the correct form '[' number ',' number ']' . I have to have at least 2 entries. The end of input isn't new line, but EOF ( CTRL + D or CTRL + Z ). My code: double ** allocation (double ** field, int i) { double x = 0, y = 0; char obr[1], cbr[1], col[1];