为struct分配内存 - 什么时候? [英] Allocating memory for struct - when?

查看:76
本文介绍了为struct分配内存 - 什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用一些结构编写一个程序,它没有运行,而且我认为这是因为有一些内存泄漏 - 调试器告诉我

代码导致问题出在malloc函数中。

是否有任何通用规则告诉我何时分配内存?

我认为如果它是一个不是指针的变量我就不必要了,而且如果它是的话,我必须付出


我是特别是对阵列有点困惑。

在正常情况下,内存是为数组[10]分配的,而我没有这样做的
,但如果我想要使用指针使得数组可以动态,我将使用*数组并分配我想要的内存量,

就像array = malloc(sizeof( data_type)* number_of_item),对吗?

如果我正在存储一个字符串 (字符数组)在一个结构中,会发生什么。

我将其声明为:


typedef struct {

void * obj ;

char key [];

} HashTableEntry;


我做的初始化对象是:


HashTableEntry * hte = malloc(sizeof(HashTableEntry));

//" key"是一个已定义的字符数组

strcpy(hte-> key,key);

hte-> obj = NULL;


程序如何知道需要为

HashTableEntry分配多少内存?看起来它是动态大小的,它取决于

char数组键。

还有一件事是,我不能改变两个成员的顺序,如果

我这样做,程序不会编译。我上面说过的

问题之间是否有任何联系?

全部谢谢!

Hi all,
I am writing a program using some structs, it is not running and I
believe it is because there''s some memory leak - the debugger tells me
that the code causes the problem is in the malloc function.
Is there any general rules that tell me when to allocate memory?
I thought I don''t have to if it is a variable that''s not a pointer, and
I have to if it is.
I am a bit confused about the arrays particularly.
In normal situations, memory is allocated for array[10] and I don''t have
to do so, but if I want to use a pointer so that the array can be
dynamic, I shall use *array and allocate the amount of memory I want,
like array = malloc(sizeof(data_type) * number_of_item), right?
And if I am storing a "string" (char array) in a struct, what happens.
I declared it as:

typedef struct {
void *obj;
char key[];
} HashTableEntry;

What I do to initialize the object is:

HashTableEntry *hte = malloc(sizeof(HashTableEntry));
// "key" is a defined char array
strcpy(hte->key, key);
hte->obj = NULL;

How do the program know how much memory have to be allocated for the
HashTableEntry? It seems that it is dynamically sized, it depends on the
char array key.
And one more thing is, I can''t reverse the order of the two members, if
I do so, the program does not compile. Is there any link between the
problem I have said above?
Thanks all!

推荐答案

fix写道:
大家好,
我正在编写一个程序,使用一些结构,它没有运行,我相信它是因为那里'''一些内存泄漏 - 调试器告诉我
代码导致问题出在malloc函数中。
是否有任何通用规则告诉我何时分配内存?
我以为我不知道如果它是一个'不是指针的变量',我必须这样做。
如果是的话,我必须这样做。
我特别对阵列感到困惑。
正常情况下情况,内存是为数组[10]分配的,我没有这样做,但如果我想使用指针使数组可以动态,我将使用*数组并分配我想要的内存量,
像array = malloc(sizeof(data_type)* number_of_item),对吗?
如果我存储一个& QUOT;串QUOT; (字符数组)在一个结构中,会发生什么。
我将其声明为:

typedef struct {
void * obj;
char key [];
这是什么???

} HashTableEntry;

我在初始化对象时所做的是:

HashTableEntry * hte = malloc (sizeof(HashTableEntry));
//" key"是一个已定义的char数组
strcpy(hte-> key,key);
Hi all,
I am writing a program using some structs, it is not running and I
believe it is because there''s some memory leak - the debugger tells me
that the code causes the problem is in the malloc function.
Is there any general rules that tell me when to allocate memory?
I thought I don''t have to if it is a variable that''s not a pointer, and
I have to if it is.
I am a bit confused about the arrays particularly.
In normal situations, memory is allocated for array[10] and I don''t have
to do so, but if I want to use a pointer so that the array can be
dynamic, I shall use *array and allocate the amount of memory I want,
like array = malloc(sizeof(data_type) * number_of_item), right?
And if I am storing a "string" (char array) in a struct, what happens.
I declared it as:

typedef struct {
void *obj;
char key[]; Whatz this ???
} HashTableEntry;

What I do to initialize the object is:

HashTableEntry *hte = malloc(sizeof(HashTableEntry));
// "key" is a defined char array
strcpy(hte->key, key);




这与之前提到的问题1直接相关。你没有为字段分配内存 - ''key''。试试这个:

typedef struct {

void * obj;

char * key;

} HashTableEntry;


HashTableEntry * hte = malloc(sizeof(HashTableEntry));

//" key"是一个已定义的char数组

key = malloc(sizeof(char)* MAX_CHAR_LEN);

strcpy(hte-> key,key);

hte-> obj = NULL;

这应该有帮助..

删除对象时不要忘记删除指针。

免费(hte); //内存泄漏

免费(obj);

免费(密钥);

免费(hte);

//很酷。但你仍然需要确保

字段确实被释放...


HTH

-

Karthik

人类请''removeme_''查看我的真实电子邮件。



This is directly related to the problem 1 mentioned before. You are
not allocating memory for the field - ''key'' . Try this :
typedef struct {
void *obj;
char * key;
} HashTableEntry;

HashTableEntry *hte = malloc(sizeof(HashTableEntry));
// "key" is a defined char array
key = malloc(sizeof(char) * MAX_CHAR_LEN);
strcpy(hte->key, key);
hte->obj = NULL;
This should help ..
Dont forget to delete the pointers when deleting the object.
free(hte) ; // Memory leak
free(obj);
free(key);
free(hte);
// Pretty cool . But you still need to make sure that indeed the
fields were allocated to be freed...

HTH
--
Karthik
Humans please ''removeme_'' for my real email.


Karthik写道:
Karthik wrote:
fix写道:
fix wrote:
大家好,
我正在编写一个程序使用一些结构,它没有运行而且我
相信这是因为有一些内存泄漏 - 调试器告诉我
代码导致问题出在malloc函数中。
是否有任何通用规则告诉我何时分配内存? br />我想如果它是一个不是指针的变量我就不必要了,如果是的话我必须这样做。
我对阵列有点困惑。
在正常情况下,内存是为数组[10]分配的,我不必这样做,但如果我想使用指针使数组可以是
动态,我hall使用*数组并分配我想要的内存量,
像array = malloc(sizeof(data_type)* number_of_item),对吗?
如果我正在存储一个字符串 (字符数组)在一个结构中,会发生什么。
我将其声明为:

typedef struct {
void * obj;
char key [];
Hi all,
I am writing a program using some structs, it is not running and I
believe it is because there''s some memory leak - the debugger tells me
that the code causes the problem is in the malloc function.
Is there any general rules that tell me when to allocate memory?
I thought I don''t have to if it is a variable that''s not a pointer,
and I have to if it is.
I am a bit confused about the arrays particularly.
In normal situations, memory is allocated for array[10] and I don''t
have to do so, but if I want to use a pointer so that the array can be
dynamic, I shall use *array and allocate the amount of memory I want,
like array = malloc(sizeof(data_type) * number_of_item), right?
And if I am storing a "string" (char array) in a struct, what happens.
I declared it as:

typedef struct {
void *obj;
char key[];
Whatz this ???
} HashTableEntry;

我做的初始化对象是:

HashTableEntry * hte = malloc(sizeof(HashTableEntry));
//" key"是一个已定义的char数组
strcpy(hte-> key,key);
} HashTableEntry;

What I do to initialize the object is:

HashTableEntry *hte = malloc(sizeof(HashTableEntry));
// "key" is a defined char array
strcpy(hte->key, key);



这与前面提到的问题1直接相关。你没有为字段分配内存 - ''key''。试试这个:

typedef struct {
void * obj;
char * key;
} HashTableEntry;

HashTableEntry * hte = malloc (sizeof(HashTableEntry));
//" key"是一个已定义的char数组
key = malloc(sizeof(char)* MAX_CHAR_LEN);


This is directly related to the problem 1 mentioned before. You are
not allocating memory for the field - ''key'' . Try this :
typedef struct {
void *obj;
char * key;
} HashTableEntry;

HashTableEntry *hte = malloc(sizeof(HashTableEntry));
// "key" is a defined char array
key = malloc(sizeof(char) * MAX_CHAR_LEN);



相反 - 更好 -

key = malloc(sizeof (char)*(strlen(key)+ 1));

//记住''\ 0''char

strcpy(hte-> key, key);
hte-> obj = NULL;

这应该有帮助..
删除对象时别忘了删除指针。

免费(hte); //内存泄漏

免费(obj);
免费(密钥);
免费(hte);
//非常酷。但是你仍然需要确保
字段确实被释放...

HTH


Instead - better -
key = malloc(sizeof(char) * (strlen(key) + 1 ));
// Remember the ''\0'' char
strcpy(hte->key, key);
hte->obj = NULL;
This should help ..
Dont forget to delete the pointers when deleting the object.
free(hte) ; // Memory leak
free(obj);
free(key);
free(hte);
// Pretty cool . But you still need to make sure that indeed the
fields were allocated to be freed...

HTH



-

Karthik

Humans please''removeme_''我的真实电子邮件。


--
Karthik
Humans please ''removeme_'' for my real email.




"固定" < fi*@here.com>一个écritdansle message de

news:c7 ********** @ news.tamu.edu ...

"fix" <fi*@here.com> a écrit dans le message de
news:c7**********@news.tamu.edu...
大家好,





[snipped]

typedef struct {
void * obj;
char键[];


key目前是一个数组元素数量未知的数组,它是一个不完整类型(比如灵活数组)的
。你可以在这里做的事情是确定你想要的元素数量,$ key $ 50;例如(一个

#define指令也可能有帮助),或使用指针。

} HashTableEntry;

我做什么来初始化对象是:
HashTableEntry * hte = malloc(sizeof(HashTableEntry));


/ * hte tot test * /

if(hte!= NULL)

{

//key是一个已定义的char数组
strcpy(hte-> key,key);


假设hte->键现在有一个完整类型,其中包含上述

之类的已知大小,使用strncpy()而不是strcpy()将阻止在外面写字

hte->键阵列的界限:


/ * 50-1写入至少一个空终止字符hte->键,只需

49个字符

来自密钥被复制到hte-> key * /

strncpy(hte-> ; key,key,50-1);

hte-> obj = NULL;

程序如何知道必须为 HashTableEntry?它似乎是动态大小的,它取决于
char数组键。


你的结构HashTableEntry包含一个指向void元素的指针,

仍然假设结构中的成员键现在大小为50,50 char

连续元素。编译器知道它需要存储的空间这样的东西(IOW,他知道需要多少字节来存储一个

void元素的地址和50个字符。 ,编译器在你的结构成员之间添加填充位

(不是在第一个成员之前,这里是

obj)来正确对齐内存中的数据。

还有一件事是,我不能颠倒两个成员的顺序,如果我这样做,程序就不会编译。
问题之间是否有任何联系我上面说过了吗?
Hi all,
Hi,

[snipped]
typedef struct {
void *obj;
char key[];
key is for the moment an array whose the number of elements is unknown, it''s
an incomplete type (say, flexible array). A thing you can do here is to
precise the number of elements you want, char key[50]; for example ( a
#define directive may also help), or use a pointer.
} HashTableEntry;

What I do to initialize the object is:
HashTableEntry *hte = malloc(sizeof(HashTableEntry));
/* hte tot test */
if (hte != NULL)
{
// "key" is a defined char array
strcpy(hte->key, key);
Assuming hte->key has now a complete type with a well-known size like said
above, using strncpy() instead of strcpy() will prevent of writing outside
the bounds of the hte->key array:

/* 50-1 to write at least one null terminating character in hte->key, only
49 characters
from key are copied into hte->key */
strncpy(hte->key, key, 50-1);
hte->obj = NULL;

How do the program know how much memory have to be allocated for the
HashTableEntry? It seems that it is dynamically sized, it depends on the
char array key.
Your structure HashTableEntry contains a pointer to a void element and,
still assuming the member key in the structure is now sized to 50, 50 char
contiguous elements. The compiler knows the space it needs to store such
things (IOW, he knows how many bytes are necessary to store an adress of a
void element and 50 characters. Furthermore, the compiler adds padding bits
between the members of your structure (not before the first member, here
obj) to correctly align the data in memory.
And one more thing is, I can''t reverse the order of the two members, if
I do so, the program does not compile. Is there any link between the
problem I have said above?




是的,因为关键成员是一个不完整的类型(请记住,灵活的数组)

并首先放在结构中,编译器无法确定密钥的大小和他将存储obj的位置。如果密钥位于最后的

位置,则有意义:我不知道不知道

编译器使用的真实策略,但它可以将key视为指向char的指针。

Regis



Yes, since the key member was an incomplete type (remember, flexible array)
and placed first in the structure, the compiler was unable to determine the
size of key and where he would have stored obj. If key is placed in the last
position, it makes sense : I don''t know the real policy used by the
compiler, but it can view key as a pointer to a char.
Regis


这篇关于为struct分配内存 - 什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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