Access Violation rtrim function

A

alan

Hello,
can somebody help me with this self written rtrim function?
i do always get a access violation on the line where i want to
terminate the string:
str[i+1] = '\0'; why can't i acces this specific position?

void rtrim(char *str)
{
for( int i=strlen(str)-1; i>= 0; i-- )
{
if( str != ' ')
{
break;
}

}

str[i+1] = '\0';

} // function trim()

thank you in advance.
alan
 
F

Frank Schmidt

alan said:
Hello,
can somebody help me with this self written rtrim function?
i do always get a access violation on the line where i want to
terminate the string:
str[i+1] = '\0'; why can't i acces this specific position?

void rtrim(char *str)
{
for( int i=strlen(str)-1; i>= 0; i-- )
{
if( str != ' ')
{
break;
}

}

str[i+1] = '\0';

} // function trim()

thank you in advance.
alan


i see nothing wrong.

can it be that your test string is the problem? see
http://www.c-faq.com/decl/strlitinit.html


f~
 
J

JE

alan said:
Hello,
can somebody help me with this self written rtrim function?
i do always get a access violation on the line where i want to
terminate the string:
str[i+1] = '\0'; why can't i acces this specific position?

void rtrim(char *str)
{
for( int i=strlen(str)-1; i>= 0; i-- )
{
if( str != ' ')
{
break;
}

}

str[i+1] = '\0';

} // function trim()

thank you in advance.
alan


Maybe the scope of 'i' is restricted to the for loop?

JE
 
A

alan

hey this was the reason, i have to initialize my initial string with []
and thank you for the good website.
 
H

hde

hmm interesting how are you using i outside of the for loops namespace?

#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

int main() {
for (int i = 0; i < 10; i++)
cout << i << endl;
cout << ++i << endl;
return 0;
}

hde@Xtop:~$ g++ -o test ./test.cpp
../test.cpp: In function $-1òøint main()òù:
../test.cpp:9: error: name lookup of $-1òøiòù changed for new ISO
òøforòù scoping
../test.cpp:7: error: using obsolete binding at $-1òøiòù
hde@Xtop:~$
 
F

Frank Schmidt

hde said:
hmm interesting how are you using i outside of the for loops namespace?

thats how microsoft has interpreted the scoping rules long ago :(
everithing in for and while is not in it

f~
 
J

Jerry Coffin

@p10g2000cwp.googlegroups.com>, (e-mail address removed)
says...
What a good way to make unprotable code.

Actually, when MS started doing that, it was portable,
because it's how _all_ C++ compilers worked. It was years
later during standardization that they decided to change
the rules.

The main thing that's happened here is mostly that MS
VC++ 6.0 (in particular) has stayed in use even though
it's extremely old (about 8 years old now). Even it
actually supports the correct scope for variables
declared in loops, but to get that behavior you have to
set it to follow the rules as well as it can -- and when
you do that, it can no longer compile most of its own
headers, so it's of no practical use.
 
B

Ben Pope

JE said:
alan said:
Hello,
can somebody help me with this self written rtrim function?
i do always get a access violation on the line where i want to
terminate the string:
str[i+1] = '\0'; why can't i acces this specific position?

void rtrim(char *str)
{
for( int i=strlen(str)-1; i>= 0; i-- )
{
if( str != ' ')
{
break;
}

}

str[i+1] = '\0';

} // function trim()

thank you in advance.
alan


Maybe the scope of 'i' is restricted to the for loop?


....not by default until MSVC8. I guess they're using a previous MS
compiler.

Ben Pope
 
H

Howard

alan said:
Hello,
can somebody help me with this self written rtrim function?
i do always get a access violation on the line where i want to
terminate the string:
str[i+1] = '\0'; why can't i acces this specific position?

void rtrim(char *str)
{
for( int i=strlen(str)-1; i>= 0; i-- )
{
if( str != ' ')
{
break;
}

}

str[i+1] = '\0';

} // function trim()

thank you in advance.
alan


It is not correct to use a variable after a loop when it is defined inside
the loop. VC++ version 6 and earlier allowed this by default, but it's
still not correct.

The problem, however, might be that you haven't allowed enough space in the
array for an extra character at the end. If there are no spaces at the end
of your string, then str[i+1] will point one past the end of the string.
Unless the array you're modifying already has enough room for that extra
character, then writing that extra character will cause undefined behavior,
and is likely to crash. (Although, just about anything _might_ happen in
practice, including appearing to work correctly!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top