fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define SIZE 30
  4.  
  5. void allocate_memory(int*,int);
  6.  
  7. int main(void){
  8. int * pointer = NULL ; //set pointer to null value
  9. int array_size = SIZE;
  10. printf("\nMAIN:%p" ,(void*) pointer ); //value of pointer is null of course
  11. allocate_memory(pointer,array_size);
  12. printf("\nMAIN:%p" ,(void*) pointer ); // value of pointer is still null, even though allocate_memory was called ergo no memory was allocated.
  13. free(pointer);
  14. return 0 ;
  15. }
  16.  
  17. void allocate_memory(int *pointer,int size)
  18. {
  19. pointer = malloc(size*sizeof(int));
  20. printf("\nFUNCTION: %p" ,(void*) pointer ); //value of pointer is null of course
  21. if(pointer==NULL)
  22. {
  23. printf("Memory allocation fail!");
  24. exit(1);
  25. }
  26. else
  27. printf("\nMemory allocation successful");
  28.  
  29.  
  30. }
Success #stdin #stdout 0s 1920KB
stdin
Standard input is empty
stdout
MAIN:(nil)
FUNCTION: 0x8de5008
Memory allocation successful
MAIN:(nil)