关于malloc()的奇怪之处 [英] Strange thing about malloc()

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

问题描述

嗨!


我正在编写一个程序,它将从文件中读取二进制整数,将它们放入动态数组中。首先声明数组


int * a = malloc(sizeof(int)* N)< - N是插槽的数量。


当N为奇数时,程序运行正常,但是当N为偶数时,它将无法工作。


它只是一个Win32-issue,还是一个bug?


-


问候,


Ronny Mandal

这封电子邮件和任何附件都是保密的,可能是特权或

,否则不会被披露。它仅供上述人员使用。
。如果您不是预期的收件人,则严禁任何阅读,使用,b / b
披露,复制或分发本电子邮件的全部或部分内容或

相关附件。如果您不是预定的

收件人,请立即回复此邮件

或通过电话通知发件人并永久删除此电子邮件和任何附件

你的系统。

Hi!

I am writing a program that''ll read binary integers from a file, put them
into a dynamic array. THe array is first declared

int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.

When N is odd, the program will work just fine, however when N is even, it
will not work.

Is it just a Win32-issue, or a bug?

--

Regards,

Ronny Mandal
This e-mail and any attachment are confidential and may be privileged or
otherwise protected from disclosure. It is solely intended for the person(s)
named above. If you are not the intended recipient, any reading, use,
disclosure, copying or distribution of all or parts of this e-mail or
associated attachments is strictly prohibited. If you are not an intended
recipient, please notify the sender immediately by replying to this message
or by telephone and delete this e-mail and any attachments permanently from
your system.

推荐答案

>这封电子邮件和任何附件都是保密的,可能是特权或
>This e-mail and any attachment are confidential and may be privileged or
否则不受披露。它仅适用于上述人员。如果您不是预期的收件人,则严禁阅读,使用,披露,复制或分发本电子邮件或相关附件的全部或部分内容。如果您不是预定的
收件人,请立即回复此邮件或通过电话通知发件人,并从您的系统中永久删除此电子邮件和任何附件。


把它放在全球的USENET帖子上是绝对荒谬的。

不要威胁你想帮助你的人。

我正在编写一个程序,它将从文件中读取二进制整数,将它们放入动态数组中。首先声明数组

int * a = malloc(sizeof(int)* N)< - N是插槽的数量。

当N为奇数时,程序运行得很好,但是当N是偶数时,它将不起作用。
otherwise protected from disclosure. It is solely intended for the person(s)
named above. If you are not the intended recipient, any reading, use,
disclosure, copying or distribution of all or parts of this e-mail or
associated attachments is strictly prohibited. If you are not an intended
recipient, please notify the sender immediately by replying to this message
or by telephone and delete this e-mail and any attachments permanently from
your system.
It''s absolutely ridiculous to put this on a worldwide USENET posting.
Don''t threaten people you want to help you.
I am writing a program that''ll read binary integers from a file, put them
into a dynamic array. THe array is first declared

int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.

When N is odd, the program will work just fine, however when N is even, it
will not work.




描述如何分辨工作与工作之间的区别。有些代码会很好。


我怀疑你是在阵列末尾运行的,而且那个

你可以用一个奇数来逃避它(可能是因为malloc()

四舍五入),但没有一个偶数(当它可能没有时)

四舍五入)。但没有代码就没有办法告诉。


Gordon L. Burditt



Describe how you can tell the difference between "works" and "doesn''t work".
Some code would be nice.

I suspect that you are running over the end of the array, and that
you can get away with it with an odd number (possibly because malloc()
rounds up the size), but not with an even number (when it might not
round up). But there''s no way to tell without code.

Gordon L. Burditt


Ronny Mandal于19/04年写道/ 05:
Ronny Mandal wrote on 19/04/05 :
我正在编写一个程序,它将从文件中读取二进制整数,将它们放入动态数组中。首先声明数组

int * a = malloc(sizeof(int)* N)< - N是插槽的数量。

当N为奇数时,该程序将正常工作,但是当N是偶数时,它将无法工作。

它只是一个Win32问题,还是一个bug?
I am writing a program that''ll read binary integers from a file, put them
into a dynamic array. THe array is first declared

int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.

When N is odd, the program will work just fine, however when N is even, it
will not work.

Is it just a Win32-issue, or a bug?




很难说。你的意思是它不起作用。 ?发布一个

可编辑片段,用于描述

环境(所需输入,预期输出......)的公开问题


-

Emmanuel

C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

C库: http://www.dinkumware.com/refxc.html

C是一个锋利的工具



Hard to say. What do you meant by "it will not work." ? Post a
compilable snippet that exposes the problem with a description of the
environment (required inputs, expected outputs...)

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"





Ronny Mandal写道:


Ronny Mandal wrote:
嗨!

我正在编写一个程序,它将从文件中读取二进制整数,将它们放入动态数组中。首先声明数组

int * a = malloc(sizeof(int)* N)< - N是插槽的数量。

当N为奇数时,该程序将正常工作,但是当N是偶数时,它将无法工作。

它只是一个Win32问题,还是一个bug?
Hi!

I am writing a program that''ll read binary integers from a file, put them
into a dynamic array. THe array is first declared

int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.

When N is odd, the program will work just fine, however when N is even, it
will not work.

Is it just a Win32-issue, or a bug?




后者。我的水晶球告诉我你的错误

在第42行。


医生,它很疼!


伤害了什么?


你认为你在做什么,窥探我的个人事务?


-
Er ******** *@sun.com



The latter. My crystal ball tells me your error
is on line 42.

"Doctor, it hurts!"

"What hurts?"

"What do you think you''re doing, snooping in my
personal affairs?"

--
Er*********@sun.com


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

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