malloc stumper [英] malloc stumper

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

问题描述



假设我有一些自定义类型foo,我知道定义为

a指向某些东西的指针,但我不知道是什么什么"是。

我想使用malloc为

foo类型的变量分配一些空间来指向。即我想使用malloc为

asome分配空间。并将malloc的返回值赋给一个变量

类型foo。但要做到这一点,malloc需要知道

某事的大小,而且我很难过。是否有任何方法可以使代码确定某事物的大小。来自foo?


谢谢,


jill


-

to s& e ^ n] d me m~a} i lr%e * m?o\v [有点来自我的| d)d:r {e:s] s。


Suppose that I have some custom type foo that I know is defined as
a "pointer to something", but I don''t know what "something" is.
I want to use malloc to allocate some space for a variable of type
foo to point to. I.e. I want to use malloc to allocate space for
a "something" and assign malloc''s return value to a variable of
type foo. But to do this, malloc needs to know the size of a
"something", and I''m stumped. Is there any way for the code to
determine the size of a "something" from foo?

Thanks,

jill

--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

推荐答案

>假设我有一些自定义类型foo,我知道它被定义为
>Suppose that I have some custom type foo that I know is defined as
a指向什么,但我不知道什么是某事是。


如果你不知道什么是某事是,你怎么声明foo?

指向什么东西? void *?

我想使用malloc为
foo类型的变量分配一些空间来指向。即我想使用malloc为
分配空间某事。并将malloc的返回值赋给
类型为foo的变量。但要做到这一点,malloc需要知道某事的大小,我很难过。有没有办法让代码确定某事物的大小?来自foo?
a "pointer to something", but I don''t know what "something" is.
If you don''t know what "something" is, how did you declare foo?
pointer to something? void *?
I want to use malloc to allocate some space for a variable of type
foo to point to. I.e. I want to use malloc to allocate space for
a "something" and assign malloc''s return value to a variable of
type foo. But to do this, malloc needs to know the size of a
"something", and I''m stumped. Is there any way for the code to
determine the size of a "something" from foo?




如果foo被声明为指向某事物的指针,你可以使用:


foo = malloc(sizeof (* foo));


如果foo是void *或其他指针类型,这种方法将不起作用。


Gordon L. Burditt



If foo is declared as a pointer to something, you can use:

foo = malloc(sizeof(*foo));

If foo is a void * or other pointer type, this approach will not work.

Gordon L. Burditt




" J Krugman" < JK ********* @ yahbitoo.com>写了

"J Krugman" <jk*********@yahbitoo.com> wrote
假设我有一些自定义类型foo,我知道它被定义为指向某个东西的指针,但我不知道什么是某事。是。
我想使用malloc为
foo类型的变量分配一些空间来指向。
Suppose that I have some custom type foo that I know is defined as
a "pointer to something", but I don''t know what "something" is.
I want to use malloc to allocate some space for a variable of type
foo to point to.




typedef struct

{

int x;

} foo;


....

foo * fooptr = malloc(N * sizeof(foo));

C没有的是类型变量,它包含一个在运行时确定的类型
。但是你通常可以使用

组合void * s和尺寸。假设我们想在

哈希表中存储一些foos。


typedef struct

{

size_t elsize;

void *条目;

} HASH;


HASH hashable(size_t element_size)

{

entries = malloc(element_size * TABLESIZE);

elsize = element_size;

}


void store_entry(HASH * h,void * data,int(* hash)(void * data))

{

int slot = hash (数据);

/ *我们需要一点复杂性以确保插槽是免费的,将滑过

这个* /

无符号char * ptr = h->条目;

memcpy(ptr + slot * h-> elsize,data,elsize);

}



typedef struct
{
int x;
}foo;

....
foo *fooptr = malloc(N * sizeof(foo));
What C does not have is a "type" variable, that holds a type to be
determined at runtime. However you can usually get round this with a
combination of void *s and sizes. Let''s say we want to store some foos in a
hashtable.

typedef struct
{
size_t elsize;
void *entries;
} HASH;

HASH hashable(size_t element_size)
{
entries = malloc(element_size * TABLESIZE);
elsize = element_size;
}

void store_entry(HASH *h, void *data, int (*hash)(void *data))
{
int slot = hash(data);
/* we need a bit of complexity to make sure slot is free, will skate over
this */
unsigned char *ptr = h->entries;
memcpy(ptr + slot * h->elsize, data, elsize);
}


2004年12月4日星期六16:26:30 +0000(UTC)

J Krugman< jk ********* @ yahbitoo。 COM>写道:
On Sat, 4 Dec 2004 16:26:30 +0000 (UTC)
J Krugman <jk*********@yahbitoo.com> wrote:
假设我有一些自定义类型foo,我知道它被定义为
一个指向某个东西的指针,但我不知道是什么什么"是。
我想使用malloc为
foo类型的变量分配一些空间来指向。即我想使用malloc为
分配空间某事。并将malloc的返回值赋给
类型为foo的变量。但要做到这一点,malloc需要知道某事的大小,我很难过。有没有办法让代码确定某事物的大小?来自foo?
Suppose that I have some custom type foo that I know is defined as
a "pointer to something", but I don''t know what "something" is.
I want to use malloc to allocate some space for a variable of type
foo to point to. I.e. I want to use malloc to allocate space for
a "something" and assign malloc''s return value to a variable of
type foo. But to do this, malloc needs to know the size of a
"something", and I''m stumped. Is there any way for the code to
determine the size of a "something" from foo?




foo * bar = malloc(n * sizeof * bar);


将为n分配空间bar类型的项目指向。这是

也很容易维护,因为你可以有

foo * bar;

/ *很多代码* /

bar = malloc(n * sizeof * bar);

你不必找到定义栏的位置,看看malloc

是否足够分配空间。

-

Flash Gordon

生活在有趣的时代。

虽然我的电子邮件地址说垃圾邮件,它是真实的,我读了它。



foo *bar = malloc( n * sizeof *bar);

The will allocate space for n items of the type bar points to. It is
also easy to maintain since you could have
foo *bar;
/* lots of code */
bar = malloc( n * sizeof *bar);
and you don''t have to find where bar is defined to see if the malloc
allocates enough space.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


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

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