Isan Rivkin
Isan Rivkin

Reputation: 167

How can I allocate memory for array inside a function

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 very much appreciated.

#include <stdio.h>
#include <stdlib.h>
int* Init(int* p, int num);
int main() {
    int *p;
    int num, i;
    puts("Enter num of grades:");
    scanf("%d", &num);

    Init(&p, num);
    //for (i = 0; i < num; i++)
    //{
    //  scanf("%d", &p[i]);
    //}
    free(p);
}
int* Init(int* p, int num)
{
    int *pp;
    p = (int *)malloc(num*sizeof(int));
    if (!pp)
    {
        printf("Cannot allocate memory\n");
        return;
    }
    p = pp;
    free(pp);
}

Upvotes: 1

Views: 4047

Answers (5)

OTMANE ER-RAGRAGUI
OTMANE ER-RAGRAGUI

Reputation: 11

It's very important to know that your function doesn't modify your pointer (*p),The **p is lost And *p doesn't have a valid and known memory address in the Main function.

To allocate the memory safely I suggest these two functions.

void init(int **p,int number){
    *p =  malloc(number*sizeof(int));
}

If you want that your function returns the pointer allocated you can do this:

int* init(int number){
    int* p = malloc(number*sizeof(int));
    return p;
}

Upvotes: 1

P.P
P.P

Reputation: 121357

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.

Upvotes: 5

haccks
haccks

Reputation: 106012

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;
}  

Upvotes: 2

d4r3llo5
d4r3llo5

Reputation: 132

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.

Upvotes: 1

David Ranieri
David Ranieri

Reputation: 41017

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));
    ...

Upvotes: 2

Related Questions