|
-
July 30th, 2005, 01:56 PM
#1
"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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|