C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 18th, 2012, 09:55 AM
Chead Chead is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 5 Chead User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 31 sec
Reputation Power: 0
Strcpy behaviour

Code:
int main()
{
    char str[]="hello";
    int i =0;
    char *new;
    while(str[i]){
        printf("str[%d]= %p : %c\n",i,str+i,str[i]);
        i++;
    }
    new =strcpy(str+2,str+1);
    printf("%s\n", new);
    printf(" %p\n",new);
    i = 0;
    while(str[i]){
        printf("str[%d]= %p : %c\n",i,str+i,str[i]);
        i++;
    }
    return 0;
}


Quote:
Output:
str[0]= 0x7fff4f2572e0 : h
str[1]= 0x7fff4f2572e1 : e
str[2]= 0x7fff4f2572e2 : l
str[3]= 0x7fff4f2572e3 : l
str[4]= 0x7fff4f2572e4 : o
eello
0x7fff4f2572e2
str[0]= 0x7fff4f2572e0 : h
str[1]= 0x7fff4f2572e1 : e
str[2]= 0x7fff4f2572e2 : e
str[3]= 0x7fff4f2572e3 : e
str[4]= 0x7fff4f2572e4 : l
str[5]= 0x7fff4f2572e5 : l
str[6]= 0x7fff4f2572e6 : o


copying data from successive locations shoud generate a segfault if character by character is copied
( i mean 'e' is copied str+1 to str+2 and then takes 'e' form str+2 copies to str+3 and so on )

if takes 4 bytes from str+1 and copies to str+2 then
i should get "ello"

the output is "eello" , how is it getting extra e.

Reply With Quote
  #2  
Old September 18th, 2012, 11:24 AM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,834 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 14 h 46 m 19 sec
Reputation Power: 1774
> new =strcpy(str+2,str+1);
Nearly all of the standard library functions have undefined behaviour if you try to do operations on overlapping memory - which is what you're doing here.

http://www.manpagez.com/man/3/strcpy/
Quote:
DESCRIPTION

The stpcpy() and strcpy() functions copy the string s2 to s1 (including
the terminating `\0' character).

The stpncpy() and strncpy() functions copy at most n characters from s2
into s1. If s2 is less than n characters long, the remainder of s1 is
filled with `\0' characters. Otherwise, s1 is not terminated.

The source and destination strings should not overlap, as the behavior is
undefined.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Strcpy behaviour

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap