Thread: Most confusing topic in Static and Dynamic Memory Allocation

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    1

    Lightbulb Most confusing topic in Static and Dynamic Memory Allocation

    Code:
    Hello Friends,
    
    I have a problem in memory allocation in c programming.
    
    main()
    {
    int i=2;
    printf("The value of i is = %d", &i);
    }
    
    If this is static memory allocation , so why it returns different address after each time we run this program. 
    
    Please give me any kind suggestion ASAP.
    
    Thanks 
    Rohit

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe because the memory allocated for your program's stack happens to be different on your test runs.
    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
    Sep 2011
    Posts
    52
    You're making a integer I, and write the value 2 in it. Than you print the memory location where you strored the value! This haves nothing to do with dynamic / static memory location! You ask your operating system, each time you run this program, a slot of 4 bytes(int). Windows/Linux/Mac(i don't know witch you use) says than somthing like "fine, here you have your 4 bytes". You never specifided the place! This task is for your operating system, he is the one who's take care of that. (luckly for you, otherwise you had to search a place witch is not in use enz, and imagine what a bunch of errors you wil get when you do it wrong enz...)

    Libpgeak

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rohitrajv5 View Post
    If this is static memory allocation , so why it returns different address after each time we run this program.
    Please give me any kind suggestion ASAP.
    It is different because, unless you are working on a very tired old DOS computer, you are dealing with virtual memory. That is to say that when a program loads it is assigned a block of memory from the system pool and as laserlight has alluded, that can (and likely will) be different every time you run the program.

    What this means is that you must not write code that depends on anything being in a fixed location. All you will end up doing is crashing your system. For the most part we neither know nor care about the actual address of anything when writing C code.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    It has nothing to do with VM systems - in fact those will hide the fact that the program is loaded at a different physical address each time by presenting a consistent virtual address space to the program no matter where it loads it. In fact, I'm surprised that the address would change running the same simple code over and over. What OS and compiler are you using?

    I would expect changes if you make changes to the source and recompile - the compiler is free to put variables wherever it wants, but it would be strange that a simple stack variable would move around from run to run without changing the code.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by KCfromNC View Post
    In fact, I'm surprised that the address would change running the same simple code over and over. What OS and compiler are you using?
    I'm surprised too, but that is what happens compiling once and running multiple times (GCC + linux).

    Quote Originally Posted by CommonTater View Post
    It is different because, unless you are working on a very tired old DOS computer, you are dealing with virtual memory. That is to say that when a program loads it is assigned a block of memory from the system pool
    Virtual memory addressing is per process; it is not from a pool. Ie, every single process gets 2-4 GB of address space starting at 0, so eg. the data section, the heap section, et. al. should begin and at roughly the same addresses each time.
    Last edited by MK27; 11-18-2011 at 09:42 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    the value changes in Linux because Linux randomizes the memory layout to prevent stack based hacks.
    Last edited by dmh2000; 11-18-2011 at 05:57 PM.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by dmh2000 View Post
    the value changes in Linux because Linux randomizes the memory layout to prevent stack based hacks.
    This ^^. More recent versions of Windows do this too: Address space layout randomization - Wikipedia, the free encyclopedia


    So in a system without ASLR, virtual memory mapping presents different areas of physical memory as being the same chunk of virtual memory. In this case I'd expect the address of stuff on the stack to stay the same through various runs. With ASLR the actual layout within the virtual memory is randomised.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static memory allocation
    By jimhuf in forum C Programming
    Replies: 5
    Last Post: 10-07-2010, 11:34 AM
  2. Static Memory allocation
    By p3rry in forum C Programming
    Replies: 25
    Last Post: 12-23-2008, 08:30 AM
  3. Dynamic memory allocation
    By Luciferek in forum C++ Programming
    Replies: 118
    Last Post: 10-02-2008, 11:34 AM
  4. Dynamic Memory Allocation
    By schifers in forum C Programming
    Replies: 12
    Last Post: 05-14-2008, 01:49 PM
  5. Dynamic Memory Allocation?
    By motocross95 in forum C++ Programming
    Replies: 11
    Last Post: 12-03-2002, 08:52 PM

Tags for this Thread