Thread: Memory Pointers

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    52

    Memory Pointers

    When a program dynamically allocates memory using malloc or calloc is the pointer value returned an offset from some memory location relative to the programs assigned memory block or is it a raw memory value?

    I am just curious about this because I would have expected this code:

    EDITED: fixed a bug in the code, the change doesn't effect the concept of what the code should do.

    Code:
    pid_t parent = getpid();
    pid_t *pid = malloc(sizeof(pid));
    pid_t tmp = fork();
    if (tmp != 0) *pid = tmp;
    printf("%d\n", getpid();
    to print:

    Code:
    1277
    1277
    but instead it still prints:
    Code:
    0
    1277
    when the child prints first, or:

    Code:
    1277
    0
    when the parent prints first.

    This made me think that pointers are actually assigned an offset value rather than an absolute value. Is this then the reason mmap() is necessary?
    Last edited by jdodle; 04-25-2016 at 09:44 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,426
    Your program is equivalent to
    pid_t tmp = fork();
    printf("%d\n", getpid();

    This has nothing to do with pointers.
    What you're seeing is the randomness of which process gets to run.

    Post an actual program, not random lines.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    52
    Quote Originally Posted by Salem View Post
    Your program is equivalent to
    pid_t tmp = fork();
    printf("%d\n", getpid();

    This has nothing to do with pointers.
    What you're seeing is the randomness of which process gets to run.

    Post an actual program, not random lines.
    Oh sorry, I missed that, it is a mistake. This is what it should have been:

    Code:
    pid_t parent = getpid();
    pid_t *pid = malloc(sizeof(pid));
    pid_t tmp = fork();
    if (tmp != 0) *pid = tmp;
    printf("%d\n", *pid);
    


  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,426
    So you're just printing the return result of fork(), or whatever garbage was in tmp when you called malloc.

    pid_t parent = getpid();
    pid_t *pid = malloc(sizeof(pid));
    pid_t tmp = fork();
    if (tmp != 0) *pid = tmp;
    else *pid = 42;
    printf("%d\n", *pid);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2016
    Posts
    52
    Quote Originally Posted by Salem View Post
    So you're just printing the return result of fork(), or whatever garbage was in tmp when you called malloc.
    I find it hard to believe that you don't understand the concept, but just to be absolutely clear, I am assuming malloc does in fact give a zeroed memory chunk. To be even more deliberate, I will use calloc instead. Happy?

    Code:
    pid_t parent = getpid();
    pid_t *pid = calloc(sizeof(pid_t), 1);
    pid_t tmp = fork();
    if (tmp != 0) *pid = tmp;
    printf("%d\n", *pid);
    The thing is that I am not actually asking about this to fix a problem, I am actually using this as an example to ask if pointers are absolute addresses in memory or if they are an offset or something.

    The code above is evidence that pointers are actually not absolute addresses. Please answer the question rather than clinging on to small mistakes in my code. It is just and example.

    See my original post for the behavior of this code.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by jdodle View Post
    I am assuming malloc does in fact give a zeroed memory chunk.
    Then you are assuming wrong. malloc allocates memory. What happens to it after that is entirely up to you.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Jan 2016
    Posts
    52
    Quote Originally Posted by Elkvis View Post
    Then you are assuming wrong. malloc allocates memory. What happens to it after that is entirely up to you.
    I was not assuming for correctness, I was assuming because I was not concerned with semantics. Again please do not cling onto the codes correctness, it is just an example!

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Pointers are essentially absolute. They are actually abstract as far as the language standard is concerned, so could conceivably be implemented in many ways. But for a standard PC these days, they are absolute values, essentially unsigned integers of 32 or 64 bits.

    I don't understand how your code demonstrates anything at all. I think that you perhaps don't exactly understand what fork does. It creates an entirely separate process (with its own completely separate virtual memory address space, which I think is where your confusion lies). The initial value of the memory of the new process is a copy of the parent (with some minor differences such as the pid_t returned by fork).

    Look at what your code is doing:
    Code:
    // Set parent to the pid of this process
    pid_t parent = getpid();
    
    // Allocate a small chunk of memory, storing its address in pid.
    pid_t *pid = calloc(sizeof(pid_t), 1);
    
    // Fork this process into two processes.
    // The parent process will recieve the pid_t of the child.
    // The child will receive 0, indicating that it is the child.
    pid_t tmp = fork();
    
    if (tmp != 0) {   // if this is NOT the child
        *pid = tmp;   // store the returned (child) pid in *pid.
    }
    
    // Now both processes will print the contents of that memory location.
    // Remember that they both have completely separate virtual memory spaces
    // so that even though the virtual address is the same, the actual
    // location in physical RAM is different.
    // Only the parent will have actually stored a value in its location.
    printf("%d\n", *pid);

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    For a PC and most operating systems, a pointer will be a virtual address within a virtual address space. Each process has it's own virtual address space, and each virtual address space will exist in a separate physical address space. In the case of Windows operating systems, memory can be shared between processes, but the virtual address to the base of the shared memory may be different for each process, so this would be a case where different virtual addresses point to the same physical memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory and pointers
    By tull2 in forum C Programming
    Replies: 2
    Last Post: 10-11-2010, 09:15 AM
  2. Not sure. (Pointers) Memory acess.
    By kevinawad in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2008, 08:55 AM
  3. Memory pointers
    By electrolove in forum C Programming
    Replies: 12
    Last Post: 02-05-2003, 05:48 AM
  4. pointers and memory
    By itld in forum C++ Programming
    Replies: 4
    Last Post: 02-06-2002, 11:34 PM
  5. Memory to pointers
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-17-2001, 05:13 PM