-
May 1st, 2011, 03:34 PM
#1
Pointer write error
Hi,
I am trying to learn C++ programming inside VS2010. I have a impossible error at line highlighted with ****. After for loop, the data can not be written inside the memory that is pointed. What kind of problem it is i could not understand. Then i tested with Borland C 5.02 it is working cool. is it some kind of joke or a overprotection remote control i could not understand
i will be very glad if you can help me about this issue
-------------------------------------------------------------------------------------------------
#include <math.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
char *Trim(char *inputText);
int main(int argc, char* argv[])
{
char *inputText=" Hello World ";
printf("%s\n",Trim(inputText));
getch();
return 0;
}
char *Trim(char *inputText){
char *inText = new char [strlen(inputText)];
int s=0;
int e=0;
for (int i=0;i<strlen(inputText);i++){
if(*(inputText+i)!=' '){
s=i;
break;
}
}
for (int i=0;i<strlen(inputText);i++){
if(*(inputText+strlen(inputText)-i-1)!=' '){
e=strlen(inputText)-i-1;
break;
}
}
int p=0;
for (int p=s;p<e+1;p++){
*(inText+p-s)=*(inputText+p);
}
*(inText+p-s)='\0'; //'****
return inText;
}
-
May 1st, 2011, 03:39 PM
#2
Re: Pointer write error
Originally Posted by algea
Hi,
I am trying to learn C++ programming inside VS2010.
Writing code as you've written is not necessary for a C++ program.
There is a standard string class in C++ called std::string . Why are you not using that?
Secondly, please use code tags when posting code.
Regards,
Paul McKenzie
-
May 1st, 2011, 03:44 PM
#3
Re: Pointer write error
Originally Posted by algea
Then i tested with Borland C 5.02 it is working cool. is it some kind of joke or a overprotection remote control i could not understand
Your program always had a bug -- the issue is that a bug in C++ doesn't mean you'll always see the error.
When you make a mistake with pointers, memory handling, etc. there may be no indication that something is wrong. The Visual C++ compiler finally exposes the bug to you that always existed.
Regards,
Paul McKenzie
-
May 1st, 2011, 03:56 PM
#4
Re: Pointer write error
Code:
*(inText+p-s)='\0'; //'****
Code:
char *inputText=" Hello World ";
printf("%s\n",Trim(inputText));
The inputText is a string-literal. In the Trim() function, you are attempting to change the value of a string literal. This is undefined behaviour.
This very same code will also crash:
Code:
int main()
{
char *p = "abc123";
p[0] = 'x'; // error, may crash, may not crash, who knows?
}
This code has undefined behaviour, as p is a string-literal constant that cannot be changed. See what I mean? A bug existed, and you never knew it.
The proper solution is to do this:
Code:
int main()
{
char p[] = "abc123";
p[0] = 'x'; // ok
}
Now that p is a writeable array of char, then it can be changed safely.
But all of this aside, the std::string class replaces all of that code you wrote. If you want a simple Trim function, it is much easier using the std::string functions than writing low-level 'C' code.
Also, Borland 5.02 is a non-standard C++ compiler written close to 15 years ago. If you are learning C++, drop that compiler, and learn C++ as it has been since 1998 (when it became standard).
Regards,
Paul McKenzie
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
|