Thread: wierd problem

  1. #1
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158

    wierd problem

    this line executes fine
    Code:
    g = p[x];
    but this line crashes the program
    Code:
    g = p[x] >> 4;
    any idea why this could be?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is g, p and x?
    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
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Yarin View Post
    any idea why this could be?
    You broke something.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If p[x] isn't a native type, perhaps your implementation of operator >> is broken.

    Under normal circumstances, a right shift shouldn't crash . . . .

    By "crash" do you mean a segmentation fault or something?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    by crash I mean "this program has generated errors and must be closed by windows".
    Code:
    int x = 2;
    BYTE p[1000];
    BYTE b;

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Is g an object of some type that you have overloaded the assignment operator?

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    I don't think I know what you mean, both g and p[] are BYTEs.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Do you have a minimal example program that exhibits the problem?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Definitely not that lines fault. The difference might be that the code generation is different, and when you use the shift operator, the code is using a different piece of code that EXPOSES an already existing undefined behaviour problem [such as overwriting the stack for example].

    It is not entirely unusual that small code changes hides/exposes a problem elsewhere - but the problem isn't in the code that changed.

    --
    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.

  10. #10
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Okay, do you know of any way to safeguard against these undefined behaviour problems?

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Yarin View Post
    this line executes fine
    Code:
    g = p[x];
    but this line crashes the program
    Code:
    g = p[x] >> 4;
    any idea why this could be?
    Are you sure there is not some other simultaneous change which is causing x to have a different value?

    What matsp described could be true, but IMHO it could only happen if you were doing incredibly weird things with memory. Is that the case?

  12. #12
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Here is the code: http://members.ozemail.com.au/~dekker/NEUQUANT.C
    The line "b = p[0] << netbiasshift;" is causing the crash. netbiasshift is defined as 4.

    It seems that the code has already been deemed ready-to-use, so I am inclined to think that matsp is right. But how would I find where the problem code really is?

    Thanks for the help so far.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Yarin View Post
    It seems that the code has already been deemed ready-to-use, so I am inclined to think that matsp is right. But how would I find where the problem code really is?
    You would want to identify the specific memory access which led to the fault, and then trace that to a specific function, and from there, to a specific piece of data. Again, matsp's theory could be correct, but only if very strange things are occurring in memory. Otherwise, I honestly am baffled. Please try to reproduce the problem in as small a fragment of code as possible.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not fixing the real problem, whatever that may be, but does this work?
    Code:
    g = p[x];
    g >>= 4;
    Also -- what kind of optimisations are you compiling with? That code looks highly mathematical, and sometimes, compilers can produce bad code with all manner of strange optimisations enabled . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Now this makes me feel kind of dumb, the problem is that p was being incrimented as well as x, causing a read-error. The reason it worked without the bitshift, but not with it, still confuses me though. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM