Dynamic memory created inside a function

I would like to know the technical reason (in terms of memory) why this piece of code will not work:

#include <stdio.h>
#include <stdlib.h>

int* fun(int*);
int main()
{
  int a=5;
  int* ptr;
  //  ptr=(int*)malloc(sizeof(int));
  fun(ptr);
  a=*ptr;

  printf("\n the val of a is:%d",a);
  return 0;
}

void fun(int* ptr)
{

  ptr = (int*)malloc(sizeof(int));
  *ptr = 115;


}

Why doesn't it work? I thought that heap (more importantly addresses) is common to all functional variables in the stack.

Also, why does this work. If I comment on the memory allocation inside the fun function and uncomment it in main. It works great.

+5
source share
7 answers

In C, everything is passed by value .

What you pass to fun () is a copy of the pointer that you have in main ().

, ptr ​​ , - 115.

ptr main() undefined, .

, fun() :

#include <stdio.h>
#include <stdlib.h>

int* fun(int**); // <<-- CHANGE
int main()
{
    int a=5;
    int* ptr;
    //  ptr=(int*)malloc(sizeof(int));
    fun(&ptr); // <<-- CHANGE
    a=*ptr;

    printf("\n the val of a is:%d",a);
    return 0;
}

int* fun(int** another_ptr) // <<-- CHANGE
{

    *another_ptr = (int*)malloc(sizeof(int)); // <<-- CHANGE
    **another_ptr = 115; // <<-- CHANGE
    return *another_ptr;
}

, fun() ( ) ptr:

#include <stdio.h>
#include <stdlib.h>

int* fun(int*);
int main()
{
    int a=5;
    int* ptr;
    //  ptr=(int*)malloc(sizeof(int));
    ptr = fun(ptr); // <<-- CHANGE
    a=*ptr;

    printf("\n the val of a is:%d",a);
    return 0;
}

int* fun(int* another_ptr)
{
    another_ptr = (int*)malloc(sizeof(int));
    *another_ptr = 115;
    return another_ptr; // <<-- CHANGE
}

: fun(), , , main(). .

+13

fun() - , fun(). , :

ptr = (int*)malloc(sizeof(int)); 
*ptr = 115;

. :

int* fun(int** ptr) 
{ 
     *ptr = (int*)malloc(sizeof(int)); 
     **ptr = 115; 
} 

, .

+3

, :

fun(&ptr);

(, , fun)

ptr , , , - .

+1

, :

int * fun()
{
  int * ptr = (int*)malloc(sizeof(int));
  * ptr = 115;
  return ptr;
}

, main():

int * ip = fun();
printf( "%d", * ip );
free( ip );

apointer ( ) :

void fun( int ** pp )
{
  * pp = (int*)malloc(sizeof(int));
  ** pp = 115;
}

main() :

int * ip;
fun( & ip );
printf( "%d", * ip );
free( ip );

, , .

+1

ptr fun. fun ptr, . ptr int**.

void fun(int** ptr)
{
   *ptr = (int*)malloc(sizeof(int));
   **ptr = 115;
}

:

fun(&ptr);

( fun, )

0

int* ptr fun. , , ptr ptr = (int*)malloc(sizeof(int));, . , a = *ptr; main(), . , ptr , fun(int** ptr) *ptr = (int*)malloc(sizeof(int));

0

Remember that if you want a function to change the value of an argument, you must pass a pointer to that argument. This refers to pointer values; if you want the function to change the value of the pointer (not what the pointer points to), you must pass a pointer to that pointer:

void fun (int **ptr)
{
  /**
   * Do not cast the result of malloc() unless you are 
   * working with a *very* old compiler (pre-C89).  
   * Doing so will supress a valuable warning if you 
   * forget to include stdlib.h or otherwise don't have 
   * a prototype for malloc in scope.
   *
   * Also, use the sizeof operator on the item you're
   * allocating, rather than a type expression; if you
   * change the base type of ptr (say from int to long),
   * then you don't have to change all the corresponding
   * malloc() calls as well.
   *
   * type of   ptr = int **
   * type of  *ptr = int *
   * type of **ptr = int
   */  
  *ptr = malloc(sizeof **ptr);
  *ptr = 115;
}

int main(void)
{
  int *p;
  fun(&p);
  printf("Integer value stored at %p is %d\n", (void *) p, *p);
  return 0;
}

By the way, you have a type mismatch in your example; your initial declaration funreturns int *, but the definition returns void.

0
source

All Articles