Thread: How to access value allocated by malloc

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    73

    How to access value allocated by malloc

    My code is not working as it supposed to do. I want a program that allocates memory for three integer's, then I want to assign value at each memory location, then I want to print value stoared at each location's

    Code:
     #include<stdio.h>
    
    #include<stdlib.h>
    
    
    int main ()
    {
      int i;
      int *ptr;
      ptr = malloc( 2 * sizeof(*ptr));	
      if( ptr != NULL )                      /*Check for failure. */
         {
            *ptr = 0;
             ptr++;	
            *ptr = 1;
             ptr++;	
            *ptr = 2;
             ptr++;	
    
    
    	    for ( i = 0; i < 2; i++)
    		{
    			printf("%d \n", *ptr);
    		    ptr++;
    		}		 
    	 }
    	 
      free( ptr );                           /*Release memory. */
    
    
      return 0;
    }
    I don't get output as I want.

    When I compile code I get this output

    234918894
    7274542

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    ...allocates memory for three integer's
    Code:
    ptr = malloc( 2 * sizeof(*ptr));
    Your code and your intention don't match.
    Don't you see the problem ?

    ptr++; This looses the address of the original pointer.

  3. #3
    Registered User
    Join Date
    Feb 2022
    Posts
    73
    Quote Originally Posted by thmm View Post
    Code:
    ptr = malloc( 2 * sizeof(*ptr));
    Your code and your intention don't match.
    Don't you see the problem ?

    ptr++; This looses the address of the original pointer.
    I get correct value without loop
    Code:
     #include<stdio.h>
    #include<stdlib.h>
    
    int main ()
    {
      int i;
      int *ptr;
      ptr = malloc( 3 * sizeof(*ptr));    
      if( ptr != NULL )                      /*Check for failure. */
         {
             *ptr = 0;
             printf("%d \n", *ptr);
             printf("%p \n", ptr);
             ptr++;
            *ptr = 1;
             printf("%d \n", *ptr);
             printf("%p \n", ptr);
             ptr++;
            *ptr = 2;
             printf("%d \n", *ptr);
             printf("%p \n", ptr);                    
         }
           
      free( ptr );                           /*Release memory. */
    
    
      return 0;
    }
    0
    00730D48
    1
    00730D4C
    2
    00730D50

    I don't know what's wrong with "for" loop in previous program.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ptr++;
    Just stop doing this.
    Do NOT muck about with the pointer you use to store the result of malloc - ever!

    Code:
      ptr = malloc( 3 * sizeof(*ptr));  
      if( ptr != NULL )                      /*Check for failure. */
      {
        for ( i = 0; i < 2; i++)
        {
          ptr[i] = i;
        }
    
        for ( i = 0; i < 2; i++)
        {
          printf("%d \n", ptr[i]);
        }        
      }
          
      free( ptr );
    If you want it in pointer notation, make a copy of the pointer.
    Code:
      ptr = malloc( 3 * sizeof(*ptr));  
      if( ptr != NULL )                      /*Check for failure. */
      {
        int *temp = ptr;
        for ( i = 0; i < 2; i++)
        {
          *temp++ = i;
        }
    
        temp = ptr;
        for ( i = 0; i < 2; i++)
        {
          printf("%d \n", *temp++);
        }        
      }
          
      free( ptr );
    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
    Feb 2022
    Posts
    73
    Quote Originally Posted by Salem View Post
    > ptr++;
    Just stop doing this.
    Do NOT muck about with the pointer you use to store the result of malloc - ever!
    I want to verify whether the dynamic memory is allocated for three integer by malloc and it store value at allocated location

    I understand program allocated space for three integer variables
    Code:
    #include<stdio.h>
     #include<stdlib.h>
      
    int main ()
    {
      int i;
      int *ptr;
     
      ptr = malloc( 3 * sizeof(*ptr));  
      if( ptr != NULL )                     
      {
           
      }
         
      free( ptr );
     
      return 0;
    }
    I want to print allocated dyanamic memory for three integer variables. I want to store values 10, 13 and 19 in allocated memory for three integer variables

    I don't understand how to print allocated memory address and how to store values 10, 13 and 19 in allocated memory for three integer variables

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I want to verify whether the dynamic memory is allocated for three integer by malloc and it store value at allocated location
    Well if malloc returns a non-null pointer, then you have your memory.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
      int asize = 3;
      int *ptr = malloc(asize * sizeof(*ptr));
      if (ptr != NULL) {            /*Check for failure. */
        int *temp = ptr;
        int base = 10;
        for (int i = 0; i < asize; i++) {
          base += i * 3;
          printf("Storing the value %d at memory location %p\n", base, (void *)temp);
          *temp++ = base;
        }
    
        temp = ptr;
        for (int i = 0; i < asize; i++) {
          printf("the value at memory location %p is %d\n", (void *)temp, *temp);
          temp++;
        }
      }
      free(ptr);
      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: 2
    Last Post: 05-06-2019, 02:54 PM
  2. malloc - verifying amount of memory allocated
    By mbxs3 in forum C Programming
    Replies: 10
    Last Post: 09-08-2013, 08:03 PM
  3. Replies: 9
    Last Post: 09-15-2011, 03:28 PM
  4. Replies: 2
    Last Post: 06-25-2010, 04:04 AM
  5. Replies: 4
    Last Post: 12-01-2005, 11:16 PM

Tags for this Thread