Thread: How to assign values to dynamic memory

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

    How to assign values to dynamic memory

    I have allocated memories for five integer variables. I want to store values 1, 3, 10, 9, 5

    Code:
     #include<stdio.h>#include<stdlib.h>
    
    
    int main ()
    {
       int *var = malloc( 5 * sizeof(int));
       
       if ( var != NULL )
       {
    	   
       }
    	return 0;
    }
    I don't understand how to store all values in allocated space ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The same way you would with an array of 5 elements.

    var[0] = 1;
    var[1] = 3;
    etc.
    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
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Quote Originally Posted by Kittu20 View Post

    I don't understand how to store all values in allocated space ?
    Well Salem presented the easiest way but you could also used pointer arithmetic to access the allocated memory.

  4. #4
    Registered User
    Join Date
    Oct 2022
    Posts
    90
    Quote Originally Posted by Salem View Post
    The same way you would with an array of 5 elements.

    var[0] = 1;
    var[1] = 3;
    etc.
    I have allocated space for only five integers , so only five values should be store, why are last two variables being stored ?

    Code:
     #include<stdio.h>#include<stdlib.h>
    
    
    int main ()
    {
       int *var = malloc( 5 * sizeof(int));
       
       if ( var != NULL )
       {
    	   var[0] =  1;
    	   var[1] =  3;
    	   var[2] =  10;
    	   var[3] =  9;
    	   var[4] =  5;
    	    
    	   var[5] =  15;
    	   var[6] =  50;
    	   
        for ( int i = 0; i < 7; i++)
    	{
    	      printf("%d ", *var);
    	       var++;
    	}
       }
    	return 0;
    }
    1 3 10 9 5 15 50

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for ( int i = 0; i < 7; i++)
    Why do you believe storing 7 things when you only allocated space for 5 would do anything at all?

    > why are last two variables being stored ?
    Because if you want to hang yourself, C will happily give you the rope to do it.
    There are very few built-in checks that stop you doing dumb stuff (like writing past the end of your allocated space).


    Later on, you would likely feel the pain when
    - you tried to free the memory
    - you tried to allocate more memory.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2022
    Posts
    90
    Quote Originally Posted by Salem View Post
    > for ( int i = 0; i < 7; i++)
    Later on, you would likely feel the pain when
    - you tried to free the memory
    - you tried to allocate more memory.
    I allocated memory for three variables, then allocated for two variables and then freed allocated memory

    Code:
     #include<stdio.h>#include<stdlib.h>
    
    
    int main ()
    {
      // Memory allocate for 3 variables 	
      int *var = malloc(3* sizeof(int));
      
      if ( var != NULL )
      {
    	var[0] =  10;
    	var[1] =  3;
    	var[2] =  9;
      }
      
      // memory allocate for more two variables, total memory allocated for five variables  
      var = realloc(var, 5 * sizeof(int)) ;
      
      if ( var != NULL )
      {
    	var[3] = 9;  
    	var[4] = 5;
    	
    	printf(" %d", *var);
      }
      
      free(var); 
      
      printf(" %d", *var); 
    
    
      return 0;
    }
    10 10

    I don't understand why the value is stored even after the memory is freed

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Again, C lets you do dumb stuff.

    The memory doesn't physically go away just by calling free.
    What you're saying by calling free is along the lines of "I've finished with this memory, someone else can use it, and I promise not to use it".

    If you go on to break that promise, well you should read my sig.

    What you have is a 'use after free' bug.
    Code:
    $ gcc -g foo.c
    $ valgrind ./a.out 
    ==27728== Memcheck, a memory error detector
    ==27728== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
    ==27728== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
    ==27728== Command: ./a.out
    ==27728== 
    ==27728== Invalid read of size 4
    ==27728==    at 0x109251: main (foo.c:29)
    ==27728==  Address 0x4a5c090 is 0 bytes inside a block of size 20 free'd
    ==27728==    at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==27728==    by 0x10924C: main (foo.c:27)
    ==27728==  Block was alloc'd at
    ==27728==    at 0x483DFAF: realloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==27728==    by 0x109200: main (foo.c:17)
    ==27728== 
     10 10==27728== 
    ==27728== HEAP SUMMARY:
    ==27728==     in use at exit: 0 bytes in 0 blocks
    ==27728==   total heap usage: 3 allocs, 3 frees, 1,056 bytes allocated
    ==27728== 
    ==27728== All heap blocks were freed -- no leaks are possible
    ==27728== 
    ==27728== For lists of detected and suppressed errors, rerun with: -s
    ==27728== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
    Tools like valgrind can help you find out where your mistakes are.
    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. Why does C assign values to arrays like this?
    By FromJavatoC in forum C Programming
    Replies: 12
    Last Post: 08-12-2011, 04:52 PM
  2. Assign values to a set of array of structures
    By glucosonte in forum C Programming
    Replies: 1
    Last Post: 08-26-2009, 08:10 AM
  3. Assign an arrays values to another array
    By laczfinador in forum C Programming
    Replies: 3
    Last Post: 05-06-2009, 07:46 AM
  4. assign values to string arrays
    By paulur in forum C Programming
    Replies: 3
    Last Post: 03-22-2006, 01:37 AM
  5. 3-d array assign string values
    By WaterNut in forum C++ Programming
    Replies: 8
    Last Post: 07-01-2004, 12:02 AM

Tags for this Thread