473,606 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does anyone know why this happens?

Hi!

I have some problems in the following code.

#include <windows.h>
#include<iostre am.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?

Sep 11 '06 #1
6 2485
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.

bi**********@gm ail.com wrote:
Hi!

I have some problems in the following code.

#include <windows.h>
#include<iostre am.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?
--
Posted via a free Usenet account from http://www.teranews.com
Warning: Do not use Ultimate-Anonymity
They are worthless spammers that are running a scam.

Sep 11 '06 #2
bi**********@gm ail.com schrieb:
Hi!

I have some problems in the following code.

#include <windows.h>
#include<iostre am.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).

Sep 11 '06 #3
bi**********@gm ail.com wrote:
>Hi!

I have some problems in the following code.

#include <windows.h>
#include<iostr eam.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
Sl********@bls. gov
Sep 11 '06 #4

bi**********@gm ail.com wrote:
Hi!

I have some problems in the following code.

#include <windows.h>
#include<iostre am.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.

Sep 11 '06 #5
ralph wrote:
>
The string buffer that you pass to the ltoa function is too small (only
one character).
And to make matters worse, it's const.
Sep 12 '06 #6
Noah Roberts wrote:
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).
Sep 12 '06 #7

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

Similar topics

7
7814
by: lawrence | last post by:
Suppose I create dynamic web pages with 3 functions (which call other functions to make everything happen, but these 3 you might think of as being the top layer): registerSessions(); sendHtmlToBrowsers(); incrementPageViews(); Is there any chance that incrementPageViews() will be executed? Or, to
37
4633
by: Curt | last post by:
If this is the complete program (ie, the address of the const is never taken, only its value used) is it likely the compiler will allocate ram for constantA or constantB? Or simply substitute the values in (as would be required if I used the hideous, evil, much-abused #define :) ----------- const int constantA = 10; static const int constantB = 20;
1
2476
by: Steve | last post by:
I have a form with about 30 fields. Much of data entry for this form involves the same data for many of the fields. To save typing time, in the form's AfterUpdate event I run a procedure that sets the defaultvalue property of each field to the value of that field in the last saved record. Essentially what happens is when a record is saved it becomes the default new record in the form. I have some comboboxes on the form that draw their data...
2
3275
by: george r smith | last post by:
Gentlemen, I know that this problem has happened before and I searched and tried all solutions but problem is not solved. I install asp.net community starter kit with some help from this newsgroup.Install went fine, database was created on the SQL server machine but when I bring up browser and try to connect I get:
2
2088
by: bonk | last post by:
Hello, I am currently trying to wrap my head around what actually happens when I compile a normal (native) c++ class with the /CLR Flag in Visual C++ 2005 (C++/CLI). Suppose I have the following class deklared in c++: // #pragma managed or #pragma unmanaged // does not seem to make any differnce here
45
3381
by: salad | last post by:
I'm curious about your opinion on setting relationships. When I designed my first app in Access I'd go to Tools/Relationships and set the relationships. Over time I'd go into the window and see relationship spaghetti....tables/queries all overthe place with lots of relationship lines between here and there. After that first app I didn't do relationships. If I had a query, I defined the relationship. Many of the times when I create a...
55
6186
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
9
2736
by: Jonathan Wood | last post by:
Does anyone know of any reason a button on a master page would have no effect? I have a complex HTML page that I'm converting to ASP.NET, and acknowledge I could have something odd that is affecting this. But I'm stuck. My handler is in C# code and the code looks right. But absolutely nothing happens when I click the button. protected void Button1_Click(object sender, EventArgs e)
162
10185
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you prefix them with 2 underscores, but I hate prefixing my vars, I'd rather add a keyword before it. Python advertises himself as a full OOP language, but why does it miss one of the basic principles of OOP? Will it ever be added to python?
0
8009
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
7939
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
8432
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...
0
8428
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8299
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
6753
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...
1
5962
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2442
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1548
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.