在CLI中使用strtok segfaults但在GDB中没有 [英] strtok segfaults in CLI but not in GDB

查看:117
本文介绍了在CLI中使用strtok segfaults但在GDB中没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

这里我有一个奇怪的问题,一个真正简单的strtok例子。


该程序如下:


### BEGIN STRTOK ###


#include< string.h>

#include< stdio.h>


int main()

{

char * input1 =" Hello,World!" ;;


char * tok;


tok = strtok(input1,"");

if(tok)printf( "%s \ n",tok);


tok = strtok(NULL,"");

if(tok)printf( %s \ nn,tok);


返回(0);


}


### END STRTOK ###

现在,当我从命令行运行它时,我收到一个总线错误:


# ## BEGIN COMMAND LINE OUTPUT ###

Hello,
here I have a strange problem with a real simple strtok example.

The program is as follows:

### BEGIN STRTOK ###

#include <string.h>
#include <stdio.h>

int main()
{
char *input1 = "Hello, World!";

char *tok;

tok = strtok(input1, " ");
if(tok) printf("%s\n", tok);

tok = strtok(NULL, " ");
if(tok) printf("%s\n", tok);

return(0);

}

### END STRTOK ###
Now, when I run it from the command line, I get a bus error:

### BEGIN COMMAND LINE OUTPUT ###


gcc -ggdb -Wall -o strtok strtok.c

./strtok
gcc -ggdb -Wall -o strtok strtok.c
./strtok



总线错误(核心转储)

退出138


## #END命令行输出###


当我在GDB中一步一步地运行它时,程序正常终止:


### BEGIN DEBUGGER OUTPUT ###

Bus error (core dumped)
Exit 138

### END COMMAND LINE OUTPUT ###

When I run it step by step in GDB, the program terminates normally:

### BEGIN DEBUGGER OUTPUT ###


gdb ./strtok
gdb ./strtok



GNU gdb 6.1.1 [FreeBSD]

[snip] GDB版权和bla bla [/ snip]

(gdb)break main

断点1在0x8048570:文件strtok.c ,第6行。

(gdb)运行

开始程序:/ home / piter / strtok


断点1,主要( )at strtok.c:6

6 char * input1 =" Hello,World!" ;;

(gdb)next

10 tok = strtok(input1," );

(gdb)

11如果(tok)printf("%s \ n",tok);

(gdb)

你好,

13 tok = strtok(NULL,"");

(gdb)

14 if(tok)printf("%s\ n,tok);

(gdb)

World!

16返回(0);

(gdb)

18}

(gdb)

0x08048485 in _start ()

(gdb)

单步执行直到退出函数_start,

没有行号信息。


程序正常退出。

(gdb)

### END DEBUGGER OUTPUT ###


是否有我缺少的东西和/或strtok,或者它是与我的环境相关的问题(在这种情况下我很乐意发表

在正确的新闻组中)?


提前预付


-

Pietro Cerutti


PGP公钥ID:
http://gahr.ch/pgp

推荐答案

Pietro Cerutti写道:
Pietro Cerutti wrote:

你好,

这里我有一个奇怪的问题真正简单的strtok例子。


该程序如下:


### BEGIN STRTOK ###

#include< string.h>

#include< stdio.h>


int main()

{

char * input1 =" Hello,World!" ;;


char * tok;


tok = strtok(input1," ");
Hello,
here I have a strange problem with a real simple strtok example.

The program is as follows:

### BEGIN STRTOK ###

#include <string.h>
#include <stdio.h>

int main()
{
char *input1 = "Hello, World!";

char *tok;

tok = strtok(input1, " ");



strtok改变其输入。你正在传递一个字符串文字,修改

字符串文字调用未定义行为的恶魔。不要。


-

Ian Collins。

strtok alters its input. You are passing it a string literal, modifying
a string literal invokes the demons of undefined behavior. Don''t.

--
Ian Collins.


Pietro Cerutti说:
Pietro Cerutti said:

你好,

这里我有一个奇怪的问题,一个真正简单的strtok例子。


该计划如下:


### BEGIN STRTOK ###


#include< string.h>

#include< stdio.h>


int main()

{

char * input1 =" ; Hello,World!" ;;


char * tok;


tok = strtok(input1,"");
Hello,
here I have a strange problem with a real simple strtok example.

The program is as follows:

### BEGIN STRTOK ###

#include <string.h>
#include <stdio.h>

int main()
{
char *input1 = "Hello, World!";

char *tok;

tok = strtok(input1, " ");



strtok修改传递它的字符串。你传给它一个字符串文字。

你不允许修改字符串文字。


更改


char * input1 =" Hello,World!"





char input1 [] =" Hello,World! ; $

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

strtok modifies the string you pass it. You pass it a string literal.
You''re not allowed to modify string literals.

Change

char *input1 = "Hello, World!";

to

char input1[] = "Hello, World!";
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


Pietro Cerutti写道:
Pietro Cerutti wrote:

char * input1 =" Hello,World!" ;;
char *input1 = "Hello, World!";



以防万一,我知道要标记的字符串不应该是一个

常量,而是一个字符数组。

所以,它应该被声明为


char input1 [14] =" Hello,World!" ;;

我不明白的是:为什么它在GDB中有效?


-

Pietro Cerutti


PGP公钥:
http://gahr.ch/ pgp


这篇关于在CLI中使用strtok segfaults但在GDB中没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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