string routines go to never never land on unix

Discussion in 'C Programming' started by Kevin, Oct 17, 2003.

  1. Kevin

    Kevin Guest

    I have a routine that is copying chunks of a super large string into a
    new order. It is acting as a message translator. For certain source
    messages, the string routines seem to stop, and the cpu utilization
    goes to 99%. When I attach to the process, the program is always on
    either strchr or strlen.

    Here is a snippet of code demonstrating these symptoms:

    /* Look for space in input to break line upon*/
    pcSrcCharLoc = pcAdjustedInput;
    while (pcSrcCharLoc < pcAdjustedInput + lWidthData)
    {
    pcSpacePointer = strchr(pcSrcCharLoc, ' ');
    tLocOfSpace = pcSpacePointer - pcSrcCharLoc;

    ... processing of string between pcSrcCharLoc and pcSpacePointer
    - a sprintf of pcSrcCharLoc onto pcDestCharLoc for length tLocOfSpace
    plus some logging wrapped in if's.

    pcSrcCharLoc += tLocOfSpace;
    }

    pcAdjustedInput is a pointer to a string of the field being copied,
    which has had extra spaces removed. The pointer was alloc'd by the
    routine that copied the string removing the spaces. The string is
    null terminated. LWidthData is the strlen of pcAdjustedInput.

    What will happen is on the first run through, the first substring (a
    word) is processed, and then the program stops on the next strchr, and
    the CPU utilization goes to 99%.

    Any hints, ideas, or someone pointing out the obvious would be greatly
    appreciated.

    Thanks
    Kevin
     
    Kevin, Oct 17, 2003
    #1
    1. Advertising

  2. Kevin <> spoke thus:

    > Here is a snippet of code demonstrating these symptoms:


    > /* Look for space in input to break line upon*/
    > pcSrcCharLoc = pcAdjustedInput;
    > while (pcSrcCharLoc < pcAdjustedInput + lWidthData)
    > {
    > pcSpacePointer = strchr(pcSrcCharLoc, ' ');
    > tLocOfSpace = pcSpacePointer - pcSrcCharLoc;


    > pcSrcCharLoc += tLocOfSpace;
    > }


    If I'm not mistaken, you're stuck on the first space in the input.
    pcSrcCharLoc+tLocOfSpace puts pcSrcCharLoc right at the space, and so
    strchr(pcSrcCharLoc, '') is pcSrcCharLoc.

    pcSrcCharLoc += tLocOfSpace+1;

    would fix things, I imagine...

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 17, 2003
    #2
    1. Advertising

  3. Christopher Benson-Manica <> wrote:

    >Kevin <> spoke thus:
    >
    >> pcSrcCharLoc = pcAdjustedInput;
    >> while (pcSrcCharLoc < pcAdjustedInput + lWidthData)
    >> {
    >> pcSpacePointer = strchr(pcSrcCharLoc, ' ');
    >> tLocOfSpace = pcSpacePointer - pcSrcCharLoc;

    >
    >> pcSrcCharLoc += tLocOfSpace;
    >> }

    >
    >If I'm not mistaken, you're stuck on the first space in the input.
    >pcSrcCharLoc+tLocOfSpace puts pcSrcCharLoc right at the space, and so
    >strchr(pcSrcCharLoc, '') is pcSrcCharLoc.

    ^' ' (just to pick nit ;-)
    >
    >pcSrcCharLoc += tLocOfSpace+1;
    >
    >would fix things, I imagine...


    most probably, yes; alternatively you can write:

    pcSrcCharLoc = pcSpacePointer + 1;

    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 17, 2003
    #3
  4. Irrwahn Grausewitz <> spoke thus:


    >>strchr(pcSrcCharLoc, '') is pcSrcCharLoc.

    > ^' ' (just to pick nit ;-)


    Well, if I've graduated from "Sir, you are completely wrong, please shut up
    now" to "Sir, you forgot a space, please shut up now", I'm improving...
    Hopefully it isn't temporary ;-(

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 17, 2003
    #4
  5. Christopher Benson-Manica <> wrote:

    >Well, if I've graduated from "Sir, you are completely wrong, please shut up
    >now" to "Sir, you forgot a space, please shut up now", I'm improving...
    >Hopefully it isn't temporary ;-(


    Relax, same to me. 8^}
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 17, 2003
    #5
    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. Doug Holland

    Adventures in Whidbey land...

    Doug Holland, Jan 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    380
    Yan-Hong Huang[MSFT]
    Jan 6, 2004
  2. praveen
    Replies:
    3
    Views:
    10,357
    praveen
    Sep 15, 2004
  3. Lisa Horton
    Replies:
    4
    Views:
    394
    Aquila Deus
    Feb 4, 2005
  4. Nik Coughin

    Something strange in IE land...

    Nik Coughin, May 20, 2005, in forum: HTML
    Replies:
    3
    Views:
    440
    Blade
    May 20, 2005
  5. GenghisOne

    Confusion in string.split land

    GenghisOne, Jul 12, 2009, in forum: Javascript
    Replies:
    4
    Views:
    104
    Evertjan.
    Jul 12, 2009
Loading...

Share This Page