Thread: Unable to get an out of bounds reference on my virtual machine.

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    1

    Unable to get an out of bounds reference on my virtual machine.

    I'm wondering if anyone has experienced the same problem or has any input on it. I ran the following program:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    int    main ()
    {
      long int a[2];
      double d = 3.14;
      printf("BEFORE: d = %.15g\n", d);
      a[2] = 1073741824; /* Out of bounds reference */
      printf("AFTER: d = %.15g\n", d);
      return(EXIT_SUCCESS);
    }
    and it outputs 3.14 twice, even though my professor demonstrated the array spilling into the variable in class. I tried to get some help from the VMware community but they weren't very helpful. Does anyone have any ideas?

    --
    Windows 7 64-bit
    VMWare Workstation 8
    Linux Mint 8 32-bit VM
    (Also tried it with CentOS 6.2 64bit VM)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The relative position of variables in memory do not necessarily correspond to the relative position of their declarations. (Which is a reason why accessing an array out of bounds results in undefined behaviour.)

    That said, you could try swapping the order of the two declarations to see if it makes any difference.
    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
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    If you're compiling with optimization on, there's a good chance that the references to d are being replaced by a load of a constant so there's nothing on the stack to overwrite in the first place.

    Plus you're never using a, so the compiler might be throwing away any changes to it.

    Try it again with optimization turned off. But yeah, you're hoping that undefined behavior works in the way you'd expect it to, and you see that it's not always the case.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    word of advice : when your program doesn't do what you think it should, its not the compiler or the platform. at least, not 99.999999% of the time.

  5. #5
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by KCfromNC View Post
    But yeah, you're hoping that undefined behavior works in the way you'd expect it to, and you see that it's not always the case.
    By definition, undefined behaviour won't work in any way you expect it to, at least not consistently, because it is undefined!

    There are several reasons why what you expect doesn't happen, up to and including that C compilers are written by different people with different ideas but only have to conform to the C specification to be called C compilers. And the C specifications say NOTHING about where things should be stored in memory at all. For all you know, a compiler could be storing things at 1Mb gaps apart and then going back and filling in the gaps, or not even putting things into memory AT ALL (instead using processor registers, etc.). Hell, it could be putting them into completely random parts of memory, so long as it keeps track of them somehow (and you NEVER get to know how it keeps track from inside the C code itself).

    You're trying to trigger a particular result of undefined behaviour that, presumably, your professor demonstrated. If you're not using exactly the same machine, the exact same OS, the exact same compiler, the exact same compilation step and the exact same source code, it won't happen. And even if you DO, there's no guarantee that what you witnessed will happen. Your professor almost certainly knows this, and has to keep that example setup working because it DOESN'T always trigger as nicely as you would hope.

    Hell, depending on what you're doing, on some OS's you could (in theory) crash the program like that by triggering DEP, segfaults or other similar protections and never see any result at all.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a basic Virtual Machine
    By manasij7479 in forum Tech Board
    Replies: 10
    Last Post: 07-04-2011, 08:39 PM
  2. Implementing a virtual machine
    By cboard_member in forum Tech Board
    Replies: 8
    Last Post: 07-17-2006, 06:15 AM
  3. Java Virtual Machine
    By geek@02 in forum Tech Board
    Replies: 1
    Last Post: 06-20-2004, 11:33 AM
  4. Creating a Virtual Machine
    By Chronom1 in forum C++ Programming
    Replies: 7
    Last Post: 10-05-2003, 11:11 AM
  5. Virtual Machine
    By confuted in forum Windows Programming
    Replies: 2
    Last Post: 04-08-2003, 02:29 PM

Tags for this Thread