熟食程序的问题很少 [英] Little problem with a delirant program
问题描述
#include< string.h>
#include< stdlib.h>
#include< stdio.h>
char * _(char * str1,char * str2)
{
char * res;
res = malloc((strlen(str1) )+ strlen(str2))* sizeof(char));
strcpy(res,str1);
strcpy(res + strlen(str1),str2);
返回res;
}
int main(无效)
{
char a [] =" \ nHey man看着我摇摇晃晃地''现在''我在'&;;
char b [] =" Danny和Lisa" ;;
char c [] =" \ n他们带我离开';
char * d = _(_(a," radio"), _(a," video"));
char * e = _(d,_(&with; with,b));
char * g = _(\ n最奇怪的地方\ nSweet,b);
char * h = _(c,g);
char * i = _(_(h,c),_(h,c));
char * j = _(e,i);
char * k = _( _(d,j),_(j,e));
char * l = _(_(k,h),_(d,e));
puts(++ l); / * ++ l摆脱了
开头的虚假换行符* /
返回0;
}
为什么这个程序在合唱的最后一次
行发生后会添加一个虚假的''1'? (即我正和Danny和Lisa1合作拍摄视频)。
(我说它很耐心......但我担心因版权问题我不会这样做/>
能够将此修改汇总到下一个IOCCC ......)
TIA
-
#include< stdio.h>
main()
{
printf(" \\\
\\\
-- \ nArmy1987");
}
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char *_(char *str1, char *str2)
{
char *res;
res = malloc((strlen(str1)+strlen(str2))*sizeof(char));
strcpy(res, str1);
strcpy(res+strlen(str1), str2);
return res;
}
int main(void)
{
char a[] = "\nHey man look at me rockin'' now\nI''m on the ";
char b[] = "Danny and Lisa";
char c[] = "\nThey take me away from";
char *d = _(_(a,"radio"),_(a,"video"));
char *e = _(d,_(" with ",b));
char *g = _("\nThe strangest places\nSweet ",b);
char *h = _(c,g);
char *i = _(_(h,c),_(h,c));
char *j = _(e,i);
char *k = _(_(d,j),_(j,e));
char *l = _(_(k,h),_(d,e));
puts(++l); /* ++l gets rid of the spurious newline character at the
beginning */
return 0;
}
Why does this program add a spurious ''1'' after each occurrence of the last
line of the chorus? (i.e. "I''m on the video with Danny and Lisa1").
(I said it was delirant... But I fear that for copyright problems I won''t be
able to sumbit a modification of this to the next IOCCC...)
T.I.A.
--
#include <stdio.h>
main()
{
printf("\n\n--\nArmy1987");
}
推荐答案
Army1987写道:
Army1987 wrote:
#include< string.h>
#include< stdlib.h>
#include< stdio.h>
char * _(char * str1,char * str2)
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char *_(char *str1, char *str2)
不要这样做。以_开头的标识符是
保留。有一些情况下它们不是保留的,但这不是其中之一。
Don''t do that. Identifiers beginning with `_'' are
reserved. There are a few contexts in which they are not
reserved, but this isn''t one of them.
{
char * res;
res = malloc((strlen(str1)+ strlen(str2))* sizeof(char));
strcpy(res ,str1);
strcpy(res + strlen(str1),str2);
{
char *res;
res = malloc((strlen(str1)+strlen(str2))*sizeof(char));
strcpy(res, str1);
strcpy(res+strlen(str1), str2);
这可能是你的程序失控的地方;
如果str2长度为零,也许早一行。我认为
这个问题将在常见问题解答中介绍 - 但也许它只是一个经常犯错误而不是经常犯下的错误
b问的问题,因为问的问题暗示了这个问题的意识。
无论如何,直接的错误是一个长度为
是N个字符需要N + 1个字节的存储空间。
This is probably where your program goes off the rails;
perhaps one line earlier if str2 has zero length. I thought
this matter would be covered in the FAQ -- but perhaps it''s
just a Frequently Committed Error rather than a Frequently
Asked Question, since the very asking of the question implies
awareness of the issue.
Anyhow, the immediate error is that a string whose length
is N characters requires N+1 bytes of storage.
返回res;
}
int main(无效)
{
char a [] =" \ nHey man看着我摇摇晃晃''now'\
I ''on the ;;
char b [] =" Danny and Lisa" ;;
char c [] =" \ n他们带我离开;;
char * d = _(_(a," radio"),_(a," video"));
char * e = _ (d,_(" with,b));
char * g = _(" \\\
最奇怪的地方\ nSweet",b);
char * h = _(c,g);
char * i = _(_(h, c),_(h,c));
char * j = _(e,i);
char * k = _(_(d,j) ,_(j,e));
char * l = _(_(k,h),_(d,e));
puts(++升); / * ++ l摆脱了
开头的虚假换行符* /
返回0;
}
为什么这个程序在合唱的最后一次
行发生后会添加一个虚假的''1'? (即我正和Danny和Lisa1合作拍摄视频)。
(我说它很耐心......但我担心因版权问题我不会这样做/>
能够将此修改汇总到下一个IOCCC ......)
return res;
}
int main(void)
{
char a[] = "\nHey man look at me rockin'' now\nI''m on the ";
char b[] = "Danny and Lisa";
char c[] = "\nThey take me away from";
char *d = _(_(a,"radio"),_(a,"video"));
char *e = _(d,_(" with ",b));
char *g = _("\nThe strangest places\nSweet ",b);
char *h = _(c,g);
char *i = _(_(h,c),_(h,c));
char *j = _(e,i);
char *k = _(_(d,j),_(j,e));
char *l = _(_(k,h),_(d,e));
puts(++l); /* ++l gets rid of the spurious newline character at the
beginning */
return 0;
}
Why does this program add a spurious ''1'' after each occurrence of the last
line of the chorus? (i.e. "I''m on the video with Danny and Lisa1").
(I said it was delirant... But I fear that for copyright problems I won''t be
able to sumbit a modification of this to the next IOCCC...)
它不会赢。 IOCCC寻求的混淆远远超过使用一个不寻常的名字,特别是一个不寻常的名称,这不是你的名字。
>
-
Eric Sosman
es ***** @ acm-dot-org.inva 盖子
It wouldn''t win. The IOCCC seeks obfuscations far more
original than just using an unusual name, especially an unusual
name that isn''t yours to use.
--
Eric Sosman
es*****@acm-dot-org.invalid
" Army1987" < pl ******** @ for.itwrote in message n
"Army1987" <pl********@for.itwrote in message n
#include< string.h>
#include< stdlib.h>
#include< stdio.h>
char * _(char * str1,char * str2)
{
char * res;
res = malloc((strlen(str1)+ strlen(str2))* sizeof(char));
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char *_(char *str1, char *str2)
{
char *res;
res = malloc((strlen(str1)+strlen(str2))*sizeof(char));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^
没有空间终止nul。
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
No room for terminating nul.
strcpy(res,str1);
strcpy(res + strlen(str1),str2);
strcpy(res, str1);
strcpy(res+strlen(str1), str2);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
此处未定义的行为。
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Undefined behaviour here.
返回res;
}
int main(无效)
{
char a [] =" \ nHey man看着我摇摇晃晃地''现在''我在'&;;
char b [] =" Danny和Lisa" ;;
char c [] =" \ n他们带我离开';
char * d = _(_ (a,radio),_(a," video"));
char * e = _(d,_(" with,b));
char * g = _(" \\\
最奇怪的地方\ nSweet",b);
char * h = _(c,g);
char * i = _(_(h,c),_(h,c));
char * j = _(e,i);
char * k = _(_(d,j),_(j,e));
char * l = _(_(k,h),_(d,e ));
puts(++ l); / * ++ l摆脱了
开头的虚假换行符* /
返回0;
}
为什么这个程序在合唱的最后一次
行发生后会添加一个虚假的''1'? (即我正和Danny和Lisa1一起观看视频)。
(我说它很耐用......但我担心因为版权问题我不会>
可以将此修改汇总到下一个IOCCC ......)
TIA
-
#include< stdio.h>
main()
{
printf(" \\\
\\\
-- \ nArmy1987");
}
return res;
}
int main(void)
{
char a[] = "\nHey man look at me rockin'' now\nI''m on the ";
char b[] = "Danny and Lisa";
char c[] = "\nThey take me away from";
char *d = _(_(a,"radio"),_(a,"video"));
char *e = _(d,_(" with ",b));
char *g = _("\nThe strangest places\nSweet ",b);
char *h = _(c,g);
char *i = _(_(h,c),_(h,c));
char *j = _(e,i);
char *k = _(_(d,j),_(j,e));
char *l = _(_(k,h),_(d,e));
puts(++l); /* ++l gets rid of the spurious newline character at the
beginning */
return 0;
}
Why does this program add a spurious ''1'' after each occurrence of the last
line of the chorus? (i.e. "I''m on the video with Danny and Lisa1").
(I said it was delirant... But I fear that for copyright problems I won''t
be able to sumbit a modification of this to the next IOCCC...)
T.I.A.
--
#include <stdio.h>
main()
{
printf("\n\n--\nArmy1987");
}
-
免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
我发现了问题。对于由函数_(str1,
str2)分配的内存来包含str1和str2的串联,它的大小应该是
strlen(str1)+ strlen(str2) )+ 1,因为需要一个字节来包含
终结符''\ 0''字符。
所以我替换了函数中的赋值正文:
res = malloc((strlen(str1)+ strlen(str2)+1)* sizeof(char));
可能,在该程序的前一版本'
之后的'\\'0'字节这些字符串被某种方式覆盖,因为它没有被分配。对于那个
字符串。
BTW,我的意思是谵妄。除了我的C之外,我应该检查一下我的英语...
I''ve found the problem. For the memory allocated by the function _(str1,
str2) to contain the concatenation of str1 and str2, its size should be
strlen(str1) + strlen(str2) + 1, as one byte is needed to contain the
terminator ''\0'' character.
So I replaced the assignment in the function body with:
res = malloc((strlen(str1)+strlen(str2)+1)*sizeof(char)) ;
Probably, in the former version of the program the ''\0'' byte after one of
these strings was somehow overwritten as it was not "allotted" for that
string.
BTW, I meant "delirious". I should have checked my English besides my C...
这篇关于熟食程序的问题很少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!