new char issues

This is a discussion on new char issues within the C++ Programming forums, part of the General Programming Boards category; Hi I am trying to use strtok. The problem is that when I dynamically allocate an array of two characters ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    205

    new char issues

    Hi
    I am trying to use strtok. The problem is that when I dynamically allocate an array of two characters as below, it would seem a bigger array is created.
    Code:
    input = new char[2]; /*get the length of the string*/
    This is what I get from the debugger:
    input = 0x00362aa8 "══¤¤¤¤ллллллллю■"

    I tried with different sizes and it would seem there are always a certain number of I which correspond to the size with the extra stuff at the end.
    The issue is that strtok gives the last token with these garbage values and I can't figure out what to do. Thanks
    Amish

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You have to set the null terminator for the debugger to show the string properly. In your code example, input can hold a string of length one because it needs the second spot for the null terminator. As always, the C++ string class is generally preferred for string processing.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    This is what I get from the debugger:
    input = 0x00362aa8 "══¤¤¤¤ллллллллю■"

    I tried with different sizes and it would seem there are always a certain number of I which correspond to the size with the extra stuff at the end.
    This:

    0x00362aa8

    is the address in memory that is stored in input. This:

    "══¤¤¤¤ллллллллю■"

    is the value at that address in memory.

    The issue is that strtok gives the last token with these garbage values and I can't figure out what to do.
    strtok() as well as the rest of the functions in <cstring> require null terminated char arrays--otherwise known as c-strings. All those funtions have loops like:
    Code:
    while(str[i] != '\0')
    {
         //do something
         ++i;
    }
    Last edited by 7stud; 03-06-2006 at 06:37 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Technically, it is the sequence of characters starting at that address until a null character is found (or until the debug window gets tired of showing weird characters and stops).

    Since the string is uninitialized, unknown data is in that memory. In debug builds, often the same "unknown" data is used to aid in debugging, which is why you usually see the I.

  5. #5
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    "dynamically allocate"

    I hate this phrase, sure it was dynamic back then, but now it just seems silly to call it dynamic.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    I created the array with one more charater at the end and filled it with 0. Thanks. Incidentally I was wondering if there was any easy way to convert from const char * to char *. or from std::string to char *. Thanks,
    Amish

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Quote Originally Posted by loopshot
    "dynamically allocate"

    I hate this phrase, sure it was dynamic back then, but now it just seems silly to call it dynamic.
    How so? And what else would you call it?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Incidentally I was wondering if there was any easy way to convert from const char * to char *. or from std::string to char *
    Code:
    /*
    const char* str = "hello";
    char* ptr = const_cast<char*>(str);
    ptr[0] = 'a';
    
    No compiler error, however program will crash because 
    string literals are constants by definition, and you can't change them.*/ 
    
    const char str[] = "hello";  
    char* ptr = const_cast<char*>(&str[0]);
    ptr[0] = 'a';
    cout<<ptr<<endl;
    cout<<str<<endl<<endl;
    //The string literal is copied char by char into str by operator=
    //so str can be changed without violating any C++ rules, i.e. str isn't 
    //a pointer to a string literal; it's just an array with some chars in it
    
    string myStr = "hello";
    char* strPtr = const_cast<char*>(myStr.c_str());
    strPtr[0] = 'a';
    cout<<strPtr<<endl;
    cout<<myStr<<endl;
    You shouldn't need to be casting away const.
    Last edited by 7stud; 03-07-2006 at 03:44 AM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    To convert from const char* or std::string to non-const char*, use strcpy to copy the string. If you need to modify the actual string, then you should find another method that doesn't involve converting to char*.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

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