Tag: 免费

使用free()释放内存不起作用

这是一个小程序,它填充一些数组并在屏幕上打印其内容: #include #include typedef struct{ double **plist; int plistSize; } ParticleList; void sendPar(int *n, int np){ // allocate memory for struct pl ParticleList pl; // allocate memory for ParticleList np times pl.plist = malloc(sizeof(ParticleList) * np); // allocate memory for lists of size n[k] for(int k=0; k<np; k++){ pl.plist[k] = malloc(sizeof(double) * n[k]); } // […]

malloc并在C中免费

如果我有类似的东西 struct Node *root; struct Node *q; root = malloc( sizeof(struct Node)); q = root; free(q); 是节点q指向释放? 或者我必须将root传递给free函数?

基本C指针分配/释放

使用GNU的GSL库编写C代码,从未正式学习任何代码,快速基本问题。 纠正我,如果我错了,但是我理解它的方式,当我分配内存用于我的矩阵(使用内置的var = gsl_matrix_alloc(x,x) )并将它们存储在变量中时,我基本上是创建一个指针,它只是一些内存地址,如:x01234749162 它指向我的GSL矩阵的第一个指针/内存位置。 跟踪何时释放与指针相关联的结构的内存(同样,内置gsl_matrix_free(x,x,x) )是没有问题的,我知道我需要在重新分配结构的指针之前执行此操作,否则我创造了一个内存泄漏。 所以现在我的问题,再次,我知道这是基本的,但听到我 – 我无法在stackoverflow上找到一个特别直接的答案,主要是因为很多答案涉及C ++而不是C – 我怎么做释放指向结构本身的指针? 每个人都说“哦,只需将其设置为NULL”。 为什么会这样? 这只是将POINTS的内存地址更改为解除分配的结构。 这是否告诉MMU现在可以使用该内存位置? 例如,当我在XCode中调试程序时,gsl_matrix结构的所有属性都被成功释放; 一切都变成了这个随机hex字符的垃圾串,这就是自由存储应该做的事情。 但是,我仍然可以通过调试器看到变量名(指针)…即使我将变量设置为NULL。 我会解释这意味着我没有释放指针,我只是释放了结构并将其设置为x0000000(NULL)。 我做的一切都是正确的,这只是XCode的一个特性,还是我遗漏了一些基本的东西? 我意识到,如果结构被解除分配,单个指向结构的指针可能会被认为不是什么大问题,但它很重要。 这是一些试图说明我的想法的代码。 gsl_matrix* my_matrix; // create single memory address in memory, not pointing to anything yet my_matrix = gsl_matrix_alloc(5, 5); // allocates 25 memory spaces for the values that the pointer held […]

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

C – 被释放的指针未被分配

我试图释放我从malloc()分配的向量中分配的指针,当我尝试删除第一个元素(index [0])时,它工作,当我尝试删除第二个(index [1])我收到此错误: malloc: *** error for object 0x100200218: pointer being freed was not allocated 代码: table->t = malloc (sizeof (entry) * tam); entry * elem = &table->t[1]; free(elem);

释放二维arrays – 检测到堆腐蚀

编辑:对不起伙计们,我忘了提到这是在VS2013中编码的。 我有一个全局声明的结构: typedef struct data //Struct for storing search & sort run-time statistics. { int **a_collision; } data; data data1; 然后我分配我的记忆: data1.a_collision = (int**)malloc(sizeof(int)*2); //Declaring outer array size – value/key index. for (int i = 0; i < HASH_TABLE_SIZE; i++) data1.a_collision[i] = (int*)malloc(sizeof(int)*HASH_TABLE_SIZE); //Declaring inner array size. 然后我初始化所有元素: //Initializing 2D collision data array. for (int […]

malloc和free动态变化的结构

我在动态变化的结构中移动指针时遇到了麻烦。 我已经创建了我的代码,你可以在malloc中获得更多的内存,这看起来很有效。 我遇到的问题是如何添加到结构,如何释放内存以及如何从结构移动到结构并打印所有项目。 我正在尝试测试添加和打印(删除function,似乎没有工作,segfaults) 当我添加到结构然后打印结构时,我从我添加的值中获得了一个段错误。 我不知道我是否正确地从第一个结构转移到下一个结构。 #include #include #include “pointer.h” /******************************************** Creates more memory for size (strut * rec+1) *********************************************/ employee *create(int record){ employee *new_employee = malloc(sizeof(employee) * (record+1)); return new_employee; } /******************************************** Copies the data from one structure to a new structure with size “structure” multipled by rec+1 ***********************************************/ employee *copy(employee *data, int record){ […]

在实际指针的副本上使用free()是可接受/正确的吗?

这是: int *a = malloc (sizeof (int) ); int *b = a; free (b); 与此相同: int *a = malloc (sizeof (int) ); free (a); 如果是,不需要解释,但如果没有,请详细说明原因!

免费的2Darrays列

我有一个2D数组的字符串,动态分配: char*** allocateArray(int line, int col) { char*** dictionary; int i=0,j=0; dictionary=(char***)malloc(sizeof(char**)*line); for(i=0;i<line;i++) { dictionary[i] = (char**)malloc(sizeof(char*)); for(j=0;j<col;j++) dictionary[i][j] = (char*)malloc(sizeof(char*)); } return dictionary; } 现在我想释放最后一栏(比如说),我该怎么办? 我使用free(dictionary[i][j]) ,但它实际上是免费的? 数组中的[i][j]单元格,或指向它的指针? 我需要释放两者。

C调整动态数组的大小

我正在为C类创建动态数组数据结构。 我已经完成了大部分工作,现在我正在测试它。 它目前在resize时卡住了。 我用过printf来解决这个问题。 看起来resize正在工作,但是当我去添加我的下一个项目后resize时,它就停在那里。 我认为这可能与我的内存分配或我指向的方式有关,并在resize期间释放数组。 我是C的新手,所以这些都是我的观点。 #include #include #include “dynArray.h” #include struct DynArr { TYPE *data; /* pointer to the data array */ int size; /* Number of elements in the array */ int capacity; /* capacity of the array */ }; /* ************************************************************************ Dynamic Array Functions ************************************************************************ */ /* Initialize (including allocation of […]