Strange string behavior

This is a discussion on Strange string behavior within the C Programming forums, part of the General Programming Boards category; Perhaps I'm doing this wrong but I'm getting weird behavior. Basically I have a prefix string, I copy it into ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    Strange string behavior

    Perhaps I'm doing this wrong but I'm getting weird behavior. Basically I have a prefix string, I copy it into a larger message. Then I print it's length which is correct. Then I try and add a bunch of letter a's onto the end of the message. My thought would be that the length of the message would increase by 1 everytime however I have output that looks like this:
    .
    .
    .
    message has length 1990
    message has length 1991
    message has length 1994
    message has length 1994
    message has length 1994
    message has length 1995
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2017
    message has length 2018
    message has length 2019
    message has length 2056
    message to send is 2056

    There are sections where numbers are skipped or repeated. This doesn't really make sense to me.

    Code:
      char *prefix = "12345678901234";
      char message[2048];
      strcpy(message,prefix);
      printf("message prefix is %s with length %d\n",message,strlen(message));
      int i = strlen(message);
      while(strlen(message)<=2048)
      {
      	message[i]='a';
    	i++;
    	printf("message has length %d\n",strlen(message));
      }

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Well, message is uninitialized, so it is filled with garbage data. strlen reads length to null-terminator. You are just inserting an 'a' at the end, and not adding a '\0' after it, so strlen might go past the 'a' you added and stop at whatever '\0' was left in memory.
    Last edited by neandrake; 04-07-2009 at 07:17 PM. Reason: verbage-check
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    That makes sense. I'm still getting used to C. I did a memset and set everything to \0 first and it worked. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  3. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21