By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
Knew it was nothing. strstr was run upon the constant token, not
lexxBuffer...
Jul 22 '05 #2

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.