How can I allocate memory for array inside a function

前端 未结 4 1575
逝去的感伤
逝去的感伤 2020-12-21 19:41

I am trying to receive a number from the user. And create an array with that number, but, inside a function. Here are my few attempts, I get into run time errors. Help is ve

相关标签:
4条回答
  • 2020-12-21 19:59

    You have done well upto the point you understood you need to pass a pointer to pointer. But your function signature doesn't take an int **. Either you pass a pointer to pointer and store the allocated memory in it:

    void Init(int **pp, int num)
    {
        int *p;
        p = malloc(num*sizeof(int));
        if (!p)
        {
            printf("Cannot allocate memory\n");
        }
        *pp = p;
    }
    

    And check if the Init() returns a proper pointer:

       Init(&p, num);
       if(p == NULL) {
          /*Memory allocation failed */
       }
    

    Or allocate memory and return the pointer:

    int* Init(int num)
    {
        int *p;
        p = malloc(num*sizeof(int));
        if (!p)
        {
            printf("Cannot allocate memory\n");
        }
    
        return p;
    }
    

    and from main() call as:

    int * p = Init(num);
    if(p == NULL) {
       /*Memory allocation failed */
    }
    

    Change the prototype of Init() accordingly.

    In any case, you must not free() the pointer in Init(). That just de-allocates memory immediately and you'll be left with a dangling pointer.

    And you need to free() in the main() after you are done with it.

    0 讨论(0)
  • 2020-12-21 20:05

    Some typos in your code,

    p = (int *)malloc(num * sizeof(int));
    

    should be

    pp = (int *)...
    

    Your free(pp); is what is causing it to not work chiefly, you do not want to call that or the memory you allocated will not be saved. Also the memory of pp is essentially "lost" at the end of the function call as method parameter to Init p is a value copy not exact reference to main's version of p, thus when Init returns, the changes to p are 'lost'.

    simply do: p = Init(); and in init return pp;

    Exp: This line p = pp, sets variable p to point to the memory allocated by pp, thus a free of pp is a free to p as well. I am not sure if returning an address to memory is always considered good practice, as you have to ensure it is freed, but for your program it would work.

    0 讨论(0)
  • 2020-12-21 20:09

    First you need to fix the prototype of your function. It should be

    int* Init(int** p, int num);  
    

    Then fix the function definition

    int* Init(int** p, int num)
    {
        //int *pp;   // You don not need this variable
        *p = malloc(num*sizeof(int));   // Allocate memory
        if (!*p)
        {
            printf("Cannot allocate memory\n");
            return NULL; // Return a NULL pointer
        }
    
        return *p;
    }  
    
    0 讨论(0)
  • 2020-12-21 20:13
    int *pp;
    p = (int *)malloc(num*sizeof(int));
    if (!pp) /* pp is used uninitialized at this point */
    

    int *p;
    int num, i;
    puts("Enter num of grades:");
    scanf("%d", &num);
    Init(&p, num);
    free(p); /* p is used uninitialized at this point */
    

    If you want to allocate space for a pointer to int inside another function, you need to pass a pointer to pointer:

    ...
    Init(&p, num);
    ...
    int Init(int **pp, int num)
    {
        *pp = malloc(num * sizeof(int));
        ...
    
    0 讨论(0)
提交回复
热议问题