Click to See Complete Forum and Search --> : "new" issue... cutting off part of another string!
prosh0t
July 30th, 2005, 01:56 PM
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.
prosh0t
July 30th, 2005, 01:59 PM
correction... 'str' is cut down to 12 characters
bijuabrahamp
July 30th, 2005, 02:25 PM
It is because you havent allocated memory for the null terminating character '\0'
Here is the corrected program:
#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;
}
prosh0t
July 30th, 2005, 03:50 PM
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 :)
bijuabrahamp
July 30th, 2005, 03:55 PM
I think each compiler makes the results differ.. On my Visual C++ 6, this was the output of your 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)
Philip Nicoletti
July 30th, 2005, 04:14 PM
You are not allocating memory correctly. Look at one of youe lines :
char *strTemp = new char(50);
This only allocates ONE byte of memory at initizlizes it to 50. It
would have the same effect as :
char *strTemp = new char;
*strTemp = 50;
What you want is ...
char *strTemp = new char[50] ;
Also, you have a memory leak since you never de-allocate the memory.
I recommend using std::string instead.
RoboTact
July 30th, 2005, 04:19 PM
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.
bijuabrahamp
July 30th, 2005, 04:21 PM
Oh.. yeah.. Philip's right.. I didn't see that part.. so you will have to combine both these for the perfect one :
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;
}
Paul McKenzie
July 30th, 2005, 04:25 PM
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.
#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
prosh0t
July 31st, 2005, 09:36 AM
Thanks for the help everyone! I'm actually just trying to understand C strings better and work with them a little more.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.