Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   But..... WHY??? Access Violation (http://www.velocityreviews.com/forums/t284852-but-why-access-violation.html)

Morten Aune Lyrstad 08-06-2004 08:30 AM

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



Morten Aune Lyrstad 08-06-2004 08:32 AM

Re: But..... WHY??? Ok, shoot me.
 
Knew it was nothing. strstr was run upon the constant token, not
lexxBuffer...



Ron Natalie 08-06-2004 12:47 PM

Re: But..... WHY??? Access Violation
 

"Morten Aune Lyrstad" <wantno@spam.ever> wrote in message news:CkHQc.6802$Mq3.179425@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";
}

Morten Aune Lyrstad 08-06-2004 11:20 PM

Re: But..... WHY??? Access Violation
 
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.



Owen Jacobson 08-07-2004 03:34 AM

Re: But..... WHY??? Access Violation
 
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.


Old Wolf 08-08-2004 10:19 PM

Re: But..... WHY??? Access Violation
 
"Morten Aune Lyrstad" <wantno@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"


All times are GMT. The time now is 11:47 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.