Thread: strange debugger results on atomic 16 bit variable

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    75

    strange debugger results on atomic 16 bit variable

    I have unsigned int variable that throughout code is either assigned a 0 or a 1 ....that's it....I am noticing the debugger when stopped sometimes (due to code going awry ---- verified with logic analyzer) that the variable is showing large int values like 15191, etc....I am now wondering if I am running into buffer overflow issues. I am using memcpy multiple times throughout the code and I found that ppl are saying buffer overflow can be a very real issue with memcpy. Can someone tell me if the following should be ok to use without buffer overflow issues?

    Code:
    memcpy(radioID, (const void *)&RXData[9], sizeof(radioID));
    Here radioID is 4 deep
    RXData is 32 deep BUT I am specifically using the sizeof on the smaller array

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,304
    When you say "RXData is 32 deep", you mean that it has a size of 32 bytes? How many bytes is the size of RXData[0]?

    But yeah, what you have shown should not result in overwriting memory unexpectedly; but that doesn't mean you are reading from memory correctly (although that's unlikely to result in the behaviour you observed for the unsigned int).

    Note that you don't need the cast to const void* since the implicit type conversion from pointer to object type to const void* is always safe.
    Last edited by laserlight; 12-27-2020 at 05:53 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,617
    Your memcpy is wrong to start with (is this really your code, or just something you paraphrased from memory?)

    > memcpy(radioID, (const void *)&RXData[9], sizeof(radioID));
    You're missing an & on the first parameter.
    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.

  4. #4
    Registered User
    Join Date
    Mar 2020
    Posts
    75
    Code:
    char radioID[4];
    The code I have above seems to work.....why do I need to place &radio.....I'm guessing bcz memcpy is a block type copy vs copy one after the other until sizeof is reached?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,617
    Well you said atomic, and arrays can never be atomic.

    Sure, it makes your memcpy valid, but it isn't solving your problem.

    > I have unsigned int variable that throughout code is either assigned a 0 or a 1
    So this variable is actually nowhere in your post then?

    Your 'guess' at one line of code which isn't even to do with the thing you're observing just isn't going to get it done.

    Post a whole program, or at least some cut-down version which demonstrates the problem.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-17-2016, 12:08 PM
  2. atomic variable in C
    By ilans11il in forum C Programming
    Replies: 2
    Last Post: 04-17-2013, 02:51 PM
  3. Strange arithmetic results
    By gestva in forum C Programming
    Replies: 16
    Last Post: 10-24-2011, 07:59 AM
  4. Strange test results..
    By kwikness in forum C Programming
    Replies: 6
    Last Post: 12-08-2007, 05:47 PM
  5. Strange Results from cout
    By IdioticCreation in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2007, 05:09 AM

Tags for this Thread