Like Tree2Likes
  • 1 Post By KCfromNC
  • 1 Post By dmh2000

Most confusing topic in Static and Dynamic Memory Allocation

This is a discussion on Most confusing topic in Static and Dynamic Memory Allocation within the C Programming forums, part of the General Programming Boards category; Code: Hello Friends, I have a problem in memory allocation in c programming. main() { int i=2; printf("The value of ...

  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
    21,754
    Maybe because the memory allocated for your program's stack happens to be different on your test runs.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.
    MK27 likes this.

  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 08: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 04:57 PM.
    hauzer likes this.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    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, 07: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, 07:52 PM

Tags for this Thread


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