gets()函数使用gcc编译器生成强警告消息 [英] gets() function generates strong warning message with gcc compiler

查看:85
本文介绍了gets()函数使用gcc编译器生成强警告消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好


你能告诉我这段代码有什么问题吗?

我用的是gcc编译器,但是当我想用的时候


我的代码中的get()函数但它需要一个危险的警告(获取

函数是危险的


,不应该使用),我不知道为什么。任何方式,我决定写我的

自己获得()


函数,但现在它需要分段错误。请帮助我

谢谢


这是代码:


#包括< stdio.h>


#include< string.h>


#include< stdlib.h>

char * getstring()


{


int k = 1,j = 0,i;


char ch;


char * source = malloc(sizeof(char)* k);


if (!来源)


{


printf(" \ n \ nn不能分配足够的内存\ nn \ nn" );


退出(1);


}


else {


while((ch = getchar())!=''\ n'')


{


if(j == k)

{


realloc(来源,(k * = 2));


if(!source)

{


printf(\ ncan not allocat sufficiant memory \ n);


退出(1);


} //结束如果


} //结束如果


*(来源+ j)= ch;


j ++;

} //结束时


*(来源+ j)=''\''';


//这里显示正确


printf(&\\\\\\\\\ nn这是你输入的内容:%s \ n,来源);

} //其他结束


返回来源;


}


void main()


{


char * st;


printf("输入一个字符串:");


/ *如果你想在这里看到警告取消注释


得到(st);


* /

//我忘了这里或下一个命令产生分段错误

strcpy(st,getstring());


printf(&\\\\\\\\\ nn这是你输入的内容:%s \ n",st);


}

hi all

can you tell me what''s the wrong with this code?
I use gcc compiler,but when I wanted to use

gets() function in my code but it takes a dangerous warning(the gets
function is dangerous

and should not be used),I don''t know why.Any way,I decided to write my
own gets()

function,but now it take segmentation fault error.Please help me
thanks

THIS IS THE CODE:

#include <stdio.h>

#include <string.h>

#include <stdlib.h>
char * getstring()

{

int k=1,j=0,i;

char ch;

char *source=malloc (sizeof(char)*k);

if(!source)

{

printf("\n\nCan not allocate sufficent memory\n\n");

exit(1);

}

else{

while((ch=getchar())!=''\n'')

{

if(j==k)
{

realloc(source,(k*=2));

if(!source)
{

printf("\ncan not allocat sufficiant memory\n");

exit(1);

}//end if

}//end if

*(source+j)=ch;

j++;
}//end while

*(source+j)=''\0'';

//Here it shows correctly

printf("\n\n\n\n\nThis is what you typed :%s\n",source);
}//end of else

return source;

}

void main()

{

char *st;

printf("Enter a string: ");

/*If you want to see the warning gets message uncomment here

gets(st);

*/
//I forgot here or next command generate the segmentation fault
strcpy(st,getstring());

printf("\n\n\n\n\nThis is what you typed :%s\n",st);

}

推荐答案

pa **** @ gmail.com 写道:

hi all


你能告诉我这段代码有什么问题吗?

我用gcc编译器,但是当我想用的时候

$ b我的代码中有$ b gets()函数但它需要一个危险的警告(获取

函数是危险的


并且不应该使用),我不要不知道为什么。任何方式,我决定写我的

自己获得()


函数,但现在它需要分段错误。请帮助我是

谢谢


这是代码:


#include< stdio.h>


#include< string.h>


#include< stdlib.h>


char * getstring()


{


int k = 1,j = 0,i;


char ch;


char * source = malloc(sizeof(char)* k);


if(!source)


{


printf(\ n \ nn不能分配足够的内存\ n \ n);


退出(1);


}


else {


while((ch = getchar())!=''\ n'')


{


if(j == k)

{


realloc(来源,(k * = 2));


如果(!来源)

{


printf(\ ncan not allocat sufficiant memory\\\
);


退出(1);


} //结束如果


} //结束如果


*(source + j)= ch;


j ++;

} //结束时


*(来源+ j)=''\ 0'';


//这里显示正确


printf(&\\; \ n \ nn \ n \ n \ n这是你输入的内容:%s \ nn,来源);


} //结束其他


返回来源;


}


void main()


{


char * st;


printf("输入字符串:" ;);


/ *如果你想在这里看到警告取消消息


得到(st);
< br $>
* /

//我忘了这里或下一个命令生成分段错误

strcpy(st,getstring());
hi all

can you tell me what''s the wrong with this code?
I use gcc compiler,but when I wanted to use

gets() function in my code but it takes a dangerous warning(the gets
function is dangerous

and should not be used),I don''t know why.Any way,I decided to write my
own gets()

function,but now it take segmentation fault error.Please help me
thanks

THIS IS THE CODE:

#include <stdio.h>

#include <string.h>

#include <stdlib.h>
char * getstring()

{

int k=1,j=0,i;

char ch;

char *source=malloc (sizeof(char)*k);

if(!source)

{

printf("\n\nCan not allocate sufficent memory\n\n");

exit(1);

}

else{

while((ch=getchar())!=''\n'')

{

if(j==k)
{

realloc(source,(k*=2));

if(!source)
{

printf("\ncan not allocat sufficiant memory\n");

exit(1);

}//end if

}//end if

*(source+j)=ch;

j++;
}//end while

*(source+j)=''\0'';

//Here it shows correctly

printf("\n\n\n\n\nThis is what you typed :%s\n",source);
}//end of else

return source;

}

void main()

{

char *st;

printf("Enter a string: ");

/*If you want to see the warning gets message uncomment here

gets(st);

*/
//I forgot here or next command generate the segmentation fault
strcpy(st,getstring());



BOOM *?!@?!?!

st无处可指。你需要为st分配内存

或者只是将st指向任何getstring()返回。

[st = getstring();]

getstring()也存在潜在的问题。我将把它留给

专家评论。


-p_cricket_guy

BOOM *?!@?!?!
st is pointing nowhere here. You need to either allocate memory
to st or just point st to whatever getstring() returns.
[st = getstring();]
There are potential problems in getstring() too. I will leave it for
the experts to comment on.

-p_cricket_guy


> ;

printf(&\\; \ n \ nn \ n \ n \\ n \\ n这是你输入的内容:%s \ n",st);

}
>
printf("\n\n\n\n\nThis is what you typed :%s\n",st);

}


pa **** @ gmail .com 写道:
pa****@gmail.com wrote:

大家好


你能告诉我这是什么问题吗?代码?

我使用gcc编译器,但是当我想在我的代码中使用


gets()函数但它需要一个危险的警告(获取

功能很危险


,不应该使用),我不知道为什么。
hi all

can you tell me what''s the wrong with this code?
I use gcc compiler,but when I wanted to use

gets() function in my code but it takes a dangerous warning(the gets
function is dangerous

and should not be used),I don''t know why.



维基百科是你的朋友;-)

Wikipedia is your friend ;-)


无论如何,我决定写我的

自己获得()


功能,但现在它需要分段错误。请帮助我

谢谢

这是代码:


#include< stdio.h>


#include< string.h>


#include< stdlib.h>


char * getstring()


{


int k = 1,j = 0,i;


char ch;


char * source = malloc(sizeof(char)* k);


if(!source)


{


printf(" \ n \ n可以不分配足够的内存\ n \ n");


退出(1);


}


else {


while((ch = getchar())!=''\如果(j == k)

{


realloc(来源,(k * = 2));


if(!source)

{


printf(" \ ncan not allocat sufficiant memory\\\
);

退出(1);


} //结束如果


} //结束如果


*(source + j)= ch;


j ++;

} //结束时


*(来源+ j)=''\ 0'';


//这里显示正确


printf (\ n \ nn \ n \ n \\ n这是你输入的内容:%s \ nn,来源);


} //结束否则


返回来源;


}


void main()
< br $>
{


char * st;


printf("输入字符串:");


/ *如果你想在这里看到警告取消消息


得到(st);


* /

//我忘了这里或下一个命令产生分段错误

strcpy(st,getstring());


printf(&\\; \ n \\\\\\\ n这是你输入的内容:%s \ n",st);


}
Any way,I decided to write my
own gets()

function,but now it take segmentation fault error.Please help me
thanks

THIS IS THE CODE:

#include <stdio.h>

#include <string.h>

#include <stdlib.h>
char * getstring()

{

int k=1,j=0,i;

char ch;

char *source=malloc (sizeof(char)*k);

if(!source)

{

printf("\n\nCan not allocate sufficent memory\n\n");

exit(1);

}

else{

while((ch=getchar())!=''\n'')

{

if(j==k)
{

realloc(source,(k*=2));

if(!source)
{

printf("\ncan not allocat sufficiant memory\n");

exit(1);

}//end if

}//end if

*(source+j)=ch;

j++;
}//end while

*(source+j)=''\0'';

//Here it shows correctly

printf("\n\n\n\n\nThis is what you typed :%s\n",source);
}//end of else

return source;

}

void main()

{

char *st;

printf("Enter a string: ");

/*If you want to see the warning gets message uncomment here

gets(st);

*/
//I forgot here or next command generate the segmentation fault
strcpy(st,getstring());

printf("\n\n\n\n\nThis is what you typed :%s\n",st);

}



但是你没有使用自己的函数,你还是使用了get()
! />

关于你的功能的一些评论:


1. int k = 1,j = 0,i;


因为几乎可以肯定输入字符串将超过1个字符,如果你将k初始化为更大的值,它将保存对malloc的一些

调用,

100说。或者你的函数可以接受额外的

参数,它将指定初始缓冲区大小。


2. while((ch = getchar())!=' '\ n'')


您还需要检查EOF。


3. *(来源+ j)='' \ 0'';


就你所知,循环的最后一次执行可能已经完全填满了缓冲区,所以这将写入结束。

Spiros Bousbouras

But you''re not using your own function , you''re
still using gets() !

Some remarks about your function:

1. int k=1,j=0,i;

Since it is almost certain that the input string will
be more than 1 characters long it would save some
calls to malloc if you initialized k to a larger value ,
100 say. Or your function may accept an additional
parameter which will specify the initial buffer size.

2. while((ch=getchar())!=''\n'')

You also need to check for EOF.

3. *(source+j)=''\0'';

For all you know the last execution of the loop may have
completely filled the buffer so this will write past the end.
Spiros Bousbouras


sp ** **@gmail.com 写道:
sp****@gmail.com wrote:
pa **** @ gmail。 com 写道:


while((ch = getchar())!=''\ n'')
while((ch=getchar())!=''\n'')



您还需要检查EOF。


You also need to check for EOF.



所以你需要将ch声明为int而不是

char。

So you would need to declare ch as int rather than
char.


这篇关于gets()函数使用gcc编译器生成强警告消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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