Menu

Run time prob with variable/structure bleed.

Gairnok
2007-09-06
2012-09-26
  • Gairnok

    Gairnok - 2007-09-06

    (1) 4.9.9.2

    (2) It's not a problem with the code itself per say. It is more of data that should be in one string (char array) bleeding over to another.
    For example, I have a structure that contains several character arrays sometimes data that I copy into string x also ends up in string y.

    char lname[100][25];
    char sname[100][25];

    Now sometimes for example I may copy something into lname, and it might also end up in sname. Or even odder at the very end of sname.

    However if I do something as simple as move the two arrays away from each other, and declare them in between say two bools, the error goes away.

    Another example is.
    char name[25];
    char desc[100];

    Both of them being members of struct 'command cmd[200]'

    When cmd[i].desc is actually maxed out at say 100 chars, the name store in cmd[i].name ends up on the end of cmd[i].desc

    Unfortunately I don't have any bools in the structure to place between the two.

    Sorry I dont have (3). I don't know that it would do much good as no compile errors show up.

    The problem is easily repeatable, all I have to do is move the variables back close together where I have them declared (variables.h) Which has pretty much all my structures and globals.

    Anyone else had this problem?

    The other thing I have had a problem with is some local variables retaining thier value between calls. Using a char[x]=""; to null them or using int X=0; Has been my fix, though it does seem odd.

    Best,
    Gairnok

     
    • Osito

      Osito - 2007-09-06

      Can you post the code that does the copying? I've had this sort of thing happen before and it was a bug in my copying code.

       
    • Anonymous

      Anonymous - 2007-09-06

      >> It's not a problem with the code itself per say.
      No, it is exactly a problem with the code. Unfortunately you did not post the code! Posting teh declarations is not helpful, that is not where the problem lies. And it is "per se" BTW.

      >>Unfortunately I don't have any bools in the structure to place between the two.
      Well apart frm the fact that you could use redundant dummy data, that is hardly a solution. What you are doing by this is merely hiding a problem that you don't understand; not fixing it!

      Your problem is no doubt a buffer overrun. A string of length 100 requires an array of 101 characters (a C style string requires a NUL terminator in addition to printable characters). If you write a string of 100 characters to a 100 element array, the NUL will be written to the first byte of whatever data is adjacent (which is why inserting dummy data appears to fix the problem). When the adjacent data is written, the NUL is overwritten so teh string is no longer correctly terminated. If you then copy the unterminated string, it will be of indeterminate length and probably overrun whatver you are copying to, making the situation even worse.

      The way to get a more specific answer is to reproduce the effect with as small an example as possible and post that example. That seems rather obvious, I can't think how you expect an answer without posting the erroneous code! Your problem it seems is that you believe that there is nothing wrong with your code, but you are surely incorrect in that belief - if that were true it would work!

      You might avoid all these problems by using C++ std::string objects instead of C strings. But other way you need to better understand the nature of C strings since you may not always be able to avoid them.

      Clifford

       
    • Gairnok

      Gairnok - 2007-09-06

      strcpy(cmd[i].desc,argument);

      cmd[i].name is never addressed in the part copying in the cmd description. The bleed over even got so bad, (on strings hitting 100 chars in length (the max of desc).) That I had to store the cmd[i].name into a temp string, and then copy it back over into it again as it was getting erased..

      Again when this problem happened in vars from my other structures and simply moving it position it my declarations solved it..

      Maybe my compiler just hates bald guys.

      Best,
      Gair

       
      • Osito

        Osito - 2007-09-06

        That's not quite enough code to be useful for helping you debug. You never mentioned 'argument' in your first post, and I can't tell how you've declared all the different variables.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.