CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2002
    Location
    USA
    Posts
    150

    Question "new" issue... cutting off part of another string!

    Theres this crazy error that happens when I call my string function and try to allocate memory to a new string... it cuts off part of the string passed in! Here's my code and output:

    CODE:
    char* strUcase(char* str)
    {
    int intLen, i;
    char* strTemp;
    intLen= strlen(str);

    //HERE IS WHERE THE PROBLEM HAPPENS!
    cout<<"str:"<<str<<endl;
    strTemp = new char(intLen);
    cout<<"str:"<<str<<endl;
    //ALL THE SUDDEN 'STR' IS CUT DOWN TO 15 CHARS!

    for(i=0; i<intLen; i++)
    {
    strTemp[i]=toupper(str[i]);
    }

    return strTemp;
    }


    int main(int argc, char *argv[])
    {
    char *strTemp = new char(50);
    char *strTemp2 = new char(50);
    strcpy(strTemp2," hello world ");
    strcpy(strTemp,"sdaf");

    cout<<strTemp2<<endl;
    strTemp2 = strUcase(strTemp2);
    cout<<strTemp2<<endl;

    return EXIT_SUCCESS;
    }

    OUTPUT:

    hello world
    str: hello world
    str: hello wo
    HELLO WO
    Press Enter to continue!


    Any ideas on the problem? It should be noted I'm doing this in Linux. I wonder if the same thing would happen in VC++...
    Thanks in advance.

  2. #2
    Join Date
    Jan 2002
    Location
    USA
    Posts
    150

    Re: "new" issue... cutting off part of another string!

    correction... 'str' is cut down to 12 characters

  3. #3
    Join Date
    May 2005
    Location
    Kuwait
    Posts
    65

    Re: "new" issue... cutting off part of another string!

    It is because you havent allocated memory for the null terminating character '\0'

    Here is the corrected program:
    Code:
    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>
    
    char* strUcase(char* str)
    {
    int intLen, i;
    char* strTemp;
    intLen= strlen(str);
    
    //HERE IS WHERE THE PROBLEM HAPPENS!
    cout<<"str:"<<str<<endl;
    strTemp = new char(intLen+1);
    cout<<"str:"<<str<<endl;
    //ALL THE SUDDEN 'STR' IS CUT DOWN TO 15 CHARS!
    
    for(i=0; i<intLen; i++)
    {
    strTemp[i]=toupper(str[i]);
    }
    strTemp[i] = '\0';
    return strTemp;
    }
    
    
    int main(int argc, char *argv[])
    {
    char *strTemp = new char(50);
    char *strTemp2 = new char(50);
    strcpy(strTemp2," hello world ");
    strcpy(strTemp,"sdaf");
    
    cout<<strTemp2<<endl;
    strTemp2 = strUcase(strTemp2);
    cout<<strTemp2<<endl;
    
    return 0;
    }
    BJxtreme
    If you like a post, rate it

    "Flattery is the art of telling another person exactly what he thinks of himself"

  4. #4
    Join Date
    Jan 2002
    Location
    USA
    Posts
    150

    Re: "new" issue... cutting off part of another string!

    Thansk for the reply! Can you explain why the language would cut off part of the OTHER string though? I'm just trying to understand string memory issues and things... thanks

  5. #5
    Join Date
    May 2005
    Location
    Kuwait
    Posts
    65

    Re: "new" issue... cutting off part of another string!

    I think each compiler makes the results differ.. On my Visual C++ 6, this was the output of your code:
    Code:
     hello world
    str: hello world
    str: hello world
     HELLO WORLD ☺
    That output can be explained more better .. because the string was not terminated, another character came (that character comes from some part of the memory that was used by another program)
    BJxtreme
    If you like a post, rate it

    "Flattery is the art of telling another person exactly what he thinks of himself"

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: "new" issue... cutting off part of another string!

    You are not allocating memory correctly. Look at one of youe lines :
    Code:
    char *strTemp = new char(50);
    This only allocates ONE byte of memory at initizlizes it to 50. It
    would have the same effect as :

    Code:
    char *strTemp = new char;
    *strTemp = 50;
    What you want is ...
    Code:
    char *strTemp = new char[50] ;
    Also, you have a memory leak since you never de-allocate the memory.
    I recommend using std::string instead.
    Last edited by Philip Nicoletti; July 30th, 2005 at 04:19 PM.

  7. #7
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: "new" issue... cutting off part of another string!

    You wrote strTemp = new char(intLen); instead of strTemp = new char[intLen];, which in fact should be strTemp = new char[intLen+1];.

    Anyway, are you moving from Java? Don't use new. Especially the way you do. For strings use std::string. For other memory allocation use containers. Read about RAII.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  8. #8
    Join Date
    May 2005
    Location
    Kuwait
    Posts
    65

    Re: "new" issue... cutting off part of another string!

    Oh.. yeah.. Philip's right.. I didn't see that part.. so you will have to combine both these for the perfect one :
    Code:
      char* strUcase(char* str)
     {
     int intLen, i;
     char* strTemp;
     intLen= strlen(str);
     
     //HERE IS WHERE THE PROBLEM HAPPENS!
     cout<<"str:"<<str<<endl;
     strTemp = new char[intLen+1];
     cout<<"str:"<<str<<endl;
     //ALL THE SUDDEN 'STR' IS CUT DOWN TO 15 CHARS!
     
     for(i=0; i<intLen; i++)
     {
     strTemp[i]=toupper(str[i]);
     }
     strTemp[i] = '\0';
     return strTemp;
     }
     
     
     int main(int argc, char *argv[])
     {
     char *strTemp = new char[50];
     char *strTemp2 = new char[50];
     strcpy(strTemp2," hello world ");
     strcpy(strTemp,"sdaf");
     
     cout<<strTemp2<<endl;
     strTemp2 = strUcase(strTemp2);
     cout<<strTemp2<<endl;
     
     return 0;
     }
    Last edited by bijuabrahamp; July 30th, 2005 at 04:26 PM.
    BJxtreme
    If you like a post, rate it

    "Flattery is the art of telling another person exactly what he thinks of himself"

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: "new" issue... cutting off part of another string!

    Quote Originally Posted by prosh0t
    Thansk for the reply! Can you explain why the language would cut off part of the OTHER string though? I'm just trying to understand string memory issues and things... thanks
    May I ask why you're doing things this way (using 'C' string)? What's wrong with using std::string? Then you don't have any of these problems.
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    std::string strUcase(const std::string& str)
    {
        int intLen, i;
        std::string strTemp;
        intLen= str.length();
    
       //HERE IS WHERE THE PROBLEM HAPPENS!
       cout<<"str:"<<str<<endl;
       strTemp = str;
       for(i=0; i<intLen; i++)
           strTemp[i]=toupper(strTemp[i]);
       return strTemp;
    }
    
    
    int main(int argc, char *argv[])
    {
       string strTemp;
       string strTemp2 = "hello world";
       strTemp = "sdaf";
       cout<<strTemp2<<endl;
       strTemp2 = strUcase(strTemp2);
       cout<<strTemp2<<endl;
    }
    Also, use code tags when posting code.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 30th, 2005 at 04:28 PM.

  10. #10
    Join Date
    Jan 2002
    Location
    USA
    Posts
    150

    Re: "new" issue... cutting off part of another string!

    Thanks for the help everyone! I'm actually just trying to understand C strings better and work with them a little more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured