1

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);
}
asked Dec 21, 2015 at 20:02
2
  • 5
    You get run-time errors? I get 6 compiler warnings! Pay heed to them please. Commented Dec 21, 2015 at 20:25
  • Seems like a duplicate to me. Commented Dec 21, 2015 at 20:52

5 Answers 5

5

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.

answered Dec 21, 2015 at 20:10
Sign up to request clarification or add additional context in comments.

2 Comments

1) void Init(int *pp, int num) { ... int *p; ... *pp = p; }?? Did you mean void Init(int **pp, ...? 2) malloc(num*sizeof(int)); may return NULL when n==0 and is not an out-of-memory condition.
@chux Thanks for pointing them out. I have now rewrote the parts to let the caller decide what to do on malloc() returning NULL.
2
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));
 ...
answered Dec 21, 2015 at 20:12

Comments

2

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;
} 
answered Dec 21, 2015 at 20:10

Comments

1

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.

answered Dec 21, 2015 at 20:18

Comments

1

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;
}
xTheWolf
1,9064 gold badges25 silver badges47 bronze badges
answered Dec 21, 2020 at 21:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.