Thread: Memory location of variable

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    41

    Memory location of variable

    Code:
    #include <stdio.h>
    int main()
    {
        int i=3;
        float j=1.5;
        char k='c';
        printf("%u, %u, %u\n", &i, &j, &k);
        return 0;
    }
    My book said the memory location of these variables is contiguous. But I have got like below :
    Code:
    3216967268, 3216967272, 3216967279
    And three warnings :
    Code:
    gcc -Wall -o "untitled1" "untitled1.c" (in directory: /home/duh/Desktop/C)
    untitled1.c: In function ‘main’:
    untitled1.c:7:2: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat]
    untitled1.c:7:2: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 3 has type ‘float *’ [-Wformat]
    untitled1.c:7:2: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 4 has type ‘char *’ [-Wformat]
    Compilation finished successfully.
    How to avoid these error message?

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by justine
    My book said the memory location of these variables is contiguous.
    Your book makes an assumption that is not necessarily true.

    Quote Originally Posted by justine
    And three warnings :
    That is because the correct specifier to use to print pointers to void is %p, e.g.,
    Code:
    printf("%p, %p, %p\n", (void*)&i, (void*)&j, (void*)&k);
    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
    Nov 2012
    Posts
    41
    Your book makes an assumption that is not necessarily true.
    Can you please explain it? You said the book is wrong, or it may be because of my 32 bit compilar?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your book is wrong because the memory locations for those variables are not guaranteed to be contiguous.
    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

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    Ok, Thank you.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Please tell us the book.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    It is Let Us C by YPK. But I thing it is not specifying that it will be contiguous, but during a program for showing the increase of memory location by one it point to the next location of its type, He gives :
    First address of int=65524
    First address of float=65520
    First address of char=65519
    Final address of int=65528
    Final address of float=65524
    Final address of char=65520

    May be I misunderstood this.

    Hope you get me. Not good at english.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It's not guaranteed to be increasing in memory. The compiler is free to allocate variables in any order. It's not specified in the C standard how a compiler translates user code into machine instructions and absolute addresses. Only that for an array, its elements are contiguous in memory and therefore pointer arithmetic is allowed.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Furthermore, modern compilers often randomize the variable locations on purpose for security reasons. Read more here: Address space layout randomization - Wikipedia, the free encyclopedia.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Let Us C by YPK
    I figured as much. That book is well-known to be crap.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory location overwritten
    By burningmosfet in forum C Programming
    Replies: 12
    Last Post: 03-13-2011, 12:07 PM
  2. allocate apecified memory location for a c variable
    By BharathKumar in forum Linux Programming
    Replies: 5
    Last Post: 06-01-2007, 03:47 PM
  3. Memory Location
    By SomC in forum C Programming
    Replies: 6
    Last Post: 05-08-2004, 02:54 AM
  4. memory location
    By xlordt in forum C Programming
    Replies: 3
    Last Post: 10-21-2002, 09:39 AM
  5. accessing Memory location
    By a_learner in forum C Programming
    Replies: 6
    Last Post: 10-10-2001, 02:16 PM