Thread: question about order in how the data is placed in memory.

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    147

    question about order in how the data is placed in memory.

    I have developed my program for a few years now. I have a data.c where I have put all global staff and vars, also I have a data.h declaring all data to be used in other modules, but unfortunatly data inside both files are not sorted and writted in the same way. I would like to now how the compiler put the data in memory, the way that is in data.c, or the order that is in data.h,
    Is only I question I would like to now for "general culture", not by practical reasons, althought I supposed it could affect how cache do it.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Hmm...so I'm not sure what your last sentence means, but there is no guarantee to how your data is laid out in general. Neither for global/static duration variables (which you should avoid for these reasons), nor for stack variables or function parameters. It's completely up to the compiler to pick whatever order it wants. In the old days, this was usually the order you declared them in, though modern, optimizing compilers may rearrange things without telling you. Anyhow, the exact location is of no importance to you, save that all global/static duration variables will be together in one clump in a data section, while all parameters and variables local to a function will be clumped together in a separate area called the stack. Note that each instance of a function (as in recursive functions) have their own copy of parameters and local variables, at different places on the stack.

    Here's some good reading:
    process memory layout - Google Search

    EDIT: And as for the cache, it doesn't really care where the data is, but it does cache other memory cells nearby the address you requested, due to the principle of locality. Here's some cache reading: http://en.wikipedia.org/wiki/CPU_cache.
    Last edited by anduril462; 06-10-2011 at 02:28 PM.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Kempelen View Post
    I have developed my program for a few years now. I have a data.c where I have put all global staff and vars, also I have a data.h declaring all data to be used in other modules, but unfortunatly data inside both files are not sorted and writted in the same way. I would like to now how the compiler put the data in memory, the way that is in data.c, or the order that is in data.h,
    Is only I question I would like to now for "general culture", not by practical reasons, althought I supposed it could affect how cache do it.
    Like anduril above said, it is very unlikely you will be able to control what the compiler does unless you have a very deep understanding of the compiler and of the architecture you are compiling on. In 99% of the cases the compiler will make "better" (read more informed) decisions than you ever will.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If you want to control the ordering of variables, put them in a struct.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. print the data by order (i.e by last name)
    By SalamuB in forum C Programming
    Replies: 0
    Last Post: 12-08-2010, 05:10 PM
  2. Replies: 4
    Last Post: 11-01-2009, 06:19 AM
  3. Order of Memory Locations
    By vlrk in forum Linux Programming
    Replies: 11
    Last Post: 06-30-2008, 07:11 AM
  4. Memory Order in Classes
    By skewray in forum C++ Programming
    Replies: 13
    Last Post: 12-14-2006, 06:40 AM
  5. byte and bit order question
    By chunlee in forum C Programming
    Replies: 7
    Last Post: 11-07-2004, 01:50 PM