我的strcpy()不起作用 [英] my strcpy() is not working

查看:92
本文介绍了我的strcpy()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

shell程序在Linux下很好地编译,使用-g以便显示任何错误

,但是当代码执行时,该过程结束

没有错误。


这是电话:

strcpy(路径,(const char *)令牌[0] .c_str());


路径声明为:char * path;但从未初始化为

任何东西。

tokens []是字符串的STL向量(不是c字符串)。

at the point调用,令牌[0] =" / bin / ls"


以下是调用发生的上下文。子进程是
产生,输出看起来像

" esh%/ bin / ls

输入是一个用户命令,即将fork

在子进程中,构建args []

即将构建路径

路径是libc.so.6

esh%"


什么时候应该继续

"只是用标记填充路径[0]"


但它没有。

上下文:

else if(pid == 0){// child process


cout<< 在子进程中,构建args [] << endl;


cout<< 即将建立路径 << endl;

cout<< 令牌[0]是 <<标记[0] .c_str()<< endl;

char * path; // execl的路径

cout<< 路径是 <<路径<< endl;

//构建路径

//一切正常,直到这里

strcpy(path,(const char *)tokens [0 ] .c_str());


//这行代码永远不会被执行因为前一个

导致进程

//死了

cout<< 只用tokens [0]填充路径: <<路径<< endl;

如需任何帮助,我将不胜感激。


谢谢,

Dan

推荐答案

dj ***** @ iwebworks.com 写道:

大家好,

shell程序在Linux下编译很好,用-g以便任何
$ b将显示$ b错误,但是当代码执行时,过程

结束时没有错误。
Hi all,
The shell program compiles under Linux fine, with -g so that any
errors would be displayed, but when the code is executed, the process
ends without error.



您可能对-g的含义存在误解。

You may have a misconception about what -g means.


以下是电话:

strcpy(path,(const char *)tokens [0] .c_str());
Here is the call:
strcpy( path, (const char *)tokens[0].c_str() );



无需转换调用c_str()的结果。它已经是''const char *''类型的

There is no need to cast the result of calling c_str(). It is already
of type ''const char*''.


>

路径被声明with:char * path;但从未初始化为

任何东西。
>
path was declared with: char* path; but never initialized to
anything.



如果将未经宣传的指针传递给''strcpy'',则

代码的行为是未定义的。它可能(允许)表现得好像一切都好。

If you pass an uninitalised pointer to ''strcpy'', the behaviour of the
code is undefined. It may (allowed to) behave as if everything is OK.


tokens []是字符串的STL向量(不是c字符串)。

在通话时,令牌[0] =" / bin / ls"


以下是调用发生的上下文。生成子进程

,输出看起来像

" esh%/ bin / ls

输入是一个用户命令,即将发布fork

在子进程中,构建args []

即将构建路径

路径是libc.so.6

esh%"


什么时候应该继续

"只是用标记填充路径[0]"


但它没有。
tokens[] is an STL vector of strings (not c-strings).
at the point of the call, tokens[0] = "/bin/ls"

So below is the context in which the call occurs. The child process
is spawned, the output looks like
"esh% /bin/ls
Input was a user command, about to fork
In the child process, building the args[]
about to build path
path is libc.so.6
esh%"

when it should continue
"just filled path with tokens[0]"

but it doesn''t.



在将
初始化之前将''path''传递给''cout''时,行为是未定义的。 ''cout''尝试取消引用指针

,这就是未定义行为发生的地方。

The behaviour is undefined when you pass ''path'' to ''cout'' before
ever initialising it. ''cout'' attempts to dereference the pointer
and that''s where undefined behaivour occurs.


>


上下文:

else if(pid == 0){//子进程


cout<< 在子进程中,构建args [] << endl;


cout<< 即将建立路径 << endl;

cout<< 令牌[0]是 <<标记[0] .c_str()<< endl;

char * path; // execl的路径

cout<< 路径是 <<路径<< endl;

//构建路径

//一切正常,直到这里

strcpy(path,(const char *)tokens [0 ] .c_str());


//这行代码永远不会被执行因为前一个

导致进程

//死了

cout<< 只用tokens [0]填充路径: <<路径<< endl;


如有任何帮助,我将不胜感激。
>

Context:
else if ( pid == 0) { // child process

cout << "In the child process, building the args[]" << endl;

cout << "about to build path" << endl;
cout << "tokens[0] is " << tokens[0].c_str() << endl;
char* path; // path for execl

cout << "path is " << path << endl;
// build path
// everything works fine up til here
strcpy( path, (const char *)tokens[0].c_str() );

// this line of code is never executed because the previous one
caused process
// to die
cout << "just filled path with tokens[0]: " << path << endl;
For any help, I would greatly appreciate it.



确保''path''指向有效内存。更好的是,声明它是
一个*数组*而不是一个指针。并给予足够的空间,以便角色

不会写在你为他们分配的地方之外。即使

更好,仍然是在任何地方都使用''string'',除非你绝对必须,否则永远不要触摸裸b / b
指针,然后仍然不要触摸它们
如果你不知道它们是如何工作的(即在使用它们之前学习它们)。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Make sure ''path'' points to valid memory. Better yet, declare it
an *array* and not a pointer. And give enough room so the characters
would not be written beyond the place you allocate for them. Even
better, still, is to use ''string'' everywhere and never touch bare
pointers unless you absolutely have to, and then still not touch them
if you don''t know how they work (i.e. learn them before using them).

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



strcpy(path,(const char *)tokens [0] .c_str());
strcpy( path, (const char *)tokens[0].c_str() );



摆脱那个演员阵容 - 这是多余的。


Get rid of that cast -- it''s redundant.


路径声明:char * path;但从未初始化为

任何东西。
path was declared with: char* path; but never initialized to
anything.



天才的想法。就你而言,指针变量path,

包含随机存储器地址。你正在写一个随机的记忆位置。

那么好,你觉得呢?


你不明白指针。这是我最近的一篇文章,可能有所帮助:

http://groups.google.ie/group/comp.l...ddfb2a7?hl=en&


-


Frederick Gotham


Genius idea. As far as you''re concerned, the pointer variable, "path",
contains a random memory address. You''re writing to a random memory location.
Is that good, do you think?

You don''t understand pointers. Here''s a recent post of mine which may help:

http://groups.google.ie/group/comp.l...ddfb2a7?hl=en&

--

Frederick Gotham


Victor Bazarov发布:
Victor Bazarov posted:

没有需要转换调用c_str()的结果。它已经是''const char *''类型的

There is no need to cast the result of calling c_str(). It is already
of type ''const char*''.



假设说,即使它是char *而不是char

const *,演员在这个函数的上下文中仍然是多余的

调用。


-


Frederick Gotham

And hypothetically speaking, even if it were "char*" rather than "char
const*", the cast would _still_ be redundant in the context of that function
call.

--

Frederick Gotham


这篇关于我的strcpy()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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