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

  • Thread starter Morten Aune Lyrstad
  • Start date
M

Morten Aune Lyrstad

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;
}
 
M

Morten Aune Lyrstad

Knew it was nothing. strstr was run upon the constant token, not
lexxBuffer...
 
R

Ron Natalie

Morten Aune Lyrstad said:
_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";
}
 
M

Morten Aune Lyrstad

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.
 
O

Owen Jacobson

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...
 
O

Old Wolf

Morten Aune Lyrstad said:
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"
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top