malloc()用于struct成员 [英] malloc() for struct member
问题描述
您好,我有一个非常简单的问题,我现在对
感到有点困惑。可以通过malloc()或calloc()为
结构成员分配内存然后调用free()吗?例如,我在下面有代码
。
struct mystruct {
int a;
char * b;
int c;
};
struct mystruct myobject;
myobject.b =(char *)malloc(50);
....
....
免费(myobject.b);
上面的代码是好的,还是有任何潜在的问题?
谢谢!
Hello, I have a very simple question that I''m a bit confused about
right now. Is it OK to allocate memory by malloc() or calloc() for a
struct member and then call free() on it? For example, I have the code
below.
struct mystruct {
int a;
char *b;
int c;
};
struct mystruct myobject;
myobject.b = (char *)malloc(50);
....
....
free(myobject.b);
Is the above code OK, or is there any potential problem with it?
Thanks!
推荐答案
" googler" < PI ******** @ yahoo.com>写道:
"googler" <pi********@yahoo.com> writes:
你好,我有一个非常简单的问题,我现在对
感到有些困惑。可以通过malloc()或calloc()为
struct成员分配内存然后调用free()吗?例如,我的代码如下所示。
struct mystruct {
int a;
char * b;
int c;
结构mystruct myobject;
myobject.b =(char *)malloc(50);
...
...
free(myobject.b);
上面的代码是好的,还是有任何潜在的问题?
Hello, I have a very simple question that I''m a bit confused about
right now. Is it OK to allocate memory by malloc() or calloc() for a
struct member and then call free() on it? For example, I have the code
below.
struct mystruct {
int a;
char *b;
int c;
};
struct mystruct myobject;
myobject.b = (char *)malloc(50);
...
...
free(myobject.b);
Is the above code OK, or is there any potential problem with it?
我能看到的唯一问题就是你施放了malloc()的结果。
这是合法但不必要的,可以掩盖错误。将行改为
到
myobject.b = malloc(50);
除此之外,我没有看错。
-
Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
The only problem I can see is that you cast the result of malloc().
This is legal but unnecessary, and can mask errors. Change the line
to
myobject.b = malloc(50);
Other than that, I don''t see anything wrong.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
2005年10月5日20:06:15 -0700,googler < PI ******** @ yahoo.com>写道:
On 5 Oct 2005 20:06:15 -0700, "googler" <pi********@yahoo.com> wrote:
你好,我有一个非常简单的问题,我现在对
有点困惑。可以通过malloc()或calloc()为
struct成员分配内存然后调用free()吗?例如,我的代码如下所示。
struct mystruct {
int a;
char * b;
int c;
} mystruct myobject;
myobject.b =(char *)malloc(50);
不要从malloc投出回报。它没有帮助,可能会导致
编译器抑制你真正想要的诊断....
...
free(myobject.b);
这是你避免内存泄漏的方法。是什么促成了
的问题?
上面的代码是好的,还是有任何潜在的问题?
谢谢!
Hello, I have a very simple question that I''m a bit confused about
right now. Is it OK to allocate memory by malloc() or calloc() for a
struct member and then call free() on it? For example, I have the code
below.
struct mystruct {
int a;
char *b;
int c;
};
struct mystruct myobject;
myobject.b = (char *)malloc(50);
Don''t cast the return from malloc. It doesn''t help and may cause the
compiler to suppress a diagnostic you really want to see....
...
free(myobject.b);
This is how you would avoid a memory leak. What prompted the
question?
Is the above code OK, or is there any potential problem with it?
Thanks!
<<删除电子邮件的del>>
<<Remove the del for email>>
>是什么促成了这个问题?
像我给的那个代码导致了崩溃。这就是我发布
问题的原因。我的主要观点是,如果在结构成员上完成分配和释放内存是否正常(我知道它没关系,但上面的崩溃使我成为了一个
位我认为这是一个编译问题,因为发布版本有效,而且b $ b发生了调试版本的崩溃(这很奇怪,因为它是因为它是混乱的)。
通常
反过来说。
> What prompted the question?
A code like the one I gave resulted in a crash. That''s why I posted the
question. My main point was if allocating and freeing memory is OK when
done on a struct member (I knew it''s OK, but the above crash made me a
bit confused).
I presume it''s a compilation problem since the release build works and
the crash happens for debug build (which is weird since it is usually
the other way round).
这篇关于malloc()用于struct成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!