473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

my strcpy() is not working

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.

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

path was declared with: char* path; but never initialized to
anything.
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.
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.

Thanks,
Dan

Oct 3 '06 #1
10 3927
dj*****@iwebworks.com wrote:
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.
You may have a misconception about what -g means.
Here is the call:
strcpy( path, (const char *)tokens[0].c_str() );
There is no need to cast the result of calling c_str(). It is already
of type 'const char*'.
>
path was declared with: char* path; but never initialized to
anything.
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[] 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.
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.
>

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.
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
Oct 3 '06 #2
strcpy( path, (const char *)tokens[0].c_str() );

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

path was declared with: char* path; but never initialized to
anything.

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
Oct 3 '06 #3
Victor Bazarov posted:
There is no need to cast the result of calling c_str(). It is already
of type 'const char*'.
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
Oct 3 '06 #4

Frederick Gotham wrote:
strcpy( path, (const char *)tokens[0].c_str() );


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

path was declared with: char* path; but never initialized to
anything.


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
Hi all,
Thanks for your help. Indeed it is not good to randomly overwrite
whatever the uninitialized pointer is pointing to. Interesting is that
my first try worked on a Mac OS, but did not work on Linus which is why
I had to change it. Here is what I did to get the program to not
fault.

char* path; // path for execl
path = (char *)tokens[0].c_str();

Thanks,
Dan

Oct 3 '06 #5
my first try worked on a Mac OS, but did not work on Linus which is why
I had to change it.
Linux!

Sorry,
Dan

Oct 3 '06 #6
dj*****@iwebworks.com wrote:
[..] Here is what I did to get the program to
not fault.

char* path; // path for execl
path = (char *)tokens[0].c_str();
WHY??? What for, FCOL?

Why couldn't you simply pass 'tokens[0]' to cout?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '06 #7
char* path; // path for execl
path = (char *)tokens[0].c_str();
Ignoring the fact that there's no need for the invocation of c_str, I want to
talk to you about constness.

Please do me a little favour and compile the following:

int main()
{
char const *p = 0;

char *q = p;
}

Let me know how your compiler reacted... any errors, any warnings?

--

Frederick Gotham
Oct 3 '06 #8

Frederick Gotham wrote:
char* path; // path for execl
path = (char *)tokens[0].c_str();

Ignoring the fact that there's no need for the invocation of c_str, I want to
talk to you about constness.

Please do me a little favour and compile the following:

int main()
{
char const *p = 0;

char *q = p;
}

Let me know how your compiler reacted... any errors, any warnings?

--

Frederick Gotham
test.cpp: In function `int main()':
test.cpp:7: error: invalid conversion from `const char*' to `char*'
make: *** [test.o] Error 1

I was getting lots of that until I happened upon my final solution I
posted above. Maybe the c_str() is not necessary but the cast got it
into usable form.

Thanks,
Dan

Oct 3 '06 #9
dj*****@iwebworks.com wrote:
Frederick Gotham wrote:
>>char* path; // path for execl
path = (char *)tokens[0].c_str();

Ignoring the fact that there's no need for the invocation of c_str,
I want to talk to you about constness.
[..]
I was getting lots of that until I happened upon my final solution I
posted above. Maybe the c_str() is not necessary but the cast got it
into usable form.
Usable for WHAT? What are you doing with that pointer after you obtain
it by const-casting the 'c_str' return value?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '06 #10
dj*****@iwebworks.com schrieb:
Frederick Gotham wrote:
>>char* path; // path for execl
path = (char *)tokens[0].c_str();
Ignoring the fact that there's no need for the invocation of c_str, I want to
talk to you about constness.

Please do me a little favour and compile the following:

int main()
{
char const *p = 0;

char *q = p;
}

Let me know how your compiler reacted... any errors, any warnings?

test.cpp: In function `int main()':
test.cpp:7: error: invalid conversion from `const char*' to `char*'
make: *** [test.o] Error 1
The compiler complains about an error and you tell him to shut up by using
a cast. Trust the compiler. Don't cast.

You can use a const char* or a std::string, maybe a std::string reference:

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

std::string path = tokens[0];

const std::string& path = tokens[0];

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Oct 3 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Sheer | last post by:
I need to automatically search and replace all fixed size buffer strcpy's with strncpy's (or better yet, strlcpy's) as a security and stability audit. The code base is large and it is not feasable...
7
by: David. E. Goble | last post by:
Hi all; I have the following files; index.html, sigsheader.js, sigsboby.js, smilesbody.js and smiles.js. The sourse is below. The page displays two manual slide shows... Each slideshow has a set...
9
by: David. E. Goble | last post by:
Arrrh! some buttons work while others don't, but I can't see why. I have tried comparing the files that do work, with the ones that don't. But to no help. The funny thing is the parts that work...
53
by: SK | last post by:
Hi I appreciate all of the feedback I have recieved. I am enjoying working on my program and I hope that it can be appreciated. I have my program compiling now and I am continuing to work out...
3
by: nirvana4lf | last post by:
this is probably a noob question but wutever. im using the strcpy(); function but it isnt working. here is an example: #include <stdio.h> #include <iostream> #include <string> #include...
302
by: Lee | last post by:
Hi Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). Is this due to the possibility of array overflow? Is it correct that the program...
1
by: ifmusic | last post by:
I have This Code. Token.c: typedef struct { unsigned char orden; char ciudad; unsigned char ip; //que son del router Asociado a la ciudad unsigned int puerto; // """ //int socket; }Ciudad;
3
by: RavindraB | last post by:
I am migrating C++ code to VS2005, for this i have to replace the some Deprecated CRT Functions like “strcpy” by strcpy_s I have written template to replace the function call. For strcpy_s I need...
4
by: Benoit Lefebvre | last post by:
Weird subject you will say.. I didn't know what to put in there.. Long story short, I'm changing the value of a variable from a function.. Nothing unusual there.. How come the value of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.