|
-
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.
-
July 30th, 2005, 01:59 PM
#2
Re: "new" issue... cutting off part of another string!
correction... 'str' is cut down to 12 characters
-
July 30th, 2005, 02:25 PM
#3
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" 
-
July 30th, 2005, 03:50 PM
#4
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
-
July 30th, 2005, 03:55 PM
#5
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" 
-
July 30th, 2005, 04:14 PM
#6
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.
-
July 30th, 2005, 04:19 PM
#7
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."
-
July 30th, 2005, 04:21 PM
#8
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" 
-
July 30th, 2005, 04:25 PM
#9
Re: "new" issue... cutting off part of another string!
 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.
-
July 31st, 2005, 09:36 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|