Thread: Why does this code give two different memory addresses?

  1. #1
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195

    Why does this code give two different memory addresses?

    I was just looking at this code and I can't explain why it's working in this way. I would expect the value given by &x and ptr to be the same, but it doesn't if you compile and run the program.

    Code:
    int main(int argc, char *argv[])
    {
           int x=5, *ptr;           // x set to 5 for example only
    
           *ptr = x;
    
           printf("%x\n%x\n\n, ptr, &x);
           free(ptr);
    
           return 0;
    }
    I can't understand why they give different addresses, but for some reason, they do. I would be grateful if someone could explain to me why this is the case.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you would like to take a good look at the code below and observe the output:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int x = 5;
        int *ptr;
    
        printf("&x = &#37;p\n", (void*)&x);
    
        ptr = &x;
        printf("ptr = %p\n", (void*)ptr);
    
        ptr = malloc(sizeof(*ptr));
        *ptr = x;
        printf("ptr = %p\n", (void*)ptr);
        free(ptr);
    
        return 0;
    }
    Note that pointers are printed with the %p format specifier, and should be pointers to void when printed, hence the cast.

    EDIT:
    Oops, I noticed a mistake: dynamic memory allocation is in fact needed, so the mistake is not with free(), but the lack of malloc().
    Last edited by laserlight; 06-19-2008 at 11:27 AM.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are invoking undefined behavior because you are trying to use the pointer as a storage.
    You aren't assigning an address to the pointer! How then, can they be the same?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch memory locs with 2 lines of code
    By saltzmanjoelh in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2008, 07:55 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Linking error with memory leak detection code. HELP!
    By Lister in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2003, 03:54 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM