Strangeness

This is a discussion on Strangeness within the C Programming forums, part of the General Programming Boards category; Hello guys, I have the following buffer: Code: char buf[63]; And sometimes the data is perfectly copied into the buffer ...

  1. #1
    pfs
    pfs is offline
    Registered User
    Join Date
    Sep 2008
    Posts
    14

    Red face Strangeness

    Hello guys,

    I have the following buffer:

    Code:
    char buf[63];
    And sometimes the data is perfectly copied into the buffer and others it gets perfectly copied at some point and them it shows up garbage characters (memory?).

    Example:

    'ThisIsANiceBufferContent'

    'ThisIsANiceB^AOq^D^B'

    Any ideas on what could be causing this?

    Thanks in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, who knows what you're doing? (Hint: not us.) Provided what you're doing is null terminated, things should get copied more-or-less properly. However, things won't get copied past the null terminator.

  3. #3
    pfs
    pfs is offline
    Registered User
    Join Date
    Sep 2008
    Posts
    14

    Red face

    Yes, I'm sorry, my bad.

    So, I have a home-made strncpy() and sprintf().

    Could these be the source of the problem?

    The strange thing about this is that it triggers this behavior randomly and during the operation of the application.

    Like, for example:

    - Application receives the order to copy the buffer (and does it nicely);
    - After awhile when I check up the buffer, it's mangled;

    Still, any ideas?

    Any other references that you would like me to make, just tell me.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    So, I have a home-made strncpy() and sprintf().

    Could these be the source of the problem?
    Of course they could be the source of the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    pfs
    pfs is offline
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    Even if the behavior is random?

    Why does it assume that behavior?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by pfs View Post
    - After awhile when I check up the buffer, it's mangled;
    Like it got overwritten somehow? Why don't you try to come up with a concise version of the code, reproducing the problem, and post it? This is probably something simple.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Post your home made functions, then we'll probably be able to tell you right away what the problem is.

    Otherwise, it's just pointless guessing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    pfs
    pfs is offline
    Registered User
    Join Date
    Sep 2008
    Posts
    14

    Red face

    The functions aren't mine and can be found here (line 58) and
    here (the whole file).

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Code:
    char* strncpy_irc(char* s1, const char* s2, size_t n)
    {
      char* endp = s1 + n;
      char* s = s1;
      while (s < endp && (*s++ = *s2++))
        ;
      return s1;
    }
    Yeah, unless you're NULL-terminating the return value yourself, that's a problem there.
    Last edited by rags_to_riches; 09-16-2008 at 12:14 PM.

  10. #10
    pfs
    pfs is offline
    Registered User
    Join Date
    Sep 2008
    Posts
    14

    Question

    Quote Originally Posted by rags_to_riches View Post
    Code:
    char* strncpy_irc(char* s1, const char* s2, size_t n)
    {
      char* endp = s1 + n;
      char* s = s1;
      while (s < endp && (*s++ = *s2++))
        ;
      return s1;
    }
    Yeah, unless you're NULL-terminating the return value yourself, that's a problem there.
    So, I guess, doing

    Code:
    char* strncpy_irc(char* s1, const char* s2, size_t n)
    {
      char* endp = s1 + n;
      char* s = s1;
      while (s < endp && (*s++ = *s2++))
        ;
      *--s = '\0';
      return s1;
    }
    would fix the problem.

  11. #11
    pfs
    pfs is offline
    Registered User
    Join Date
    Sep 2008
    Posts
    14

    Cool

    I was able to track down the nasty bug and do a nice squishing sound.

    This problem wasn't related to strncpy_irc() or ircsprintf(), its seems that memory leaks can be pretty much random. I wasn't really expecting this behaviour, but this was a nice lesson and I really loved learning from it.

    Thank you very much for all of your input on this. I'm much appreciated to you guys and I'm sorry for the initial lack of information (sudden numbness of some sort).

  12. #12
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by pfs View Post
    its seems that memory leaks can be pretty much random.
    Er, no. Memory doesn't just randomly leak...
    If you no longer have a pointer to a piece of allocated memory, you have a memory leak. If you later find out that this allocated buffer is garbage, you must still have had a pointer to it somewhere, so it's not leaked.
    If your buffer contains garbage, that could be the result of some undefined behavior operation, which, yeah, could be random.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  13. #13
    pfs
    pfs is offline
    Registered User
    Join Date
    Sep 2008
    Posts
    14

    Talking Problem solved.

    What I meant was that their behaviour can be random.

    I was able to track down the bugger and squished it right.

    The code that triggered this behaviour was something like this (example):

    Code:
     struct the_whale
     {
        char buf[63];
     };
     (...)
     free(willy);
     free(willy->the_whale);
    The strange thing about this is that the process didn't crash. Shouldn't this be reason enough for the process to crash?

    Anyways, this problem is solved and gone.

    Once again, thanks to all of you who helped me with this.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pfs View Post
    What I meant was that their behaviour can be random.

    I was able to track down the bugger and squished it right.

    The code that triggered this behaviour was something like this (example):

    Code:
     struct the_whale
     {
        char buf[63];
     };
     (...)
     free(willy);
     free(willy->the_whale);
    The strange thing about this is that the process didn't crash. Shouldn't this be reason enough for the process to crash?
    It MAY crash if you are unlucky enough (or lucky enough, depending on how you look at it). The behaviour of expression willy->the_whale after free(willy) is undefined, so "anything" can happen. It depends on what free does with the memory when you give it up - and there is no definition for free in this respect - it only defines that free makes the memory available to use after the call. In a multithreaded environment, with the right amount of (un)luck, you'd have some other process allocate the memory immediately after free(willy), and allocate another pointer in the same location as the_wale, and you free the memory that was just allocate by the other thread - this thread then uses memory that has been freed, and some time later when the memory gets used for a different purpose, the second thread crashes in some weird way. Figuring out what went wrong in that case can take quite some time...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This is more or less the main reason behind destructors in C++. It will theoretically fix these problems though I should note the use of the word theoretically since the problem is not in the nature of dynamic memory allocation but rather in the carelessness of the person writing the code.

    On a side note, and refering back to the initial problem: sometimes it helps to do an fwrite() to a file or pipe stdout to a file or something in order to visually see what is actually going on. Experience will typically help you recognize when a null character is not properly being inserted into a string. But sometimes its not quite that simple. At least if you can view the binary version of whatever was printed you can see that your whole string was probably there and alive and well... But because of other characters such as \b, \a, \r, \n etc. also being present, you got all sorts of other crap that ended up looking like part of the string even though it was in all probability stuff that just got backspaced into the position it printed to. Or carriage returned too... Out of curiosity, did it print a \a? (The one character that you would know if it printed without even looking at the screen).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Form.ShowDialog() strangeness
    By Elkvis in forum C# Programming
    Replies: 5
    Last Post: 01-18-2009, 01:00 PM
  2. Modem Driver Strangeness.
    By civix in forum Tech Board
    Replies: 1
    Last Post: 03-26-2006, 07:02 PM
  3. integer strangeness
    By lostminds in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2002, 01:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21