Tag: realloc

realloc给出错误 – 下一个大小无效

#include #include #include int temp; int main() { FILE * fp; fp = fopen(“input2.txt”, “r”); //Open the input int counter = 0; int realloc_counter = 10; int *line_array; //Initialize the array line_array = malloc(10 * sizeof(int)); //Allocate memory for initial ten numbers, of size int for each while (fscanf(fp, “%d”, &temp) > 0) { line_array[counter] […]

结构数组内存realloc stderr

当我将元素放入其中时,我试图重新分配一个结构数组,因为我将元素放入其中但我仍然收到一个realloc stderr。 struct数组最终将包含235,000个元素。 当我将初始起始大小设置为100,000时,我在尝试重新分配时收到stderr。 如果我将初始起始大小设置为300,000,则不会给出错误,因为它永远不会到达realloc语句。 #define _XOPEN_SOURCE #include #include #include #include #define BUFFERLEN 200 #define START_SIZE 100000 #define GROW 1000 #define TRUE 1 #define FALSE 0 typedef struct{ char *forw; char *back; } word; typedef struct{ char *entry; } single_words; FILE *words; /*————-Function Prototypes————*/ void reverse(char* string, char* revstr, int len); int search_struct(char* find, word* […]

Realloc设置指针为空

void test(){ char *c = malloc(strlen(“I like coffe”) + 1); strcpy(c, “I like coffe”); char **s = &c; while(strlen(*s) =p; i–){ w[i+1] = w[i]; } w[p] = c; } 此函数用于在char *插入新字符。 此外,此function在while循环内。 它工作正常但是第三次​​循环运行时,它只设置*s = “” 。 我认为通过使用char *tmp我可以保留数据,如果有任何错误的事情发生。 我不明白为什么P *s被设置为空字符串。

C中的双重自由或损坏(快速顶部)错误/分段错误

我正在尝试动态分配一个数组来从命令行读取用户输入。 它可以工作99/100次,但是如果我反复键入一堆字符,我有时会遇到分段错误错误或双重自由或损坏(快速顶部)错误。 此错误相对难以重现。 我很确定错误的发生是因为我重新分配数组的方式。 while(1){ char *buf_in; // Holds user keyboard input int cnt = 0, length = 0; // cnt stores current read buffer size, length allows input into buf_in char ch; int buf_max = 64; // Current buffer size. Dynamically allocated buf_in = malloc(buf_max * sizeof(char)); if (buf_in==NULL){ fprintf(stderr,”Error allocating memory!\n”); exit(EXIT_FAILURE); } […]

Realloc导致程序崩溃

目前正在尝试编写基于菜单的程序,要求用户输入学生记录,在输入初始记录后,我希望用户可以选择添加更多记录槽但是当我尝试重新分配我的2d指针数组时,我的程序崩溃,特别是在第二个function之后调用。 #include #include #include #define LENGTH 21 void printRecords(int* size, char **fistName, char **lastName, float *grade); void addRecord(int* size, char** firstName, char** lastName, float *grade); int main(void) { /* Minimum measure check */ int size = 0, *check = (int*)malloc(sizeof(int)); *check = 1; while (check) { printf(“Please indicate number of records you want to enter […]

C realloc不改变字符数组的表观大小

当我运行下面的代码时,我得到了给定的输出。 #include /* printf */ #include /* malloc, realloc */ int main() { char* characters = (char *) malloc(10 * sizeof(char)); printf(“Before: characters at %p, size=%lu\n”, (void *) characters, sizeof(characters) / sizeof(characters[0])); char* temp = (char *) realloc(characters, 100); if (temp) { printf(“After realloc, characters size = %lu, temp size = %lu\n”, sizeof(characters) / sizeof(characters[0]), […]

使用realloc时获取(核心转储)

void replace(char *str) { unsigned int len = 0; unsigned int no_of_spaces = 0; while (*str) { if ((char)*str == SPACE) no_of_spaces++; str++; len++; } unsigned int new_len = len + 2 * no_of_spaces; str = (char*) realloc(str, new_len * sizeof(char)); str[new_len] = ‘\0’; } 我使用像replace(“random string”);这样的函数replace(“random string”); 。 在这里,我试图增加字符串的大小,以便可以用另一个字符串替换空格。 为此,我需要计算空格的数量,并获得原始字符串的长度。 我能够做到这一点。 为了resize我使用realloc但是当我运行它时,它会Aborted (core dumped) […]

为什么必须为realloc分配指针而不改变内存块中的第一个值?

int *ptr; … realloc(ptr,++count*sizeof(int)); or ptr=realloc(ptr,++count*sizeof(int)); 我注意到如果我不止一次使用第一个选项,第一个内存地址( ptr指向)的值变为未定义(尽管内存块中的所有其他值都很好,可以通过下标ptr访问)。 但是,我假设所有realloc都是缩小或增加内存块的大小,而ptr仍然指向相同的内存块,它的值都不会改变。 因此,如果我使用第一个选项,为什么内存块中的第一个地址最终会出现意外值,因为ptr仍然指向同一个地址? EDIT:我确实记得为ptr分配内存,只是没想到它的工作提到。

将值附加到动态数组的末尾

好吧,我在这个寒假期间一直在研究一点C,在我的冒险经历中,我偶然发现了动态arrays的问题。 这真的是一个相当简单的程序。 我想要做的是创建一个包含斐波那契数列的数组。 这是代码: #include #include int dynamic_arry_append(int* arry, int* number, int* size); int main() { int i, n, size = 3, *arry = NULL, fibarr[size]; printf(“Dynamic array, Fibonacci series. \n”); printf(“Capture upto element: “); scanf(“%d”, &n); i = 0; // passing the first elements fibarr[0] = 0; fibarr[1] = 1; fibarr[2] = 1; while […]

在sqlite的回调函数中动态重新分配2个dim数组

我正在研究sqlite-.dll用于教育目的。 我试图在每次使用数据库中的新行调用回调函数时动态地在我的2维数组中添加一行。 (例如SELECT * FROM CUSTOMER)。 然后,应将此数组中存储的数据作为C接口返回。 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; } 使用回调函数: static int callback(void *data, int argc, char **argv, char **azColName) { char **old = (char **)data; int num_rows = sizeof(old) / sizeof(old[0]); […]