Thread: Value is not being stored at correct location

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    55

    Value is not being stored at correct location

    I am trying to allocate dynamic memory for five integer to store value from 0 to 4
    as shown

    00DE2AB4 = 0
    00DE2AB8 = 1
    00DE2ABC = 2
    00DE2AC0 = 3
    00DE2AC4 = 4

    Code:
    #include<stdio.h>
    
    #include<stdlib.h>
    
    int main ()
    {
      int *x = malloc(5 * sizeof(x));
      
      int i = 0;
      for ( i = 0; i < 5; i++)
      {
          x++;
          printf( "Value of x[%d] = %p\n", i, x );
              
      }
      
      printf("\n");
      
      for ( i = 0; i < 5; i++)
      {
          *x = 1;
          *x++;
          printf("Value %d store at address %p\n", *x ,  i);
      }
      
      return 0;
      
    }
    Value of x[0] = 00022AB4
    Value of x[1] = 00022AB8
    Value of x[2] = 00022ABC
    Value of x[3] = 00022AC0
    Value of x[4] = 00022AC4


    Value 902931021 store at address 00000000
    Value 134222834 store at address 00000001
    Value 1970037078 store at address 00000002
    Value 959520869 store at address 00000003
    Value 808596793 store at address 00000004



    Program output show that memory has been allocated for five integer but value are not being stored at address. Can anyone point out what is reason for this ?
    Last edited by Rahul11; 09-19-2021 at 11:29 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,077
    Please do not destroy values returned from malloc commands!

    Use a second pointer that you then change instead!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    55
    Quote Originally Posted by stahta01 View Post
    Please do not destroy values returned from malloc commands!

    Use a second pointer that you then change instead!

    Tim S.
    I want to store value from 0 to 4 in memory allocated by malloc function

    store value 0 at memory location 1
    store value 1 at memory location 2
    store value 2 at memory location 3
    store value 3 at memory location 4
    store value 4 at memory location 5

    How can this be done

    This line allocate space for an five varaibale of type int.
    Code:
      int *x = malloc(5 * sizeof(x));
    Where is the return value being destroyed by malloc function

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,077
    Code:
    x++;
    The malloc return value was stored in X; changing the value of X results in the value being destroyed!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    The following is one way to write your code:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main ()
    {
       int *x = malloc(5 * sizeof(*x)); // 5 * sizeof(int) NOT sizeof(int *)
    
       // NEVER change the address that malloc() returns!!!  If you change that value,
       // then when you free(x), you will corrupt the heap!!!
    
      int i = 0;
    
      for ( i = 0; i < 5; i++)
      {
         x[i] = i;  // Set the values of the array, 0 - 4
         printf( "Value of x[%d] = %d\n", i, i);
      }
    
      printf("\n");
    
      for ( i = 0; i < 5; i++)
      {           // Cast the address x to a void * to correctly print the address
         printf("Value %d store at address %p\n", x[i] ,  (void *) (x + i));
      }
    
      free(x);  // Free the memory allocated!
    
      return 0;
    }
    I would also use a better name for the variable x. Single letters are usually used as the loop variable for foo(), etc...

  6. #6
    Registered User
    Join Date
    May 2021
    Posts
    55
    Quote Originally Posted by rstanley View Post
    The following is one way to write your code:
    I would also use a better name for the variable x. Single letters are usually used as the loop variable for foo(), etc...
    Thank you rstanley. Next time I would use better name for variable

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-02-2016, 08:47 PM
  2. Replies: 3
    Last Post: 05-13-2015, 06:45 AM
  3. Defining the correct location of a type declaration
    By vinians in forum C Programming
    Replies: 3
    Last Post: 03-22-2015, 07:42 PM
  4. How objects are stored
    By MTK in forum C++ Programming
    Replies: 42
    Last Post: 12-01-2009, 05:29 PM
  5. decimal being stored as 0
    By scoketboy in forum C Programming
    Replies: 7
    Last Post: 11-05-2005, 02:31 AM

Tags for this Thread