分配多维结构数组 [英] Allocation of a multidimensional array of structures

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

问题描述




我最近遇到了我的程序问题,只能通过堆损坏来解释

,所以我试过了用Valgrind调试我的程序,

这里是我用下面的多维数组得到的

分配代码:


typedef struct

{

int32_t speed;

int16_t height;

} lut_entry;


lut_entry *** lut; //< -global pointer


void lut_alloc()

{

...

lut = malloc(LUT_HC * sizeof(lut_entry **));

for(ih = 0; ih< LUT_HC; ih ++)

{

lut [ih] = malloc(LUT_DC * sizeof(lut_entry *));

for(id = 0; id< LUT_DC; id ++)

lut [ih] [id] = calloc(LUT_AC,sizeof(lut_entry)); //< - line 1407

}

...

}


int main ()

{

...

lut_alloc(); //< - line 1580

...

}


********* Valgrind输出*********


== 6051 ==地址0x4FE8D5C是一个大小为1,152的块后的4个字节

alloc''d

== 6051 ==在0x401F95F:calloc(vg_replace_malloc.c:279)

== 6051 == by 0x804C587:lut_alloc(main.c:1407)

== 6051 == by 0x804CECB:main(main.c:1580)


多年来我一直以这种方式分配多维数组

(虽然直到现在还没有结构),我从来没有遇到过这样的问题

(虽然我之前从未用过Valgrind来验证)。有没有人

知道我的代码有什么问题?


提前致谢。

Hi,

I''ve recently met issues with my program which can only be explained
by heap corruption, so I''ve tried debugging my program with Valgrind,
and here''s what I get with the following multidimensional array
allocation code :

typedef struct
{
int32_t speed;
int16_t height;
} lut_entry;

lut_entry ***lut; // <-global pointer

void lut_alloc()
{
...
lut = malloc(LUT_HC * sizeof(lut_entry **));
for (ih=0; ih<LUT_HC; ih++)
{
lut[ih] = malloc(LUT_DC * sizeof(lut_entry *));
for (id=0; id<LUT_DC; id++)
lut[ih][id] = calloc(LUT_AC, sizeof(lut_entry)); // <- line 1407
}
...
}

int main()
{
...
lut_alloc(); // <- line 1580
...
}

*********Valgrind output*********

==6051== Address 0x4FE8D5C is 4 bytes after a block of size 1,152
alloc''d
==6051== at 0x401F95F: calloc (vg_replace_malloc.c:279)
==6051== by 0x804C587: lut_alloc (main.c:1407)
==6051== by 0x804CECB: main (main.c:1580)

I''ve been allocating multidimensional arrays this way for years
(although not with structs until now), and I''ve never had such issues
(although I never used Valgrind before to verify that). Would anyone
know what''s wrong with my code?

Thanks in advance.

推荐答案



" Michel Rouzic" < Mi ******** @ yahoo.frwrote in message

news:11 ********************* @ o61g2000hsh.googlegro ups.com ...

"Michel Rouzic" <Mi********@yahoo.frwrote in message
news:11*********************@o61g2000hsh.googlegro ups.com...




我最近遇到了我的程序问题只能通过堆损坏解释

,所以我尝试用Valgrind调试我的程序,

这里是我用下面的多维数组得到的< br $>
分配代码:


typedef struct

{

int32_t speed;

int16_t height;

} lut_entry;


lut_entry *** lut; //< -global pointer


void lut_alloc()

{

...

lut = malloc(LUT_HC * sizeof(lut_entry **));

for(ih = 0; ih< LUT_HC; ih ++)

{

lut [ih] = malloc(LUT_DC * sizeof(lut_entry *));

for(id = 0; id< LUT_DC; id ++)

lut [ih] [id] = calloc(LUT_AC,sizeof(lut_entry)); //< - line 1407

}

...

}


int main ()

{

...

lut_alloc(); //< - line 1580

...

}


********* Valgrind输出*********


== 6051 ==地址0x4FE8D5C是一个大小为1,152的块后的4个字节

alloc''d

== 6051 ==在0x401F95F:calloc(vg_replace_malloc.c:279)

== 6051 == by 0x804C587:lut_alloc(main.c:1407)

== 6051 == by 0x804CECB:main(main.c:1580)


多年来我一直以这种方式分配多维数组

(虽然直到现在还没有结构),我从来没有遇到过这样的问题

(虽然我之前从未用过Valgrind来验证)。有没有人

知道我的代码有什么问题?


提前致谢。
Hi,

I''ve recently met issues with my program which can only be explained
by heap corruption, so I''ve tried debugging my program with Valgrind,
and here''s what I get with the following multidimensional array
allocation code :

typedef struct
{
int32_t speed;
int16_t height;
} lut_entry;

lut_entry ***lut; // <-global pointer

void lut_alloc()
{
...
lut = malloc(LUT_HC * sizeof(lut_entry **));
for (ih=0; ih<LUT_HC; ih++)
{
lut[ih] = malloc(LUT_DC * sizeof(lut_entry *));
for (id=0; id<LUT_DC; id++)
lut[ih][id] = calloc(LUT_AC, sizeof(lut_entry)); // <- line 1407
}
...
}

int main()
{
...
lut_alloc(); // <- line 1580
...
}

*********Valgrind output*********

==6051== Address 0x4FE8D5C is 4 bytes after a block of size 1,152
alloc''d
==6051== at 0x401F95F: calloc (vg_replace_malloc.c:279)
==6051== by 0x804C587: lut_alloc (main.c:1407)
==6051== by 0x804CECB: main (main.c:1580)

I''ve been allocating multidimensional arrays this way for years
(although not with structs until now), and I''ve never had such issues
(although I never used Valgrind before to verify that). Would anyone
know what''s wrong with my code?

Thanks in advance.



你没有检查你的malloc()。 3D阵列即使对于小尺寸也是非常大的。

You have failed to check your malloc()s. 3D arrays get very big even for
small dimensions.


6月23日晚上8:02,Malcolm McLean ; < regniz ... @ btinternet.comwrote:
On Jun 23, 8:02 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:

" Michel Rouzic" < Michel0 ... @ yahoo.frwrote in message


news:11 ********************* @ o61g2000hsh .googlegro ups.com ...
"Michel Rouzic" <Michel0...@yahoo.frwrote in message

news:11*********************@o61g2000hsh.googlegro ups.com...

您好,
Hi,


我已经最近遇到了我的程序问题,这只能通过堆损坏来解释

,所以我尝试用Valgrind调试我的程序,

这里是我的得到以下多维数组

分配代码:
I''ve recently met issues with my program which can only be explained
by heap corruption, so I''ve tried debugging my program with Valgrind,
and here''s what I get with the following multidimensional array
allocation code :


typedef struct

{

int32_t speed;

int16_t height;

} lut_entry;
typedef struct
{
int32_t speed;
int16_t height;
} lut_entry;


lut_entry *** lut; //< -global pointer
lut_entry ***lut; // <-global pointer


void lut_alloc()

{

.. 。

lut = malloc(LUT_HC * sizeof(lut_entry **));

for(ih = 0; ih< LUT_HC; ih ++)

{

lut [ih] = malloc(LUT_DC * sizeof(lut_entry *));

for(id = 0; id< LUT_DC; id ++)

lut [ih] [id] = calloc(LUT_AC,sizeof(lut_entry)); //< - 第1407行

}

...

}
void lut_alloc()
{
...
lut = malloc(LUT_HC * sizeof(lut_entry **));
for (ih=0; ih<LUT_HC; ih++)
{
lut[ih] = malloc(LUT_DC * sizeof(lut_entry *));
for (id=0; id<LUT_DC; id++)
lut[ih][id] = calloc(LUT_AC, sizeof(lut_entry)); // <- line 1407
}
...
}


int main()

{

...

lut_alloc(); //< - line 1580

...

}
int main()
{
...
lut_alloc(); // <- line 1580
...
}


*** ****** Valgrind输出*********
*********Valgrind output*********


== 6051 ==地址0x4FE8D5C是一个块后的4个字节,尺寸1,152

alloc''d

== 6051 ==在0x401F95F:calloc(vg_replace_malloc.c:279)

== 6051 == by 0x804C587:lut_alloc(main.c:1407)

== 6051 == by 0x804CECB:main(main.c:1580)
==6051== Address 0x4FE8D5C is 4 bytes after a block of size 1,152
alloc''d
==6051== at 0x401F95F: calloc (vg_replace_malloc.c:279)
==6051== by 0x804C587: lut_alloc (main.c:1407)
==6051== by 0x804CECB: main (main.c:1580)


我多年来一直用这种方式分配多维数组

(虽然直到现在还没有结构),我从来没有遇到过这样的问题

(虽然我之前从未使用过Valgrind来验证)。有人会知道我的代码有什么问题吗?
I''ve been allocating multidimensional arrays this way for years
(although not with structs until now), and I''ve never had such issues
(although I never used Valgrind before to verify that). Would anyone
know what''s wrong with my code?


先谢谢。
Thanks in advance.



你没有检查你的malloc()。即使对于小尺寸,3D阵列也会变得非常大。


You have failed to check your malloc()s. 3D arrays get very big even for
small dimensions.



后来我通过检查所有我的NULL返回值来检查

fprintf',并且我没有NULL。如果我有NULL,不管怎样我的app sigsegv

?无论如何,我的阵列是仅。大约1.7 MB。

Later I checked by checking all my allocations for NULL returns with
fprintf''s, and I had no NULLs. If I had NULLs, wouldn''t my app sigsegv
anyways? Anyways, my array is "only" about 1.7 MB.


Malcolm McLean说:


< snip>
Malcolm McLean said:

<snip>

您未能检查malloc()。对于小尺寸,3D阵列甚至会变得非常大。
You have failed to check your malloc()s. 3D arrays get very big even
for small dimensions.



是的,当你的程序试图分配很多东西时,即使在有大量内存的系统上,malloc也会失败RAM + 1。

值得铭记的是,Malcolm。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Yes, malloc can fail, even on systems with a great deal of RAM
available, when your program tries to allocate a great deal of RAM + 1.
It''s worth bearing in mind, Malcolm.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


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

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