Thread: what address of memory, after malloc()

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    81

    what address of memory, after malloc()

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct student
    {
      int roll_no;
    };
    
    int main()
    {
      
      struct student *st = (struct student*) malloc(4 * sizeof(struct student));
      
      st->roll_no = 10;
      
      printf(" value : %d \n", (st )-> roll_no);
      printf(" address : %p \n", (st )-> roll_no);
      
      return 0;
    }
    value : 10
    address : 0000000A

    What would be addresses of the memory after malloc() ?
    Last edited by gajya; 11-14-2019 at 02:36 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just print the pointer whose value was returned by malloc:
    Code:
    printf(" address : %p \n", (void*)st);
    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
    Oct 2019
    Posts
    81
    Quote Originally Posted by laserlight View Post
    Just print the pointer whose value was returned by malloc:
    Code:
    printf(" address : %p \n", (void*)st);
    I have doubt Am I printing correct value of N
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    struct student
    {
      int roll_no;
      struct student *N;  
    };
     
    int main()
    {
       
      struct student *st = (struct student*) malloc(4 * sizeof(struct student));
      struct student *s1 = (struct student*) malloc(4 * sizeof(struct student));
      
      printf(" address : %p \n", (void*)st);
       
      st->roll_no = 10;
      st -> N = s1;
       
      printf(" value : %d \n", (st )-> roll_no);
      printf(" N : %p \n", (st )-> N);
    
    
      
      return 0;
    }
    address : 00B10DD8
    value : 10
    N : 00B10E00

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Explain why you have such a doubt.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I have doubt Am I printing correct value of N
    Yes you are.

    Maybe you're thinking, the difference between DD8 and E00 is 40 bytes, but the size of the structure is 8, and I'm allocating 4 of them (32 bytes).

    So the difference between the two pointers should be 32 right?
    No, such a thing would be a false assumption.


    Two malloc'ed pointers are un-comparable.
    You can't infer anything from comparing one malloc pointer from another.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get address returned by malloc()?
    By ChickenChowMinh in forum C Programming
    Replies: 5
    Last Post: 12-07-2013, 01:38 AM
  2. Replies: 10
    Last Post: 07-10-2012, 04:28 AM
  3. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM
  4. malloc address
    By Max in forum C Programming
    Replies: 8
    Last Post: 10-04-2002, 04:45 PM

Tags for this Thread