Thread: Memory allocation at run time

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

    Memory allocation at run time

    Can someone clarify what's memory allocation at run time ?

    When I need to write a program I write it on editor and save it. when i compile/run program. compiler allocate memory. if there is error in code compiler show error. if there is no error then compiler show result

    Code:
    #include<stdio.h>#include<stdlib.h>
     
    int main ()
    {
      int *x = malloc(sizeof(*x)); // allocate dynamic memory for x
      *x = 1;  
      printf( "x = %d\n", *x);
    
    
      return 0;
       
    }
    In a program dynamic memory will allocate for pointer when i compile and run the code

    What's term " Memory allocation at run time " in code development?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you're doing dynamic allocation (which is also the definition of "at runtime") when you call malloc / calloc / realloc / free.

    Your code should be
    Code:
    #include<stdio.h>
    #include<stdlib.h>
      
    int main ()
    {
      int *x = malloc(sizeof(*x)); // allocate dynamic memory for x
      if ( x != NULL ) { // always check, it might fail
        *x = 1;  
        printf( "x = %d\n", *x);
        free(x);  // always free when you're done.
      }
    
      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. Allocation time.
    By RyanC in forum C Programming
    Replies: 1
    Last Post: 12-03-2018, 08:42 AM
  2. Help! -Linked Lists, Memory Allocation, Time Steps, Debugg
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-14-2009, 08:50 PM
  3. Memory allocation error (Nicely formatted this time)
    By killiansman in forum C Programming
    Replies: 1
    Last Post: 06-08-2007, 11:14 AM
  4. Memory allocation
    By ski_photomatt in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 01:52 PM
  5. memory allocation!
    By coo_pal in forum C Programming
    Replies: 11
    Last Post: 04-04-2003, 01:06 AM

Tags for this Thread