Tag: malloc

使用sizeof分配是否会为结构指针生成错误的大小?

使用valgrind读取这个我得到:无效的写入/读取大小4 struct Person{ char* name; int age; }; struct Person* create_person(char *name, int age) { struct Person* me = (struct Person*)malloc(sizeof(struct Person*)); assert(me!= NULL); //make sure that the statement is not null me->name = name; me->age = age; return me; } 使用这个用valgrind得到干净的日志 struct Person{ char* name; int age; }; struct Person* create_person(char *name, int age) […]

调用free()会导致程序崩溃

我有一个非常奇怪的问题,试图在分配的内存上调用free导致我的程序崩溃。 这是相关的代码: int i, count; char *specifier; char aisle[1]; count = 0; /*Find name of the new item and assign to the name field of new_node…*/ for (i = 0; input[i] != ‘,’; i++){ count++; } specifier = (char*)malloc((count+1)*sizeof(char)); if (specifier == NULL){ printf(“Out of memory. Shutting down.\n”); exit(EXIT_FAILURE); } for (i = 0; input[i] […]

即使我使用malloc,C中的fread和fwrite也会出现分段错误

我一直致力于一个程序,由于分段错误而不断崩溃。 我裁掉了导致此问题的最简单的代码部分。 根据我到目前为止所学到的,如果我要求操作系统进行内存分配,然后使用指针读取和写入内存的那个地址,那么应该是好的。 在这种情况下,以下剪切看起来对我来说非常简单:打开一个新文件进行写入,打开源文件,创建outdata以保存足够的字节,从源读取精确的字节值,并使用具有相同参数的fwrite写入新文件。 我有什么基本的东西吗? int main (void) { FILE *outimg = fopen(“test.jpg”, “w”); FILE *rawFile = fopen(“source.file”, “r”); if(rawFile==NULL) { fprintf(stderr, “Unable to open source file\n”); return 1; } int chunkSize = 512; int picChunkCount = 440; unsigned char *outdata = malloc(chunkSize*picChunkCount*sizeof(unsigned char)); if(outdata==NULL) { fprintf(stderr,”Unable to allocate memory for *outdata\n”); return 2; } […]

在C结构上使用free

我有一个简单的链接列表节点如下 typedef struct node { void *data; struct ListElement *next; } node; 我还有一个节点创建和删除function,如下所示: void createNode(void *data){ node *n = malloc(sizeof(node)); //assign data to data and initialize pointer to NULL } void deleteNode(List *list, Node *node){ //Take care of the next pointer free(node); } 当我释放节点时,是否还必须删除结构的成员(数据和下一个指针)? 既然我没有专门为成员使用malloc,而只是为了整个结构? 如果是的话那我该怎么做? 是否将节点的所有成员放在堆上,并且根本不会使用堆栈?

在MPI_Gather C中寻址内存

我正在尝试将数据传递给MPI_Gather 。 我按如下方式分配内存: float *phie, *phitemp; MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); phitemp=(float *) malloc(20*sizeof(float)); if (rank==1) phie=(float *) malloc(itermax*20*size*sizeof(float)); 然后让所有进程使用MPI_Gather()将数据发送到排名1,如下所示: for (iter=0;iter<itermax;iter++) { MPI_Gather((float *) phitemp, 20, MPI_FLOAT, (float *) (phie+iter*20*size*sizeof(float)), 20, MPI_FLOAT, 1, MPI_COMM_WORLD); iter=0; } 我收到错误消息表明我没有正确分配内存。

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 […]

gnu gdb malloc返回不可访问的指针

运行一些代码后,gdb调试会话中的malloc返回不可访问的地址。 首先在主要function开始时rest。 一切都好。 Breakpoint 9, main (argc=5, argv=0x7fffffffe418) at src/ose/sdv/ose_sdv/linux/main.c:557 557 char *cfgfile = NULL; (gdb) call malloc(4) $50 = 23293968 (gdb) x 23293968 0x1637010: 0x00000000 (gdb) c 在运行一些行后,它开始返回无法访问的内存地址,该地址从0xffffffff~开始 Program received signal SIGINT, Interrupt.0x00007ffff70c1f4d in read () from /lib64/libc.so.6 (gdb) call malloc(4) $52 = -1811110576 (gdb) x -1811110576 0xffffffff940ca550: Cannot access memory at address 0xffffffff940ca550 […]

我有一个分段错误,但我没有找到oO?

它应该与合并排序。 合并和排序合并有两个function。 一些未知的函数(从文件和打印数组中读取数组)在输入文件中完全起作用。 Valgrind向我展示了失败是在array2的分配以及它在void合并的第3个while循环中的读写时。 void merge(int* array, int start, int middle, int end) { int size = end – start + 1; int *array2 = malloc(size*sizeof(array2)); int k = start; int m = middle + 1; int i = 0; int j = 0; while ( k <= middle && m <= end ) { […]

错误C2440初始化无法将void转换为typ

#include #include // self – referential structure struct listNode { char data; // each listNode contains a character struct listNode *nextPtr; //pointer to next node }; typedef struct listNode ListNode; // synonym for struct listNode typedef ListNode *ListNodePtr; // synonym for struct listnode* void insert(ListNodePtr *sPtr, char value) { ListNodePtr newPtr = malloc(sizeof(ListNode)); // create […]

打印一系列字符时遇到问题?

由于我不太了解我遇到的整体问题,因此调试变得非常困难。 char *o_key_pad = (char*)malloc(SHA256_DIGEST_LENGTH*sizeof(char)); for(int i = 0; i < SHA256_DIGEST_LENGTH; i++){ o_key_pad[i] = 'a'; } printf("%s\n", o_key_pad); char *i_key_pad = (char*)malloc(SHA256_DIGEST_LENGTH*sizeof(char)); for(int i = 0; i < SHA256_DIGEST_LENGTH; i++){ i_key_pad[i] = 'b'; } printf("%s\n", o_key_pad); printf("%s\n", i_key_pad); 我获得了输出: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 为什么数组’o_key_pad’扩展到包含我放在数组’i_key_pad’中的任何内容,看起来像某种内存问题? 注意:我知道它可以更有效地完成,但为了更清楚地表明我的观点,我已经这样做了。