Does anyone know why this happens?

B

bimal.eranda

Hi!

I have some problems in the following code.

#include <windows.h>
#include<iostream.h>

void main(){
LPSTR x="";
long y=125;
_ltoa(y, x, 10);
cout<<x<<endl;

}

Some times it work without any errors but some times it throws an
exception saying Access violation. it happens only in the release mode
compilation. when u assign the "" string to the x variable some times
the x get assigned to memory address 0x000000. then it'll throw that
exception.

Does any one know the reason to why this happens?
 
R

ralph

Hi!

I have some problems in the following code.

#include <windows.h>
#include<iostream.h>

void main(){
LPSTR x="";
long y=125;
_ltoa(y, x, 10);
cout<<x<<endl;

}

Some times it work without any errors but some times it throws an
exception saying Access violation. it happens only in the release mode
compilation. when u assign the "" string to the x variable some times
the x get assigned to memory address 0x000000. then it'll throw that
exception.

Does any one know the reason to why this happens?

Why didn't you check your documentation before asking here?

I don't know what LPSTR is but I googled for _ltoa and found a
Microsoft-specific function with that name. _ltoa's second parameter is
a pointer to a character array, so I suppose LPSTR is a macro that
expands to char*, right?

The Microsoft docs say that _lto converts a long integer to a string
and that the string buffer must be large enough to hold the converted
digits plus the trailing null-character and a sign character.

The string buffer that you pass to the ltoa function is too small (only
one character).
 
P

Pedro Sousa

Lets look at the definition of function ltoa

"char * ltoa ( long value, char * buffer, int radix );

Convert long integer value to string.
Converts a long integer value to a null-terminated string using the
specified radix and stores the result in the given buffer.
If radix is 10 and value is negative the string is preceded by the
minus sign (-). With any other radix, value is always considered unsigned.
buffer should be large enough to contain any possible value:
(sizeof(long)*8+1) for radix=2"

As I see your code I don't see you request the needed space! You just
alloc an string type variable with one position occupy with '\0'.

Make a better allocation for variable x.
 
T

Tim Slattery

Hi!

I have some problems in the following code.

#include <windows.h>
#include<iostream.h>

void main(){
LPSTR x="";
long y=125;
_ltoa(y, x, 10);
cout<<x<<endl;

}

x is a pointer that points to a string which has only one byte: a
zero terminator. You then convert a number to a string. The result of
that transformation will be a string at least three bytes long,
needing a fourth for its terminator. You tell the conversion function
to store that at x. But you are sure of only one byte at x.

So of course you get an access violation. You're writing data to space
that you have not allocated.

--
Tim Slattery
(e-mail address removed)
 
N

Noah Roberts

Hi!

I have some problems in the following code.

#include <windows.h>
#include<iostream.h>

void main(){
LPSTR x="";
long y=125;
_ltoa(y, x, 10);
cout<<x<<endl;

}

LPSTR is a synonym for char*, don't ask me why they decided to create
typedefs for standard types. What your x is is actually a char const*
but the compiler doesn't warn you about this kind of const assignment
(imho this is a weakness of the language, it should complain). _ltoa
is a "standard compliant" version of the posix ltoa...that's MS wordage
there, it's BS.

So anyway, what you are trying to do is modify data stored in constant
memory through a non-constant point. This results in undefined
behavior, this type of undefined behavior often shows itself as a crash
or access violation.

You should use the standard types and function names instead of the MS
verbage. You can turn off the influx of warnings you'll get by using
ltoa with a macro definition and the other type is just a typedef so it
won't even turn up. The standard types are more descriptive and won't
get you heckled when you post in a c++ group or forum. So learn what
the typedefs are for DWORD, LONG, LPSTR, LPCSTR, etc, and use the
original type instead of the MS specific alias. This has the added
benefit of making your code more portable, which MS is trying to avoid
by tricking you into using them.
 
R

Ron Natalie

Noah said:
LPSTR is a synonym for char*, don't ask me why they decided to create
typedefs for standard types.

LPSTR is apparently Long Pointer To Str. Well there aren't any such
things (and never have been in REAL C and C++) as long pointers. And
it's not a pointer to a string, it's a pointer to a character. This
is one of the inane features of Microsnot coding (and a bastardization
of what Synomi was trying to accomplish with his notation).
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top