#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Perth Amboy
    Posts
    15
    Rep 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!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Perth Amboy
    Posts
    15
    Rep Power
    0
    Thanks alot! I knew I was missing something!

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

    Again thanks!
  6. #4
  7. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    new is just the c++ version of memory allocation in c that is a little better automated.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    someString = (char*)malloc(strlen(anotherString)+1);

    Change that line to:

    someString = new char[ strlen(anotherString)+1];
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    Originally posted by EnigmaedgE
    Ok...how is this achieved using "new"?
    It's in the comment following the malloc statement.

IMN logo majestic logo threadwatch logo seochat tools logo