Thread: Does pointer qtr hold the same or new memory in every function call

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    96

    Does pointer qtr hold the same or new memory in every function call

    Does pointer qtr hold the same or new memory in every function call in my code

    Code:
     #include<stdio.h>#include<stdlib.h> 
      
    void foo()
    {
       int *qtr = malloc (sizeof(int));
        
        if (qtr != NULL) 
        { 
            *qtr = 10;
        } 
    }
         
    int main()
    {
         foo();
    	 foo();
    	 foo();
        
         
        return 0;    
    }
    I am confused because qtr is a local variable. local variable is create and destroy in function. I think i qtr should hold the same in every function call

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    qtr will receive a different address in all three calls to foo(). You are then throwing away the pointer returned from malloc(), resulting in lost memory.

    If you malloc() memory in a function, you should either free the memory before exiting the function, or return the pointer to the calling function, and have the calling function deal with freeing the memory.

    You should also look at a utility called valgrind, and use it for debugging memory errors.
    Last edited by rstanley; 01-05-2023 at 11:00 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > I think i qtr should hold the same in every function call
    While you're busy thinking about the answer, also think about writing some tests to test your ideas.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void foo()
    {
        int *qtr = malloc (sizeof(int));
        printf("DEBUG: address of variable=%p, value of variable=%p\n",
            (void*)&qtr, (void*)qtr);
        if (qtr != NULL) 
        { 
            *qtr = 10;
        } 
    }
         
    int main()
    {
        foo();
        foo();
        foo();
        return 0;    
    }
    
    DEBUG: address of variable=0x7fffccfc3400, value of variable=0x5599596702a0
    DEBUG: address of variable=0x7fffccfc3400, value of variable=0x5599596706d0
    DEBUG: address of variable=0x7fffccfc3400, value of variable=0x5599596706f0
    Since foo is always called from the same parent, local variables will be at the same address.
    The answer it gets back from malloc will always be different (since you never called free).
    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.

  4. #4
    Registered User
    Join Date
    Oct 2022
    Posts
    96
    I am trying to modify value of structure via function call. I don't understand how to do it in function

    I am passing address of structure to function

    Code:
     #include<stdio.h>
    
    #include<stdlib.h>
    
    
    struct node 
    {
        int x ;
        char y;
    };
    
    
    void modify( struct node **dsptr)
    {
        printf(" dsptr store at location : %p \n", &dsptr);
        printf(" value of dsptr = %p \n", dsptr);
        printf(" %p \n", *dsptr);
        printf(" %p \n", **dsptr);
    }
    
    
    int main(void)
    {
        struct node *sptr = malloc (sizeof(struct node));
        printf(" sptr store at location : %p \n", &sptr);  
        printf(" value of sptr = %p \n", sptr);   
        sptr -> x = 1;         
        sptr -> y = 'A';
        
        printf(" value of first object of structure = %d \n",  sptr -> x);
        printf(" value of second object of structure = %c \n",  sptr -> y);
        
        modify(&sptr);
        
        return 0;
    }
    Output
    Code:
     sptr store at location : 0061FF1C value of sptr = 009F1598
     value of first object of structure = 1
     value of second object of structure = A
     dsptr store at location : 0061FF00
     value of dsptr = 0061FF1C
     009F1598
     00000001

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You don't need the extra indirection.
    Code:
    void modify( struct node *dsptr)
    {
        dsptr->x = 42;
        dsptr->y = 'Q';
    }
     
    int main(void)
    {
        struct node *sptr = malloc (sizeof(struct node));
        printf(" sptr store at location : %p \n", &sptr);  
        printf(" value of sptr = %p \n", sptr);   
        sptr -> x = 1;         
        sptr -> y = 'A';
         
        printf(" value of first object of structure = %d \n",  sptr -> x);
        printf(" value of second object of structure = %c \n",  sptr -> y);
         
        modify(sptr);
         
        return 0;
    }
    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. Replies: 4
    Last Post: 02-13-2022, 12:01 AM
  2. exception handling, function call and memory
    By George2 in forum C++ Programming
    Replies: 21
    Last Post: 01-30-2008, 08:00 AM
  3. Memory overlap in function call
    By wots_guge in forum C Programming
    Replies: 5
    Last Post: 06-06-2006, 03:22 AM
  4. I have the Memory Address of a function, how do I call it?
    By vulcan_146 in forum C++ Programming
    Replies: 8
    Last Post: 05-22-2005, 02:00 AM
  5. How Call a function using it's pointer?
    By WebSnozz in forum C++ Programming
    Replies: 11
    Last Post: 11-12-2001, 12:22 PM

Tags for this Thread