Thread: How do strings get accidentally get modified

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    How do strings get accidentally get modified

    I can't think of situation or coding example when a string might get accidentally modified. Can someone point me in the right direction.

    Chad

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    char foo[10];
    char bar[] = "this is a test";
    strcpy( foo, "this is a long message" );
    This may or may not cause bar to change unexpectedly.

    char bar[] = "this is a test";
    char foo[10];
    strcpy( foo, "this is a long message" );
    This may or may not cause bar to change unexpectedly.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    the best example for you is the strtok fucntion which actuall modifies the string. a sample code might make u clear

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
        char str[15]="hello world";
        char temp[15];
        char *p;
        
        strcpy(temp,str);
        
        if((p = strtok(str," "))!=NULL)
            printf("%s\n",p);
        
        printf("%s\n",str);    <---  the original string is changed here
        printf("%s\n",temp);  <-- the backup string is not changed
        
        
        getchar();
        return 0;
    }
    /*myoutput
    hello
    hello
    hello world
    */
    ssharish2005

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by cdalten
    Iwhen a string might get accidentally modified.
    Chad
    Most common is buffer overflow, attempting to copy more characters into a buffer than the buffer can hold. Salem's first example is a good example. It can happen also with functions such as strcpy(), scanf(), fscanf(), and gets() -- none of these have any limits at all on the size of character buffers.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    On salem's example:

    char bar[] = "this is a test";
    char foo[10];
    strcpy( foo, "this is a long message" );
    This may or may not cause bar to change unexpectedly.

    How can "this is a long message" impact bar?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if bar[] immediately follows foo[] in memory, then overstepping foo will start writing in bar.

    Most compilers allocate variables pretty close to one another, but the exact ordering and space between them obviously varies from one compiler to another (and even with different compiler options).

  7. #7
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125

    Stomping on memory

    As Salem has said, it might impact bar if bar is adjacent to foo in memory.

    But also keep in mind, if bar is not adjacent to foo in memory, that buffer overflow (writing far too many characters into a too small buffer) will stomp on whatever memory *is* adjacent to foo, and that can cause wildly unexpected results -- and not necessarily right there in that part of the code.

    That's when your job as a programmer gets REALLY interesting...

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So beginneth the lesson "why does my debug code work and my release code doesn't".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM