473,799 Members | 2,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access Violation rtrim function

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[i] != ' ')
{
break;
}

}

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

} // function trim()

thank you in advance.
alan

Feb 28 '06 #1
9 2668

"alan" <eh*****@gmail. com> schrieb im Newsbeitrag
news:11******** ************@p1 0g2000cwp.googl egroups.com...
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[i] != ' ')
{
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~
Feb 28 '06 #2
JE

alan wrote:
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[i] != ' ')
{
break;
}

}

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

} // function trim()

thank you in advance.
alan


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

JE

Feb 28 '06 #3
hey this was the reason, i have to initialize my initial string with []
and thank you for the good website.

Feb 28 '06 #4
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:~$

Feb 28 '06 #5

"hde" <hd*@foobar-qux.org> schrieb im Newsbeitrag
news:11******** **************@ z34g2000cwc.goo glegroups.com.. .
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~
Feb 28 '06 #6
hde
> thats how microsoft has interpreted the scoping rules long ago :(
everithing in for and while is not in it

What a good way to make unprotable code.

Feb 28 '06 #7
In article <1141095024.865 808.79250
@p10g2000cwp.go oglegroups.com> , hd*@foobar-qux.org
says...
thats how microsoft has interpreted the scoping rules long ago :(
everithing in for and while is not in it

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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 28 '06 #8
JE wrote:
alan wrote:
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[i] != ' ')
{
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
--
I'm not just a number. To many, I'm known as a string...
Feb 28 '06 #9

"alan" <eh*****@gmail. com> wrote in message
news:11******** ************@p1 0g2000cwp.googl egroups.com...
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[i] != ' ')
{
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!)


Feb 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
18033
by: Steven Reddie | last post by:
I understand that access violations aren't part of the standard C++ exception handling support. On Windows, a particular MSVC compiler option enables Microsoft's Structured Exception Handling (SEH) in C++ EH so that a catch (...) will catch an access violation. I don't know if other platforms support something similar. I'm wondering about how to best protect an application or library from poorly written user-defined callbacks. It...
3
6813
by: Kyle Teague | last post by:
I have a list of pointers to structs as a private member of a class. If I call begin() in the same function as I added the data then no access violation occurs. However, if I try to call begin() in a member function of the same class I get a memory access violation. For example: // this is fine, no error void CBase::FuncA( void ) { plugin_info_t *plugin_info = new plugin_info_t;
3
10702
by: Andy B | last post by:
I've tried using Trim or RTrim to strip trailing space characters from my data. When I check on the transformed data space characters are still there. We have an address table containing two fields: BuildName and RoadName. Both have the following properties: size 50, not indexed, not required, allowed zero length. Some records have BuildName, RoadName as null, some have content. No content is 50 chr long. When i run a Len(BuildName)...
7
3142
by: Daniel | last post by:
I want to write a method to remove the last node of the linked list. But the error "Access Violation" exists and the error point to this method. What does it means by Access Violation and how can I debug it? Thanks void RemoveNode(StepNodePtr pList) { StepNodePtr preq, q; q = pList; preq = pList;
7
1672
by: Bonj | last post by:
Hi I have a mixed managed/unmanaged project which thanks to you guys I've managed to get rid of the linker errors of, so cheers for that. But now I'm experiencing an unknown access violation. Firstly this is an extended stored procedure DLL, I've been able to debug it successfully by starting SQL server with the -c switch and when it is called via osql then it breaks into the debugger. Fine. I'm happy with that.... However the project...
0
1142
by: mkarim | last post by:
I am running this query: UPDATE REPORT_CLIENT RC SET ca_move = (SELECT UNIQUE (CASE WHEN rtrim(ltrim(ca_vormonat)) = '' OR length(rtrim(ltrim(ca_vormonat))) IS NULL OR rtrim(ltrim(ca_vormonat)) = '' OR length(rtrim(ltrim(ca_vormonat))) IS NULL OR upper(rtrim(ltrim(ca_vormonat))) = upper(rtrim(ltrim(ca_move))) THEN '' ELSE 'X' END) FROM REPORT_CLIENT RC) It runs fine in Oracle, but gives the error "single-query returned multiple rows"...
2
2201
by: jafastinger | last post by:
I have a large union. If I break it into its individual parts they all run quick. The longest is the last select it takes 2 minutes to fetch all rows. When I run the query below it does not come back for quite some time. 20 minutes. There are very few duplicates. When I run the sql's individually the first two give no warning but the third does(SQL0437W Performance of this complex query may be sub-optimal. Reason code:"1". ...
1
13907
by: =?Utf-8?B?c2F0aGVlc2t1bWFy?= | last post by:
In my project i have a component named PTS Engine which is developed in VC++ ..Net 2003. And it is migrated to VC++.NET 2005 version. Initially this migration process has coded and tested in VC++ .NET 2005 Beta version. In beta version everything is working fine. When i tryied to run in .NET 2005 full version i am facing the following access violation Error. "Unhandled exception at 0x4bc0145c in Engine.exe: 0xc0000005: Access Violation...
12
1400
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? ----------------------------------------------------------------------- Using Regular Expressions (JavaScript 1.2/JScript 3+) : String.prototype.lTrim = function() { return this.replace(/^\s+/,'');
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.