Chris
Chris

Reputation: 8422

Problem with passing pointer to function in C

This is one of those annoying things where you know the answer is easy, but you just can't see it.

The printf statement in AllocIntArray shows that arrayPtr is correctly being assigned a memory location, however when the printf statement in main is run, it shows arrayB is still set to NULL.

Can someone show me what I am doing wrong when passing in arrayB to AllocIntArray?

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

void AllocIntArray(int *arrayPtr, int numElements);

int main()
{
   int *arrayB = NULL;

   AllocIntArray(arrayB, 10);
   printf("Pointer: %p\n", arrayB);

   free(arrayB);

   getchar();
   return EXIT_SUCCESS;
}

void AllocIntArray(int *arrayPtr, int numElements)
{
   arrayPtr = (int *)malloc(sizeof(int) * numElements);
   printf("Pointer: %p\n", arrayPtr);

   if(arrayPtr == NULL)
   {
      fprintf(stderr, "\nError allocating memory using malloc");
      exit(EXIT_FAILURE);
   }
}

Upvotes: 0

Views: 6287

Answers (5)

KRUPASINDHU MOHANTY
KRUPASINDHU MOHANTY

Reputation: 71

The basic problem here is you are passing arrayB to AllocIntArray function as passed by value .In AllocIntArray its allocating memory properly and arrayptr is valid but in main function its not the same memory which you are expecting . This is the basic C programming concept and you can check bu adding print in both the function .

EX: I am sharing the difference between both problem and success case with below example .

/*Code with passed by value as a parameter*/ 
#include<stdio.h>
#include<stdlib.h>
void AllocateIntarray(int *arrayptr,int numElements)
{
     arrayptr = (int*) malloc(sizeof(int)*numElements);
     printf("Inside _func_AllocateIntarray_pointer:%p\n",arrayptr);

     if(arrayptr == NULL)
     {
         printf("ERR_MEM_ALLOCATION_FAILED:\n");
     }
}

int main()
{
     int *arrayB = NULL;
     AllocateIntarray(arrayB,10);
     printf("Inside _func_mainPointer:%p\n",arrayB);

     free(arrayB);
     return 0;
 }
/*Output :
 Inside _func_AllocateIntarray_pointer:0x55be51f96260
 Inside _func_mainPointer:(nil)*/

Code with Passed by reference and using double pointer .

#include<stdio.h>
#include<stdlib.h>
void AllocateIntarray(int **arrayptr,int numElements)
{
     *arrayptr =  malloc(sizeof(int)*numElements);
     printf("Inside _func_AllocateIntarray_pointer:%p\n",*arrayptr);

     if(*arrayptr == NULL)
     {
          printf("ERR_MEM_ALLOCATION_FAILED:\n");
     }
 }
 int main()
 {
     int *arrayB = NULL;
     AllocateIntarray(&arrayB,10);
     printf("Inside _func_mainPointer:%p\n",arrayB);

     free(arrayB);
     return 0;
  }
  /*Output :
    Inside _func_AllocateIntarray_pointer:0x562bacd1f260
    Inside _func_mainPointer:0x562bacd1f260*/

Upvotes: 0

Fred Foo
Fred Foo

Reputation: 363577

That's because arrayB is passed to AllocIntArray by value. Either pass it by reference (with a pointer-to-pointer), or better, return it from AllocIntArray:

int *AllocIntArray(int numElements)
{
   int *arrayPtr = malloc(sizeof(int) * numElements);
   printf("Pointer: %p\n", arrayPtr);

   if(arrayPtr == NULL)
   {
      fprintf(stderr, "\nError allocating memory using malloc");
      exit(EXIT_FAILURE);
   }
   return arrayPtr;
}

Upvotes: 2

Jim Blackler
Jim Blackler

Reputation: 23169

arrayPtr is a pointer and the pointer is passed by value to the parameter. AllocIntArray can modify its version of arrayPtr but the changes won't be seen by main().

(Edit: if you're using C++) modifying the signature for AllocIntArray to change the type of arrayPtr to a reference ought to fix your problem.

void AllocIntArray(int *&arrayPtr, int numElements)

Upvotes: 0

kalyan
kalyan

Reputation: 3106

Pass the double pointer.

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

void AllocIntArray(int **arrayPtr, int numElements);

int main()
{
   int *arrayB = NULL;

   AllocIntArray(&arrayB, 10);
   printf("Pointer: %p\n", arrayB);

   free(arrayB);

   getchar();
   return EXIT_SUCCESS;
}

void AllocIntArray(int **arrayPtr, int numElements)
{
   *arrayPtr = malloc(sizeof(int) * numElements);
   printf("Pointer: %p\n", *arrayPtr);

   if(*arrayPtr == NULL)
   {
      fprintf(stderr, "\nError allocating memory using malloc");
      exit(EXIT_FAILURE);
   }
}

Upvotes: 4

Neowizard
Neowizard

Reputation: 3017

You need to brush up a bit on parameter passing to functions.

The pointer you are sending to AllocIntArray is being copied into arrayPtr, The line

   arrayPtr = (int *)malloc(sizeof(int) * numElements);

assigns a value into the copy, and not the original variable, and therefore the original variable still points to nowhere.

First solution that comes to mind is to send a pointer to that pointer, but I think you'd best do some general brushing up on the matter of parameter passing before going much further.

Upvotes: 0

Related Questions