473,396 Members | 2,109 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,396 software developers and data experts.

strcpy Question

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

int main()
{
char *imsip;

strcpy(imsip, "archie");
return 0;
}

the above code causes error which i expected.. as the object imsip is
not initalized.
But the below one executes alright..

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

int main()
{
struct msg
{
char p1[30];
char *p2;
} myptrs;

strcpy(myptrs.p1, "Wonderful day");
strcpy(myptrs.p2, "Beautiful Day");

puts(myptrs.p1);
puts(myptrs.p2);
return 0;
}

I have no idea how come the second one executes alright..

Thanks in advance..

Nov 13 '05 #1
8 5537
herrcho wrote:
#include <stdio.h>
#include <string.h>

int main()
{
char *imsip;

strcpy(imsip, "archie");
return 0;
}

the above code causes error which i expected.. as the object imsip is
not initalized.
But the below one executes alright..

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

int main()
{
struct msg
{
char p1[30];
char *p2;
} myptrs;

strcpy(myptrs.p1, "Wonderful day");
strcpy(myptrs.p2, "Beautiful Day");

puts(myptrs.p1);
puts(myptrs.p2);
return 0;
}

I have no idea how come the second one executes alright..

To dereference and otherwise try to access the memory pointed by an
uninitialized pointer is Undefined Behaviour. Undefined Behaviour
includes behaving (apparently) correctly.

On some facetious platforms, such as the DS9k, it also includes
systematically behaving the opposite of what the user expects. I know.
This is very advances technology (able of mind reading, etc), which the
world isn't ready for. That's why you only find DS9ks in Area 51...

--
Bertrand Mollinier Toublet
Currently looking for employment in the San Francisco Bay Area
http://www.bmt.dnsalias.org/employment

Nov 13 '05 #2

Since strcpying into an uninitialized/unallocated pointer is undefined, you
are not guaranteed of the results. Either point the pointer to some
allocated memory or declare another character array with adequate space so
you won't have this problem.
"herrcho" <he*********@kornet.net> wrote in message
news:bl**********@news1.kornet.net...
#include <stdio.h>
#include <string.h>

int main()
{
char *imsip;

strcpy(imsip, "archie");
return 0;
}

the above code causes error which i expected.. as the object imsip is
not initalized.
But the below one executes alright..

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

int main()
{
struct msg
{
char p1[30];
char *p2;
} myptrs;

strcpy(myptrs.p1, "Wonderful day");
strcpy(myptrs.p2, "Beautiful Day");

puts(myptrs.p1);
puts(myptrs.p2);
return 0;
}

I have no idea how come the second one executes alright..

Thanks in advance..

Nov 13 '05 #3
On Mon, 29 Sep 2003 23:22:04 +0900 (KST), in comp.lang.c , "herrcho"
<he*********@kornet.net> wrote:
struct msg
{
char p1[30];
char *p2;
} myptrs;

strcpy(myptrs.p1, "Wonderful day");
strcpy(myptrs.p2, "Beautiful Day");

I have no idea how come the second one executes alright..


By bad luck. By chance, there happened to be some spare memory after
the declaration of p2.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #4
"herrcho" <he*********@kornet.net> wrote in message news:<bl9f6c$b85
....
But the below one executes alright..

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

int main()
{
struct msg
{
char p1[30];
char *p2;
} myptrs;

strcpy(myptrs.p1, "Wonderful day");
strcpy(myptrs.p2, "Beautiful Day");

puts(myptrs.p1);
puts(myptrs.p2);
return 0;
}

I have no idea how come the second one executes alright..

Thanks in advance..


Pointer p2 uses allocated memory from p1.
Try 2 bigger strings and you'll see that they overlap...
e.g.
strcpy(myptrs.p1, "Wonderful day ...bla...bla...bla...");
strcpy(myptrs.p2, "Beautiful Day ...bla...bla...bla...");
printf("%s\n",myptrs.p1);
printf("%s\n",myptrs.p2);
Nov 13 '05 #5
Bertrand Mollinier Toublet wrote:
herrcho wrote:
#include <stdio.h>
#include <string.h>

int main()
{
char *imsip;

strcpy(imsip, "archie");
return 0;
}

the above code causes error which i expected.. as the object
imsip is not initalized.
But the below one executes alright..

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

int main()
{
struct msg
{
char p1[30];
char *p2;
} myptrs;

strcpy(myptrs.p1, "Wonderful day");
strcpy(myptrs.p2, "Beautiful Day");

puts(myptrs.p1);
puts(myptrs.p2);
return 0;
}

I have no idea how come the second one executes alright..

To dereference and otherwise try to access the memory pointed
by an uninitialized pointer is Undefined Behaviour. Undefined
Behaviour includes behaving (apparently) correctly.

On some facetious platforms, such as the DS9k, it also includes
systematically behaving the opposite of what the user expects.
I know. This is very advances technology (able of mind reading,
etc), which the world isn't ready for. That's why you only find
DS9ks in Area 51...


You are leaving the OP even more confused. Not only do the
pointers imsip and myptrs.p2 need to be initialized, they have to
be initialized to point to sufficient memory to hold the strings
to be copied into them (which is at least one greater than the
length of such string). The usual method is via "ptr =
malloc(sizeneeded);" and checking the result is non NULL. However
the space need not be allocated by malloc, it may be function
scope or file scope declared arrays (usually called local or
global). The difference is how to release that memory later.

To the OP: The second one DOES NOT execute alright, it only
appears that way on your particular system. One possibility for
undefined behavior is to apparently succeed.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #6
Mac
On Mon, 29 Sep 2003 13:37:01 +0000, John Roussos wrote:
"herrcho" <he*********@kornet.net> wrote in message news:<bl9f6c$b85
...
But the below one executes alright..

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

int main()
{
struct msg
{
char p1[30];
char *p2;
} myptrs;

strcpy(myptrs.p1, "Wonderful day");
strcpy(myptrs.p2, "Beautiful Day");

puts(myptrs.p1);
puts(myptrs.p2);
return 0;
}

I have no idea how come the second one executes alright..

Thanks in advance..
Pointer p2 uses allocated memory from p1.


What gives you that idea? It has already been pointed out that the code
invokes Undefined Behavior (UB) so why do you try to rationalize what is
happening?

Or, better yet, what section in the standard makes you think that myptrs.p2
points just beyond myptrs.p1 in this code?
Try 2 bigger strings and you'll see that they overlap...
e.g.
strcpy(myptrs.p1, "Wonderful day ...bla...bla...bla...");
strcpy(myptrs.p2, "Beautiful Day ...bla...bla...bla...");
printf("%s\n",myptrs.p1);
printf("%s\n",myptrs.p2);


Mac
--
Nov 13 '05 #7
The unitialized (by you) pointers will, nevertheless, have some
initial value. For whatever reason, in the first code snippet, imsip
has a value that causes a memory violation. In the second example,
myptrs.p2 falls within the valid memory range for the process. You
should try printing out the values of the unitialized pointers and
verify that they are "initialized" to different values.

This is an excellent example of undefined behaviour.

"herrcho" <he*********@kornet.net> wrote in message news:<bl**********@news1.kornet.net>...
#include <stdio.h>
#include <string.h>

int main()
{
char *imsip;

strcpy(imsip, "archie");
return 0;
}

the above code causes error which i expected.. as the object imsip is
not initalized.
But the below one executes alright..

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

int main()
{
struct msg
{
char p1[30];
char *p2;
} myptrs;

strcpy(myptrs.p1, "Wonderful day");
strcpy(myptrs.p2, "Beautiful Day");

puts(myptrs.p1);
puts(myptrs.p2);
return 0;
}

I have no idea how come the second one executes alright..

Thanks in advance..

Nov 13 '05 #8
"Victor" <we******@yahoo.com> wrote in message
news:4c**************************@posting.google.c om...
The unitialized (by you) pointers will, nevertheless, have some
initial value.
No, not from the language perspective they don't.
For whatever reason, in the first code snippet, imsip
has a value that causes a memory violation.
Yes, this is a concrete example of one of
theoretically infinite possiblities.
In the second example,
myptrs.p2 falls within the valid memory range for the process. You
should try printing out the values of the unitialized pointers and
verify that they are "initialized" to different values.
Evaluating an uninitialized object produces undefined
behavior. The results *cannot* be used to draw
*any* conclusions. They verify nothing. (e.g. the result
could vary every time, even on the same system).


This is an excellent example of undefined behaviour.


Sure is.

-Mike
Nov 13 '05 #9

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...
23
by: JC | last post by:
hi, i want to combine two string together.. and put in to another string. how can i do . i try myself.. with the follow code. but seem can't get the result i want.. i want to get the result with...
10
by: ios | last post by:
Hi Can someone tell me what is different between below case? strcpy(eventname, "MDCX_RSP"); and sprintf(eventname, "MDCX_RSP"); Thanks, Leon
9
by: Pascal Damian | last post by:
I read somewhere that strcpy() is safer when dealing with malloc()-ed strings. Is that true? (Of course I know that both are unsafe). -- Pascal
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...
55
by: Jake Thompson | last post by:
I need to copy a value into a char * field. I am currently doing this strcpy(cm8link.type,"13"); but I get an error of error C2664: 'strcpy' : cannot convert parameter 1 from 'const char'...
4
by: chikito.chikito | last post by:
1. Can someone tell me the difference between these two functions: void strcpy(char *s1, const char *s2) { while(*s1++ = *s2++) ; } //function prototype of strcpy follows char...
16
by: mdh | last post by:
I have a question about the library function strcpy. In K&R on page 109, strcpy is used to copy a line ( line ) to a pointer (char *), which is first allocated space by K&Rs "alloc" function. In...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.