Does your computer set aside storage for the variables in the order in which they wer

This is a discussion on Does your computer set aside storage for the variables in the order in which they wer within the C++ Programming forums, part of the General Programming Boards category; Does your computer set aside storage for the variables in the order in which they were declared? Code: #include <iostream.h> ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    155

    Does your computer set aside storage for the variables in the order in which they wer

    Does your computer set aside storage for the variables in the order in which they were declared?

    Code:
    #include <iostream.h>
    using namespace std;
    
    int main(){
    	short int shorty;     
    	int num, count;
    	long date;
    	float yield;
    	double price;
    	
    	cout << "Address of shorty: " << &shorty << endl << "\tSize in bytes: " << sizeof(shorty) << endl;
    	cout << "Address of num: " << &num << endl << "\tSize in bytes: " << sizeof(num) << endl;
    	cout << "Address of count: " << &count << endl << "\tSize in bytes: " << sizeof(count) << endl;
    	cout << "Address of date: " << &date << endl << "\tSize in bytes: " << sizeof(date) << endl;
    	cout << "Address of yield: " << &yield << endl << "\tSize in bytes: " << sizeof(yield) << endl;
    	cout << "Address of price: " << &price << endl << "\tSize in bytes: " << sizeof(price) << endl;
    	
        return 0;
    }
    
    
    /*
    
    	    *OUTPUT OF PROGRAM*
    	
    	Address of shorty: 0012FF8A
    	        Size in bytes: 2
    	Address of num: 0012FF84
    	        Size in bytes: 4
    	Address of count: 0012FF80
    	        Size in bytes: 4
    	Address of date: 0012FF7C
    	        Size in bytes: 4
    	Address of yield: 0012FF78
    	        Size in bytes: 4
    	Address of price: 0012FF70
    	        Size in bytes: 8
    		
    */
    So what does this mean?
    Last edited by Nakeerb; 10-11-2002 at 07:13 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Does your computer set aside storage for the variables in the order in which they were declared?
    Maybe, but you can't be sure. I wouldn't rely on it for anything in your programs if I were you.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    your operating system is likely looking for any free block of memory to store your data. unless it's an array, there's no requirement imposed on the system to allocate data sequentially.

    on the other hand, given the amound of memory in a system, and the amount of memory you request, it's very likely variables will turn up sequentially. but, as prelude said, don't trust that it will.

  4. #4
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    your system will most likely be reserving memory for your variables from a big stack. what really counts is how your compiler works - it could decided that all variables must come from a huge blob of memory at some random location. but most likely it will just reserve x bytes by pushing down the stack pointer... and while the compiler could reorder how the variables are declared, again it might not.

    edit:
    to actually answer your question, your variables are being allocated in sequential order for the output you give, and it does look like they're being placed on a stack. for some odd reason (alignment?!) the first (supposedly 2 byte) int looks like it takes 6 bytes. maybe i just don't know what i'm talking about :>
    Last edited by ggs; 10-11-2002 at 10:24 PM.
    .sect signature

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    As far as I know (correct me if i'm wrong) variables go into a shared segment of memory for most operating systems and are cached in and out of a stack that is created for that process or created and destroyed shortly after for variables with a small scope. Remember that your compiler generates all the assembler for you so you can't be too sure what exactly it is doing. Some variables may be kept in a single register (even without the "register" keyword) during optimizations.

    Like everyone said, don't count on too much here. This is one area where you should also not be too concerned. Of course if you use an abnormal amount of variables in a small scope you may want to worry, otherwise it should never become an issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 11:05 AM
  2. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 02:08 AM
  3. Variables not being set correctly
    By Mithoric in forum C++ Programming
    Replies: 2
    Last Post: 09-08-2003, 02:56 AM
  4. Replies: 1
    Last Post: 11-27-2001, 01:07 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM

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