malloc的扼杀行为 [英] malloc's strang behavior

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

问题描述

大家好


我正在编写一个简单的代码来解决ACM问题

http://acm.uva.es ,问题是#468)。在其代码中我有以下片段的




freq = calcfreq(hashfreq,strfreq,input);

printf( 在malloc之前:%s(%p)\ n,输入+ INPUTLEN);

hchars =(char *)malloc(freq * sizeof(char));

schars =(char *)malloc(freq * sizeof(char));

printf(" malloc:%s \ n(%p)\ n",输入+ INPUTLEN);


由于输入与mallocs无关,我预计它将保持不变,但这并没有发生。 Suppouse输入指向

字符串xxxxyy,这段代码输出如下:


之前malloc:xxxxyy(0x8049ef0)
malloc之后
:xxxx(0x8049ef0)


任何人都知道这里会发生什么?

提前谢谢
< br $> b $ b -

Gustavo G. Rondina
http://gustgr.freeshell.org

Hi all

I''m writting a simple code to solve an ACM problem
(http://acm.uva.es, it is the problem #468). In its code I have the
following fragment:

freq = calcfreq(hashfreq, strfreq, input);
printf("before malloc: %s (%p)\n", input+INPUTLEN);
hchars = (char *)malloc(freq*sizeof(char));
schars = (char *)malloc(freq*sizeof(char));
printf("after malloc: %s\n (%p)\n", input+INPUTLEN);

Since input has nothing to do with the mallocs I expected it would be
unchanged but this isn''t happening. Suppouse input points to the
string "xxxxyy", this fragment of code outputs like this:

before malloc: xxxxyy (0x8049ef0)
after malloc: xxxx (0x8049ef0)

Anyone got a clue on what could be happening here?
Thanks in advance

--
Gustavo G. Rondina
http://gustgr.freeshell.org

推荐答案

Gustavo G. Rondina< gu **** @ brlivre.org> ;写道:
Gustavo G. Rondina <gu****@brlivre.org> writes:
大家好

我正在编写一个简单的代码来解决ACM问题
http://acm.uva.es ,问题是#468)。在其代码中我有以下片段:

freq = calcfreq(hashfreq,strfreq,input);
printf(" malloc之前:%s(%p)\\ \\ n",input + INPUTLEN);
hchars =(char *)malloc(freq * sizeof(char));
schars =(char *)malloc(freq * sizeof(char));
printf("在malloc之后:%s \ n(%p)\ n",输入+ INPUTLEN);

由于输入与我预期的mallocs无关保持不变,但这并没有发生。 Suppouse输入指向
字符串xxxxyy,这段代码输出如下:

在malloc:xxxxyy(0x8049ef0)
之后malloc:xxxx(0x8049ef0)

任何人都知道这里会发生什么?
Hi all

I''m writting a simple code to solve an ACM problem
(http://acm.uva.es, it is the problem #468). In its code I have the
following fragment:

freq = calcfreq(hashfreq, strfreq, input);
printf("before malloc: %s (%p)\n", input+INPUTLEN);
hchars = (char *)malloc(freq*sizeof(char));
schars = (char *)malloc(freq*sizeof(char));
printf("after malloc: %s\n (%p)\n", input+INPUTLEN);

Since input has nothing to do with the mallocs I expected it would be
unchanged but this isn''t happening. Suppouse input points to the
string "xxxxyy", this fragment of code outputs like this:

before malloc: xxxxyy (0x8049ef0)
after malloc: xxxx (0x8049ef0)

Anyone got a clue on what could be happening here?




在两个printf调用中,格式字符串都有一个%s。和一个%p

转换,但你只提供一个addtional参数而不是
两个。我不一定会期望这会导致你看到的特定问题

,但是在尝试进一步继续之前你肯定应该修复它。


转换malloc()的结果是不必要的,并且可以掩盖错误

,例如忘记#include< stdlib.h>。根据定义,sizeof(char)也是

总是1。这样两个任务就可以简化了。




hchars = malloc(freq);

schars = malloc(freq);


最后,如果您在做出这些更改后仍有疑问,请向我们展示一个小型,完整,可编辑的程序,展示

问题。我们看不到变量的声明,而且我们无法判断你是否包含了正确的标题。这使得

难以帮助你。 (还有一个相当不错的机会,你会自己解决这个问题,同时把它削减到足够小的东西上来。)

足以发布。)


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



In both printf calls, the format string has a "%s" and a "%p"
conversion, but you only provide one addtional argument rather than
two. I wouldn''t necessarily expect this to cause the specific problem
you''re seeing, but you should definitely fix it before attempting to
proceed any further.

Casting the result of malloc() is unnecessary, and can mask errors
such as forgetting the "#include <stdlib.h>". Also sizeof(char) is
always 1, by definition. Thus the two assignments can be simplified
to

hchars = malloc(freq);
schars = malloc(freq);

Finally, if you still have questions after making these changes,
please show us a small, complete, compilable program that exhibits the
problem. We can''t see the declarations of your variables, and we
can''t tell whether you''ve included the proper headers. That makes it
difficult to help you. (And there''s a fairly good chance that you''ll
solve the problem yourself while paring it down to something small
enough to post.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


2004年7月21日00:52:54 GMT,Gustavo G. Rondina< gu **** @ brlivre.org>

写道:
On 21 Jul 2004 00:52:54 GMT, Gustavo G. Rondina <gu****@brlivre.org>
wrote:
大家好

我正在编写一个简单的代码来解决ACM问题
(< a rel =nofollowhref =http://acm.uva.estarget =_ blank> http://acm.uva.es ,问题是#468)。在其代码中我有以下片段:

freq = calcfreq(hashfreq,strfreq,input);
printf(" malloc之前:%s(%p)\\ \\ n",input + INPUTLEN);
hchars =(char *)malloc(freq * sizeof(char));
schars =(char *)malloc(freq * sizeof(char));
printf("在malloc之后:%s \ n(%p)\ n",输入+ INPUTLEN);

由于输入与我预期的mallocs无关保持不变,但这并没有发生。 Suppouse输入指向
字符串xxxxyy,这段代码输出如下:

在malloc:xxxxyy(0x8049ef0)
之后malloc:xxxx(0x8049ef0)

任何人都知道这里会发生什么事吗?

提前谢谢
Hi all

I''m writting a simple code to solve an ACM problem
(http://acm.uva.es, it is the problem #468). In its code I have the
following fragment:

freq = calcfreq(hashfreq, strfreq, input);
printf("before malloc: %s (%p)\n", input+INPUTLEN);
hchars = (char *)malloc(freq*sizeof(char));
schars = (char *)malloc(freq*sizeof(char));
printf("after malloc: %s\n (%p)\n", input+INPUTLEN);

Since input has nothing to do with the mallocs I expected it would be
unchanged but this isn''t happening. Suppouse input points to the
string "xxxxyy", this fragment of code outputs like this:

before malloc: xxxxyy (0x8049ef0)
after malloc: xxxx (0x8049ef0)

Anyone got a clue on what could be happening here?
Thanks in advance




我放弃了。当你只有一个参数

跟随格式字符串时,如何在输出中格式化两个变量(

匹配两个格式说明符)?


为什么要从malloc转换回报?如果要使一个

编译器诊断关于将int转换为char *,那么你就可以通过不提供malloc的原型来调用未定义的行为。

如果要使编译器诊断关于将void *转换为

int *,那么你将它编译为C ++,这是错误的组。


由于sizeof(char)始终为1,因此您可以从

中删除malloc参数中的表达式。


您能否提供一个可展示的表达式的可编译示例行为?

<<删除电子邮件的del>>



I give up. How do you get two variables formatted in your output (to
match the two format specifiers) when you have only one argument
following the format string?

Why are you casting the return from malloc? If it is to silence a
compiler diagnostic about converting int to char* then you have
invoked undefined behavior by not providing the prototype for malloc.
If it is to silence a compiler diagnostic about converting void* to
int* then you are compiling it as C++ and this is the wrong group.

Since sizeof(char) is always 1, you can remove that expression from
the arguments to malloc.

Can you provide a compilable example that exhibits the behavior?
<<Remove the del for email>>


对不起家伙,复制并粘贴问题。这是正确的代码片段:


freq = calcfreq(hashfreq,strfreq,input);

printf("%s(%p)\ n",input + INPUTLEN,input + INPUTLEN);

hchars =(char *)malloc(freq * sizeof(char));

schars =(char *) malloc(freq * sizeof(char));

printf("%s(%p)\ n",输入+ INPUTLEN,输入+ INPUTLEN);


问题仍然存在。输出如下:


之前malloc:xxxxyy(0x8049ef0)

之后malloc:xxxx(0x8049ef0)


完整代码可在以下网址获得: http://www.brlivre.org/c /468.c


如果有空闲时间我会很感激帮助。

谢谢,

Gustavo


PS。这不是任何一种功课,我编码(至少我试着)作为一个

的爱好。
Sorry guys, copy and paste problem. Here is the correct code fragment:

freq = calcfreq(hashfreq, strfreq, input);
printf("%s (%p)\n", input+INPUTLEN, input+INPUTLEN);
hchars = (char *)malloc(freq*sizeof(char));
schars = (char *)malloc(freq*sizeof(char));
printf("%s (%p)\n", input+INPUTLEN, input+INPUTLEN);

The problem it is still there. The output follows:

before malloc: xxxxyy (0x8049ef0)
after malloc: xxxx (0x8049ef0)

The full code is avaliable at: http://www.brlivre.org/c/468.c

If anyone have spare time I would appreciate some help.
Thanks,
Gustavo

PS. this is not any kind of homework, I code (at least I try to) as a
hobby.


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

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