结构和malloc [英] structs and malloc

查看:92
本文介绍了结构和malloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类型和结构定义


typedef struct hNode {

unsigned char name [1024];

unsigned long hVal;

struct hNode * next;

} HashNode;


我想要的是确保在做什么时:


HashNode aNode = malloc(sizeof(HashNode));


i可以确定next的值在aNode中(例如,稍后在

程序中,我想比较aNode中next的值,看它是否已将
链接到另一个HashNode) ---我能用一些简单的方法吗?或者我需要编写自己的内存分配器???。

谢谢。

Hi, i have the following type and struct defined

typedef struct hNode {
unsigned char name[1024];
unsigned long hVal;
struct hNode *next;
} HashNode;

what i want, is to ensure that when doing:

HashNode aNode = malloc(sizeof(HashNode));

i can be sure of the value of "next" in the aNode (eg. later in the
program i want to compare the value of "next" in aNode to see if it has
been linked to another HashNode) --- can i do this in
some easy way or do i need to write my own memory allocator ???.
Thanks.

推荐答案

开始跟进Lars Tackmann:
begin followup to Lars Tackmann:
我想要的是确保在做的时候:

HashNode aNode =的malloc(的sizeof(HashNode));
^

这里缺少一些东西。
我可以肯定下一个的价值。在aNode


嗯,您可以使用calloc而不是malloc将

分配的内存的所有字节设置为零。在大多数平台上,你会得到一个

空指针(并且在使用不同表示的那个上面)
你会得到一个可怕的,难以追踪的运行时错误)。

[...]我可以用一些简单的方法做到这一点,还是我需要编写自己的内存分配器???。
what i want, is to ensure that when doing:

HashNode aNode = malloc(sizeof(HashNode)); ^
Something is missing here.
i can be sure of the value of "next" in the aNode
Well, you can use calloc instead of malloc to set all bytes of the
allocated memory to zero. On most platforms that will get you a
null-pointer (and on the one that uses a different representation
you will get a horrible, hard to track runtime error).
[...] can i do this in some easy way or do i need to write my own
memory allocator ???.



嗯,你需要做两件事:


HashNode * aNode = malloc(sizeof(* aNode));

aNode-> ; next = 0;


如果你想把这两行分成一个函数,请跟我好。


HashNode * new_HashNode(void )

{

HashNode * aNode = malloc(sizeof(* aNode));

aNode-> next = 0;

返回aNode;

}


-
$ b $bFürGoogle,Tux和GPL!



Well, you need to do two things:

HashNode* aNode = malloc(sizeof(*aNode));
aNode->next = 0;

If you want to group these two lines into one function, fine with me.

HashNode* new_HashNode(void)
{
HashNode* aNode = malloc(sizeof(*aNode));
aNode->next = 0;
return aNode;
}

--
Für Google, Tux und GPL!


Alexander Bartolich写道:
Alexander Bartolich wrote:
开始跟进Lars Tackmann:
begin followup to Lars Tackmann:
我想要的是确保做:

HashNode aNode = malloc(sizeof(HashNode)); ^
这里缺少一些东西。
what i want, is to ensure that when doing:

HashNode aNode = malloc(sizeof(HashNode)); ^
Something is missing here.

我可以确定next的价值。在aNode中

i can be sure of the value of "next" in the aNode



嗯,您可以使用calloc而不是malloc将分配的内存的所有字节设置为零。在大多数平台上,你会得到一个空指针(在使用不同表示的那个平台上)你会得到一个可怕的,难以追踪的运行时错误。



Well, you can use calloc instead of malloc to set all bytes of the
allocated memory to zero. On most platforms that will get you a
null-pointer (and on the one that uses a different representation
you will get a horrible, hard to track runtime error).

[...]我能以一些简单的方式做到这一点,还是我需要编写自己的内存分配器???。
[...] can i do this in some easy way or do i need to write my own
memory allocator ???.



好吧,你需要做两件事:

HashNode * aNode = malloc(sizeof(* aNode));
aNode-> next = 0;

如果你想要将这两行组合成一个函数,对我来说很好。

HashNode * new_HashNode(void)
{/ / HashNode * aNode = malloc(sizeof(* aNode));
aNode-> next = 0;



Well, you need to do two things:

HashNode* aNode = malloc(sizeof(*aNode));
aNode->next = 0;

If you want to group these two lines into one function, fine with me.

HashNode* new_HashNode(void)
{
HashNode* aNode = malloc(sizeof(*aNode));
aNode->next = 0;




ITYM:

if(aNode)aNode-> next = 0;

返回aNode;
}



ITYM:
if ( aNode ) aNode->next = 0;
return aNode;
}




-nrk。



-nrk.


I认为我的例子很糟糕 - 我需要一个能够分配整个

数组的哈希节点,并确保所有节点都有一个合理的值

从一开始。


例如。


HashNode * hashTable = malloc(sizeof(hashNode)* 100);


谢谢

I think that my example was bad - i need a to be able to allocate a entire
array of hash nodes and be sure that all of the nodes has a sane value
from the start.

eg.

HashNode *hashTable = malloc(sizeof(hashNode) * 100);

Thanks


这篇关于结构和malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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