Thread: dynamic memory allocation for single variable

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    66

    dynamic memory allocation for single variable

    I want to allocate dynamic memory for a single variable and in that variable I want to store a value 5

    What should my codes be for this

    Code:
    #include<stdio.h>
    #include<stdlib.h>  
    
    
    int main ()
    {
       int *a = (int*) malloc (1 * sizeof(int)); // memory is allocated for varaible a 
      
       return 0;
    }




    What should my codes be for this

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,763
    Code:
    #include<stdio.h>
    #include<stdlib.h>  
     
    int main ()
    {
       int *a = malloc(sizeof(*a)); // memory is allocated for varaible a
       if (a != NULL) { // if the memory was successfully allocated
          *a = 5;   // assign 5 to the "memory location" that a points to
          printf("a is %d\n", *a);
          free(a);    // free the allocated memory
       }
       
       return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    66
    Quote Originally Posted by Hodor View Post
    Code:
    *a = 5;   // assign 5 to the "memory location" that a points to

    Why should we use this sign * because we have already used it * when we were allocating memory for variable

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    733
    The asterisk '*' is used for two different places and purposes.

    Code:
    int *a = NULL;
    is used to create a pointer variable called a, initialized to NULL.

    Code:
    a = malloc(sizeof(*a));
    This allocates memory on the heap and assigns it to the pointer variable, a.

    Code:
    *a = 5;
    Assigns the value 5 to the memory area allocated on the heap and pointed to by a. The asterisk in this statement "dereferences" the pointer before assigning the data.

    a, along with most variable names, should be replaced with a more useful name that represents the use. "count", or something else.

    We usually don't allocate memory for one variable except as part of learning about pointers.

    You need to study a good book on the C Programming Language, and not some short tutorial, or podcast, online.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    697
    Quote Originally Posted by Player777 View Post
    Code:
    *a = 5;   // assign 5 to the "memory location" that a points to
    Why should we use this sign * because we have already used it * when we were allocating memory for variable
    There is a difference between DECLARING a variable and USING it:
    Code:
    int *a; // declare 'a' as a pointer to an int
    a = malloc( sizeof( int ) ); // assign an address of a dynamically allocated block of memory to 'a'.
    *a = 5; // The * here is the indirection operator (access to the address inside 'a').
    Last edited by flp1969; 02-03-2020 at 08:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic memory allocation example
    By abhi143 in forum C Programming
    Replies: 14
    Last Post: 11-05-2019, 12:31 PM
  2. How to add dynamic memory allocation
    By JJ57663 in forum C Programming
    Replies: 1
    Last Post: 04-26-2019, 07:07 PM
  3. dynamic memory allocation
    By abhi143 in forum C Programming
    Replies: 12
    Last Post: 10-09-2018, 07:22 AM
  4. dynamic memory allocation
    By mag_chan in forum C Programming
    Replies: 13
    Last Post: 10-21-2005, 07:54 AM
  5. Dynamic Memory Allocation?
    By motocross95 in forum C++ Programming
    Replies: 11
    Last Post: 12-03-2002, 08:52 PM

Tags for this Thread