基本的C指针分配/释放 [英] Basic C pointer allocation/deallocation

查看:169
本文介绍了基本的C指针分配/释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用C

写作code,从未正式学过的它,使用GNU的GSL库,快速的根本性问题。

纠正我,如果我错了,但我的理解是,当我分配内存使用我的矩阵(使用方式,内置的 VAR = gsl_matrix_alloc(X,X)),并将其存储在一个变量,我基本上是建立
一个指针,这是一个简单的记忆,像一些地址:
x01234749162

,它指向我的GSL矩阵的第一指针/内存位置。跟踪时,释放与指针相关结构的内存(同样,内置的 gsl_matrix_free(X,X,X))是没有问题的,我明白我需要要做到这一点之前,我重新分配结构的指针,否则,我创建了一个内存泄漏。

所以现在我的问题,并再次,我知道这是基本的,但听我说 - 我无法找到计算器上的一个特别直接的答案,主要是因为很多问题的答案涉及到C ++而不是C--我该如何释放指针本身的结构?

大家都说:哦,只需将其设置为NULL。为什么这项工作?这仅仅是改变指向解除分配结构的内存地址。这是否告诉MMU该内存位置还好现在使用?当我以x code调试我的程序,例如,所有的gsl_matrix结构的特性被成功释放;一切都只是变得随机十六进制字符,这是什么free'ing内存是应该做的这个垃圾字符串。但是,我仍然可以看到变量名(指针),而通过调试步进......即使我设置变量设置为NULL。我愿意为我的意思没有释放指针间preT,我刚刚释放结构并将其设置为x0000000(NULL)。

我做的一切正确的,这就是X $ C $的c只是一个功能,还是我失去了一些东西基本?

和我认识到,一个指向一个结构,如果结构被释放,可以被认为是不是一个大问题,但它很重要。

下面是一些code,试图说明我的想法。

  gsl_matrix * my_matrix;
//在内存中创建一个内存地址,而不是指向任何事情my_matrix = gsl_matrix_alloc(5,5);
// 25分配存储空间为指针由my_matrix保存的值
//点​​太
//注意:所以,现在有分配到基体26内存点,排除其他
//性质与我的矩阵结构中创建以来,对不对?gsl_matrix_free(my_matrix); //将释放这25位结构有,
//与其他属性一起,可能已经自动创建
免费(my_matrix); // SIGBRT错误。是指针解除了分配结构
//仍在使用一个内存地址?
my_matrix = NULL; //这个没有意义的我。我得到任何未来转诊
//为my_matrix指针将直接返回垃圾,所以指针设置为
//可以在调试帮助,但指针 - 这只是一个内存
//地址 - 完全释放的,例如,在调试器中的变量名
//消失?


解决方案

  

大家都说:哦,只需将其设置为NULL。为什么这项工作?


他们大概的意思是,这将解决你在哪里调用免费上一个指向已经一些数据问题取消分配,这是你在做什么这里:

  gsl_matrix_free(my_matrix); // DEALLOCATE
免费(my_matrix); //错误,大问题:my_matrix点取消分配数据

这是因为调用免费上一个空PTR解决问题是一个无操作:

  gsl_matrix_free(my_matrix); // DEALLOCATE
my_matrix = NULL;
免费(my_matrix); //错误,但没有问题

注意 my_matrix 本身具有自动存储,所以没有必要手动取消分配它。当它超出范围时,内存将被回收。需要被解除分配的唯一的事情就是被动态分配的内存(并到 my_matrix 分。)

Writing code in C, never formally learned any of it, using GNU's GSL library, quick fundamental question.

Correct me if I'm wrong, but the way I understand it, when I allocate memory to use for my matrices (using the built-in var = gsl_matrix_alloc(x,x)) and store them in a variable, I'm essentially creating a pointer, which is simply some address of memory, like: x01234749162

which POINTS to the first pointer/memory location of my GSL matrix. Tracking when to deallocate the memory of the structure associated with a pointer (again, built-in gsl_matrix_free(x,x,x)) is no problem, and I understand I need to do this before I reassign the pointer of the structure, otherwise I've created a memory leak.

So now to my question, and again, I know this is basic, but hear me out--I couldn't find a particularly straight answer on stackoverflow, mostly because a lot of the answers involve C++ rather than C--how do I free the pointer to the structure itself?

Everyone says "oh, just set it to NULL". Why would that work? That's just changing the memory address that POINTS to the deallocated structure. Does that tell the MMU that that memory location is okay to use now? When I debug my program in XCode, for example, all of the properties of the gsl_matrix structure is successfully deallocated; everything just becomes this garbage string of random hex characters, which is what free'ing memory is supposed to do. But, I can still see the variable name (pointer) while stepping through the debugger... even if I set the variable to NULL. I would interpret that as meaning I did not free the pointer, I just freed the structure and set it to x0000000 (NULL).

Am I doing everything correct, and this is just a feature of XCode, or am I missing something basic?

And I realize that a single pointer to a structure, if the structure is deallocated, could be argued to not be a big deal, but it matters.

Here's some code to try to illustrate my thoughts.

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 by my_matrix 
// points too
// Note: so, now there's 26 memory spots allocated to the matrix, excluding other
// properties created along with the my-matrix structure, right? 

gsl_matrix_free(my_matrix); // deallocates those 25 spaces the structure had, 
// along with other properties that may have been automatically created
free(my_matrix); // SIGBRT error. Is the pointer to the deallocated structure
// still using that one memory address?  
my_matrix = NULL; // this doesn't make sense to me.I get that any future referral
// to the my_matrix pointer will just return garbage, and so setting a pointer to
// that can help in debugging, but can the pointer--that is just one memory 
// address--be completely deallocated such that in the debugger the variable name
// disappears?  

解决方案

Everyone says "oh, just set it to NULL". Why would that work?

They probably mean that this would fix the problem where you are calling free on a pointer to some data that has already been de-allocated, which is what you are doing here:

gsl_matrix_free(my_matrix); // deallocate
free(my_matrix); // Mistake, BIG PROBLEM: my_matrix points to de-allocated data

It fixes the problem because calling free on a null-ptr is a no op:

gsl_matrix_free(my_matrix); // deallocate
my_matrix = NULL;
free(my_matrix); // Mistake, but no problem

Note: my_matrix itself has automatic storage, so there is no need to de-allocate it manually. Its memory will be reclaimed when it goes out of scope. The only thing that needs to be de-allocated is the memory that was dynamically allocated (and to which my_matrix points.)

这篇关于基本的C指针分配/释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆