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 June 5th, 2003, 01:54 PM
EnigmaedgE EnigmaedgE is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Location: Perth Amboy
Posts: 15 EnigmaedgE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 31 sec
Reputation Power: 0
Simple easy question...what am I overlooking?

hey guys... simple question but I can't figure it out. I'm trying to copy the CONTENTS of the string, and not the pointer.

char* someString;
char* anotherString = "devshed";

if i try to do a

strcpy(someString, anotherString);


I get segfault. if it was changed to

char someString[] = "";

i dont get any errors, it copys the contents. I dont want to acheive this

someString = anoterString;

There has to be a way to copy the contents of pointer characters. So what am I missing?

Thanks in advance guys!

Reply With Quote
  #2  
Old June 5th, 2003, 02:28 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,141 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 23 h 52 m 21 sec
Reputation Power: 1974
You need to have some place to put the string first. So, in your example:
Code:
char* someString;
char* anotherString = "devshed";

The char pointer someString has not been initialized. If you declared it outside of a function, then it most likely contains the value 0x0000:0x0000, which has it pointing right at the beginning of the Interrupt Vector Table. If you start copying strings there, you're guaranted to crash the system, which is one reason why it's protected (ie, by the segmentation fault, which says that you are trying to access memory that is not yours to access -- in the good old days of DOS, the error message would have been the system crashing).

So, you need to allocate enough memory to contain the string and then have someString point to it. In the case of anotherString, the compiler did this for you; in static memory it allocated a string 8 characters long, copied "devshed" into it followed by a NUL (the null-terminator, '\0', which marks the end of the string -- this is an absolute necessity in C/C++ strings), and then placed the address of that string in anotherString. So, your strcpy code should read something like:
Code:
char* someString;
char* anotherString = "devshed";

someString = (char*)malloc(strlen(anotherString)+1); 
      // always need to add one for the null-terminator
      // in C++, use new instead of malloc:
     //    someString = new char [strlen(anotherString)+1]; 
strcpy(someString, anotherString);  // now the string has somewhere to go!

When you tried this:
Code:
char someString[] = "";

you did the same as you did above with anotherString, except the compiler only allocated one byte and that single byte contains the null-terminator. Then right after it in memory are other of your program's variables. So when you strcpy the string into that single-char buffer, the rest of the string overwrites the variables that follow it in memory. This is called "clobbering". What's even more fun is when you clobber local variables inside of a function, because then you very well could also be clobbering the function's return address, which will cause the program to "mysteriously" crash -- yeah, I've done that trick a few times! That is why when you do a strcpy or sprintf or the like, you want to be very sure that the buffer you are copying or writing to is big enough to hold the string.

BTW, this form of clobbering is also called "buffer overflow" and "over-running a buffer", which is a common program error that hackers exploit to take over systems; basically, they overflow the buffer and insert into the right location a return address that points to their own code that they've inserted (they studied just where that would be in order to perform the exploit). So a hot topic now is how to write your C/C++ code so that it protects itself against buffer overflow.

Hope all that helps!

Last edited by dwise1_aol : June 5th, 2003 at 02:32 PM.

Reply With Quote
  #3  
Old June 5th, 2003, 03:32 PM
EnigmaedgE EnigmaedgE is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Location: Perth Amboy
Posts: 15 EnigmaedgE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 31 sec
Reputation Power: 0
Thanks alot! I knew I was missing something!

Ok...how is this achieved using "new"?

Again thanks!

Reply With Quote
  #4  
Old June 5th, 2003, 04:17 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed God (5000 - 5499 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 5,163 Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 6 Days 1 h 34 m 20 sec
Reputation Power: 791
new is just the c++ version of memory allocation in c that is a little better automated.

Reply With Quote
  #5  
Old June 5th, 2003, 11:30 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
someString = (char*)malloc(strlen(anotherString)+1);

Change that line to:

someString = new char[ strlen(anotherString)+1];

Reply With Quote
  #6  
Old June 6th, 2003, 01:09 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,141 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 23 h 52 m 21 sec
Reputation Power: 1974
Quote:
Originally posted by EnigmaedgE
Ok...how is this achieved using "new"?

It's in the comment following the malloc statement.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Simple easy question...what am I overlooking?

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