Thread: Adress assigning at the beginning of a program.

  1. #1
    Registered User
    Join Date
    Jul 2007

    Adress assigning at the beginning of a program.

    You know when we define an integer it takes an uninitalized random number. The thing is , this number is always the same when I execute the program. Shouldnt it take different varies each time because it is random ?

  2. #2
    Registered User
    Join Date
    Sep 2006
    The value isn't (necessarily) random, just undefined.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Random doesn't mean that you get a different value every single time.

    If you change the program and recompile, it might be different.
    If you change the compiler options, it might be different.
    If you change the compiler, it might be different.
    If you change the OS, it might be different.

    Random means that you have no way to predict what the value(s) are going to be given the circumstances under which it was compiled and run.

    Sure, it may seem like a constant for any given simple program, but you sure can't extrapolate that observation to every program in every circumstance.
    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
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Ehm, you mean a local variable inside main (or some other function).

    I guess I (and others) are sloppily calling these numbers "random garbage", but it's not random in that sense.

    Yes, it will often have the same value each time you run the code. It is only random from a more big-picture perspective - it is UNDEFINED, which means that it can have ANY value. It is not random in the way that it's going to be decidedly different values every time, but if you compile the same program with a different compiler, it's highly likely that it has a different value - even changing the compiler parameters (such as enabling/disabling optimization) or making changes to the code itself could make it change.

    If you want real random numbers, then you should use a random number generator.

    The point about undefined variable values (and other undefined behaviour) is that it is NOT DEFINED - it can be anything, it's not guaranteed to stay the same, and it may cause problems later on if you expect it to stay this way.

    Note also that some compilers have settings where you can "initialize all variables to a defined value", which for example will make all uninitialized local variables have a defined value - this is to help you find the problem of uninitialized variables - sometimes the compielr will even detect these things and tell you explicitly: You are using X when it's uninitialized. In this case, the value is very much DEFINED. However, this also makes the code slower, because each function will have a bunch of code to set all the local variables to some fixed value.

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM