Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Access Violation Problem

Reply
Thread Tools

Access Violation Problem

 
 
Mike Copeland
Guest
Posts: n/a
 
      06-02-2012
I have the following code that compiles but produces a "Access
violation reading location..." runtime error when executed. I tried
casting the first statement to (char*), but that produced the same
fault.
I suppose the problem lies with the use of the "const char" in the
parameter declaration, but I've been trying to follow the examples I see
here that always seem to declare incoming parameters as "const"). 8<{{
Other than that, I don't know how to deal with this problem. (I'm
using VC 2010 Express here.) Please advise. TIA

char* TTB(const char *str) // Truncate Trailing Blanks
{
string wStr = str; // faults at this statement
wStr = trim_right(wStr);
return (char*)wStr.c_str();
} // TTB

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      06-02-2012
On 06/ 3/12 08:44 AM, Mike Copeland wrote:
> I have the following code that compiles but produces a "Access
> violation reading location..." runtime error when executed. I tried
> casting the first statement to (char*), but that produced the same
> fault.
> I suppose the problem lies with the use of the "const char" in the
> parameter declaration, but I've been trying to follow the examples I see
> here that always seem to declare incoming parameters as "const"). 8<{{
> Other than that, I don't know how to deal with this problem. (I'm
> using VC 2010 Express here.) Please advise. TIA
>
> char* TTB(const char *str) // Truncate Trailing Blanks


Why don't you use a more expressive name?

> {
> string wStr = str; // faults at this statement


What is a "string", a std::string or something else? There's nothing
wrong in initialising a std::string from a const char*. What does the
name "wStr" mean?

> wStr = trim_right(wStr);
> return (char*)wStr.c_str();


Assuming wStr is a std::string, this will end in tears. You are
returning the address of some local (to the function) data.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Alain Ketterlin
Guest
Posts: n/a
 
      06-02-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) (Mike Copeland) writes:

> I have the following code that compiles but produces a "Access
> violation reading location..." runtime error when executed. I tried
> casting the first statement to (char*), but that produced the same
> fault.
> I suppose the problem lies with the use of the "const char" in the
> parameter declaration, but I've been trying to follow the examples I see
> here that always seem to declare incoming parameters as "const"). 8<{{
> Other than that, I don't know how to deal with this problem. (I'm
> using VC 2010 Express here.) Please advise. TIA
>
> char* TTB(const char *str) // Truncate Trailing Blanks
> {
> string wStr = str; // faults at this statement
> wStr = trim_right(wStr);
> return (char*)wStr.c_str();
> } // TTB


I can't see why the first statement should be wrong (without knowing how
you call TTB[*]), but I'm sure the last one is wrong: you're returning
a pointer to data that is deallocated before the actual return. You
can't use the return value of TTB.

-- Alain.

P/S: maybe you have another function on the same model removing leading
blanks, the result of which you pass to TTB...
 
Reply With Quote
 
Balog Pal
Guest
Posts: n/a
 
      06-02-2012
"Mike Copeland" <(E-Mail Removed)>

> I have the following code that compiles but produces a "Access
> violation reading location..." runtime error when executed.


> char* TTB(const char *str) // Truncate Trailing Blanks
> {
> string wStr = str; // faults at this statement
> wStr = trim_right(wStr);
> return (char*)wStr.c_str();
> } // TTB


The pointer you get from wStr.c_str() expression becomes invalid as soon as
you return from the function. So if you use it for anything it is undefined
behavior -- access violation is fair game.

Also c_str() returns const char * that you're not supposed to unconstify.

Try to play by the rules if you want a program that actually works.

 
Reply With Quote
 
Tobias Müller
Guest
Posts: n/a
 
      06-03-2012
Paavo Helde <(E-Mail Removed)> wrote:
> (E-Mail Removed) (Mike Copeland) wrote in
> news:(E-Mail Removed)-september.org:
>
>>
>> char* TTB(const char *str) // Truncate Trailing Blanks
>> {
>> string wStr = str; // faults at this statement
>> wStr = trim_right(wStr);
>> return (char*)wStr.c_str();
>> } // TTB

>
> This function is invalid as noted by others, and also totally useless;
> delete it and replace all calls to it with calls to trim_right(). Update
> the calling code to receive a std::string instead of char*.


In the general case I find it more convenient to use "const char*" for
parameters instead of "const std::string&" because the function is more
universally usable like that. "const std::string&" -> "const char*" is
easier and faster than the other way. You can also use other string
classes, since probably all of them have "operator const char*()" defined.

My rules are:
- for parameters, use the most general/abstract types
- for return values and local variables, use the most specific/concrete
types

Tobi
 
Reply With Quote
 
Tobias Müller
Guest
Posts: n/a
 
      06-03-2012
Paavo Helde <(E-Mail Removed)> wrote:
> =?UTF-8?Q?Tobias=20M=C3=BCller?= <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
>> In the general case I find it more convenient to use "const char*" for
>> parameters instead of "const std::string&" because the function is
>> more universally usable like that. "const std::string&" -> "const
>> char*" is easier and faster than the other way. You can also use other
>> string classes, since probably all of them have "operator const
>> char*()" defined.

>
>
> This way you will lose the length information and are doomed to recalculate it;


Many algorithms don't need it or are traversing the entire string anyways.

Also recalculation is cheaper than creating a temporary std::string object
in case of const char*.

> you will also lose the ability to process strings with embedded zeroes.


Never needed that. If you do, you cannot use any third party C libraries,
and I suspect that many C++ libraries are not safe too.

> Also, calling code would be more complex as there is an automatic
> conversion from const char* to const std::string&, but not vice versa.


But the conversion exists and is cheap.

>
> If you want to support different string types, then templates would be the
> right approach.


I could do that, but then I would need some kind of string class traits
first. This would probably be difficult for const char*.

Anyway, it's probably a matter of taste and depends on the frequency of
std::string and const char* in the rest of the code.

Tobi
 
Reply With Quote
 
Mike Copeland
Guest
Posts: n/a
 
      06-04-2012
In article <XnsA067CE1A85C0myfirstnameosapriee@216.196.109.13 1>,
(E-Mail Removed) says...
> > char* TTB(const char *str) // Truncate Trailing Blanks
> > {
> > string wStr = str; // faults at this statement
> > wStr = trim_right(wStr);
> > return (char*)wStr.c_str();
> > } // TTB

>
> This function is invalid as noted by others, and also totally useless;
> delete it and replace all calls to it with calls to trim_right(). Update
> the calling code to receive a std::string instead of char*.
>
> Oh, and if indeed you get the access violation at the line you show, then
> most probably this is because you are passing in a NULL pointer. Don't do
> that, it would probably not work with trim_right() either (just guessing as
> trim_right() is not shown).
>

The root of my problem was not the routine itself, but it was the way
I was using it. I was invoking it this way:

strcpy(EDate, TTB(tpCopy(s2, 1, 20)));

where "s2" was a C-style string and myCopy wasn't returning a "const
char*" to be used by TTB.
I have "fixed" the problem so that it all works, but I'm concerned
that "myCopy" is potentially faukty. Specifically, I wonder about the
many "malloc" calls by repeated calls to it (memory resource and/or
fragmentation), as well as the risk of returning a pointer that could be
removed before the called "gets" the data.
The code seems to work now, but am I invoking undefined behavior or
something that could crash my programs in the future? If so, how do I
remedy this?

char* tpCopy(const char *str, int ii, int nn)// get substring (TP
version)
{
if(strlen(str) <= 0) return "";
char *temp;
if(NULL != (temp = (char*)malloc(strlen(str)+1)))
{
temp = _strdup(str);
temp[nn] = '\0';
return temp; // can't deallocate this memory!
}
else
{
return ""; // some error code required here...
}
} // tpCopy

BTW, I have hundreds of programs and many thousand lines of code.
Although I'd like to convert my C-style string code to basic_string, the
effort is substantial. My current task is to upgrade my VC6.0 coding to
use VC 2010 Express (as suggested by many here), and that's a daunting
effort by itself! Hopefully, I get to these other conversions later (if
I live long enough: I'm 72)... 8<}}
 
Reply With Quote
 
Balog Pal
Guest
Posts: n/a
 
      06-10-2012
"Mike Copeland" <(E-Mail Removed)>

> BTW, I have hundreds of programs and many thousand lines of code.
> Although I'd like to convert my C-style string code to basic_string, the
> effort is substantial. My current task is to upgrade my VC6.0 coding to
> use VC 2010 Express (as suggested by many here), and that's a daunting
> effort by itself! Hopefully, I get to these other conversions later (if
> I live long enough: I'm 72)... 8<}}


basic_string? Forget it.

CString is your friend in that case -- it has interface suitable for about
all tasks you have in the envirionment, and has implicit conversion toward
const char *, so you can start replacing the c-style stuff without uglifying
the user code.

(As a bonus you also dodge a ton of problems if decide to put some code into
DLL).

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      06-10-2012
On Sun, 2012-06-10, Balog Pal wrote:
> "Mike Copeland" <(E-Mail Removed)>
>
>> BTW, I have hundreds of programs and many thousand lines of code.
>> Although I'd like to convert my C-style string code to basic_string, the
>> effort is substantial. My current task is to upgrade my VC6.0 coding to
>> use VC 2010 Express (as suggested by many here), and that's a daunting
>> effort by itself! Hopefully, I get to these other conversions later (if
>> I live long enough: I'm 72)... 8<}}

>
> basic_string? Forget it.


"Forget it?" Why? It's the standard string type in C++.

> CString is your friend in that case -- it has interface suitable for about
> all tasks you have in the envirionment, and has implicit conversion toward
> const char *, so you can start replacing the c-style stuff without uglifying
> the user code.
>
> (As a bonus you also dodge a ton of problems if decide to put some code into
> DLL).


/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Dombo
Guest
Posts: n/a
 
      06-10-2012
Op 10-Jun-12 13:25, Balog Pal schreef:
> "Mike Copeland" <(E-Mail Removed)>
>
>> BTW, I have hundreds of programs and many thousand lines of code.
>> Although I'd like to convert my C-style string code to basic_string, the
>> effort is substantial. My current task is to upgrade my VC6.0 coding to
>> use VC 2010 Express (as suggested by many here), and that's a daunting
>> effort by itself! Hopefully, I get to these other conversions later (if
>> I live long enough: I'm 72)... 8<}}

>
> basic_string? Forget it.
>
> CString is your friend in that case -- it has interface suitable for
> about all tasks you have in the envirionment, and has implicit
> conversion toward const char *, so you can start replacing the c-style
> stuff without uglifying the user code.
>
> (As a bonus you also dodge a ton of problems if decide to put some code
> into DLL).


Mike is using the Express edition of VC 2010, which doesn't come with
MFC nor ATL libraries, hence out-of-the-box no CString with this edition.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with pushback function - "An Access Violation.. " hadad.yaniv@gmail.com C++ 6 07-26-2007 10:48 PM
Access Violation error while saving config from PIX through PDM toa TFTP server. AM Cisco 4 10-10-2006 07:59 PM
WebSphere JVM Access Violation Problem running Java program that calls methods in a Windows C++ DLL via a JNI wrapper....... adgarcia13 Java 2 06-28-2006 11:19 AM
Access violation - Malloc problem Rossel Nathanal C++ 3 11-08-2004 03:18 PM
access violation problem JasBascom C++ 13 02-20-2004 09:01 PM



Advertisments