473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

But..... WHY??? Access Violation

Hey there! Something Wicked has turned up here with me... Guaranteed to be
"nothing", and I'm gonna be embarrassed when I find out what is wrong, but I
think I have stared myself blind on my own code.

Why the @**@ does this stop with an Access Violation on the marked line???

and:

_Is_ there a point of using "static" on lexxBuffer (wta performance), or
should I remove it?

#define BUFFERLEN 256

BOOL IsFLOAT(LPCSTR token)
{
static CHAR lexxBuffer[BUFFERLEN];
INT strLen = strlen(token);
if (strLen == 0)
return FALSE;

if (strLen >= BUFFERLEN)
return FALSE;
strcpy(lexxBuffer, token);
LPSTR strTok[2];
strTok[0] = lexxBuffer;
strTok[1] = strchr(token, '.');
if (strTok[1] == NULL)
return FALSE;
if (strTok[1][1] == NULL)
return FALSE;
strTok[1][0] = 0; // <----------------------HERE
strTok[1]++;

/*
if (!IsINT(strTok[0]))
return FALSE;

if (!IsUINT(strTok[1]))
return FALSE;
*/

return TRUE;
}
Jul 22 '05 #1
5 1674
Knew it was nothing. strstr was run upon the constant token, not
lexxBuffer...
Jul 22 '05 #2

"Morten Aune Lyrstad" <wa****@spam.ever> wrote in message news:Ck*******************@news4.e.nsc.no...

_Is_ there a point of using "static" on lexxBuffer (wta performance), or
should I remove it?


The reason it copies it, apparently, is that the incoming argument is pointer
to const char. Since the function later on pokes nulls into the string it's
manipulating, this won't work.

Your function is horrid anyhow. LPSTR, LPCSTR, and BOOL are nonstandard
types. LPCSTR and LPSTR are stupid names for types anyhow. std::string
would make things a lot cleaner and simpler. The test is probably problematic
anyhow. If you're willing to take the language definition of what a float/int/unsigned
int represented as a string is, then the following would be better:

template <class T> bool IsInput(const std::string& s) {
std::istringstream iss(s);
T test;
return iss >> test;
}

int main()
{
std::cout << "Is 3.4 float? " << IsInput<float>(3.4) << "\n";
}
Jul 22 '05 #3
There is a reason this code looks the way it is, is because it is generated
automatically by a code generator program I am creating. It creates
type-tester functions based upon strings passed to it. For example, if you
would pass the string "0x" [0-9a-zA-Z]*, you would get a tester for a
typical c++-type string (0xABC123). I know why it copies the string, I made
it that way. I was wondering if there really was a point of using static on
the buffer. Accurate testing is more important than speed. Besides, my
program must be able to create testers for virtually any format the user
would want.

The function is tested, and so far seems to work.

I am quite aware that my code looks horrid and nonstandard, I am one-hundred
percent self-taught, and 10 years as an amateur programmer with little
"outside" input tends to do something to your code style as compared to
others. ;-)

Oh, and just in case you didn't know, LPSTR stands for Long-Pointer To
String, and LPCSTR stands for Long Pointer to Constant String. LPSTR, LPCSTR
and BOOL are defined in the windows api (windows.h). I simply use them
because they are there.
Jul 22 '05 #4
On Sat, 07 Aug 2004 01:20:58 +0200, Morten Aune Lyrstad wrote:
Oh, and just in case you didn't know, LPSTR stands for Long-Pointer To
String, and LPCSTR stands for Long Pointer to Constant String. LPSTR,
LPCSTR and BOOL are defined in the windows api (windows.h). I simply use
them because they are there.


He probably knew. The point is, those types are only defined for one
platform and, as far as I can tell, add little value to the names "char
*", "const char *", and "bool". You might even consider using std::string
and const std::string...

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

Jul 22 '05 #5
"Morten Aune Lyrstad" <wa****@spam.ever> wrote:

Why the @**@ does this stop with an Access Violation on the marked line???

#define BUFFERLEN 256

BOOL IsFLOAT(LPCSTR token)
{
static CHAR lexxBuffer[BUFFERLEN];
INT strLen = strlen(token);
if (strLen == 0)
return FALSE;

if (strLen >= BUFFERLEN)
return FALSE;
strcpy(lexxBuffer, token);
LPSTR strTok[2];
strTok[0] = lexxBuffer;
strTok[1] = strchr(token, '.');
if (strTok[1] == NULL)
return FALSE;
if (strTok[1][1] == NULL)
return FALSE;

strTok[1][0] = 0; // <----------------------HERE
strTok[1] points into 'token'. If it crashes then token is probably
a read-only object, eg. you call IsFLOAT("1.1"). Did you mean for
strTok[1] to point into lexxBuffer instead?
strTok[1]++;

/*
if (!IsINT(strTok[0]))
return FALSE;

if (!IsUINT(strTok[1]))
return FALSE;
*/

return TRUE;
} and:

_Is_ there a point of using "static" on lexxBuffer (wta performance), or
should I remove it?


Remove it, unless you intend to return the value.

BTW you should stop using those typedefs, it just makes the code harder
to read. And if you don't use them then your code will also work
in programs that don't include "windows.h"
Jul 22 '05 #6

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

Similar topics

15
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...
0
by: Steven Reddie | last post by:
In article <slrnbnj19j.av.juergen@monocerus.manannan.org>, Juergen Heinzl wrote: >In article <f93791bd.0309282133.650da850@posting.google.com>, Steven Reddie wrote: >> I understand that access...
5
by: Alex | last post by:
Hello Im working on project for my college, nevermind (but it urgent :((()... So I have this code example (in VS 6.0) in main class : REAL *Input; ....
7
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...
0
by: Microsoft News | last post by:
I'm getting the following error when I shut down my C# .NET v1.1 application: 0xC0000005: Access violation reading location 0x73bc0000 This error didn't occur until I added a...
1
by: Thomas Albrecht | last post by:
My application fails during initialization of the dlls with an ExecutionEngineException and a access violation in the MFC app. The structure of the program looks like: MFC app -> mixed DLL ->...
1
by: BillyO | last post by:
In the attached code fragment I have a buffer overflow and a memory access violation. When I run the code .Net fails to verify the IL because of the buffer overflow and I get an exception as...
2
by: Boris Fortes | last post by:
I need to unhook event receiver as result of native C++ event. It unhooks successfully, but __raise does not return and throws access violation. Visual Studio 2003 How to reproduce: Consol...
2
by: =?Utf-8?B?c29jYXRvYQ==?= | last post by:
Hi, I have a DLL in VC6, when a specific function is called it will spawns a few threads and then return. The threads stay running and inside one of these threads an event is created using the...
39
by: Martin | last post by:
I have an intranet-only site running in Windows XPPro, IIS 5.1, PHP 5.2.5. I have not used or changed this site for several months - the last time I worked with it, all was well. When I tried it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.