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

Discussion in 'C++' started by Morten Aune Lyrstad, Aug 6, 2004.

  1. 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, Aug 6, 2004
    #1
    1. Advertising

  2. Re: But..... WHY??? Ok, shoot me.

    Knew it was nothing. strstr was run upon the constant token, not
    lexxBuffer...
     
    Morten Aune Lyrstad, Aug 6, 2004
    #2
    1. Advertising

  3. Morten Aune Lyrstad

    Ron Natalie Guest

    "Morten Aune Lyrstad" <> wrote in message news:CkHQc.6802$...

    >
    > _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";
    }
     
    Ron Natalie, Aug 6, 2004
    #3
  4. 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.
     
    Morten Aune Lyrstad, Aug 7, 2004
    #4
  5. 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.
     
    Owen Jacobson, Aug 7, 2004
    #5
  6. Morten Aune Lyrstad

    Old Wolf Guest

    "Morten Aune Lyrstad" <> 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"
     
    Old Wolf, Aug 8, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news
    Replies:
    0
    Views:
    433
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    905
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,974
    Smokey Grindel
    Dec 2, 2006
  4. aling
    Replies:
    12
    Views:
    560
    Greg Comeau
    Nov 15, 2005
  5. ThunderMusic
    Replies:
    1
    Views:
    154
    ThunderMusic
    Feb 22, 2007
Loading...

Share This Page