3

I asked the question: Pass array by reference using C. I realized my problem was the usage of the pointer star in C. And eventually it turned out that, this method works fine for my program:

#include <stdio.h>

void FillArray(int** myArray)
{   
     *myArray = (int*) malloc(sizeof(int) * 2);

     (*myArray)[0] = 1;
     (*myArray)[1] = 2;
}

int main()
{
     int* myArray = NULL;
     FillArray(& myArray);  
     printf("%d", myArray[0]);
     return 0;
}

Everyting was fine up to that point. Then, I modified the FillArray() function like the following for a better code readability:

#include <stdio.h>

void FillArray(int** myArray)
{
     int* temp = (*myArray);

     temp = (int*) malloc(sizeof(int) * 2);
     temp[0] = 1;
     temp[1] = 2;
}

int main()
{
     int* myArray = NULL;
     FillArray(& myArray);  
     printf("%d", myArray[0]);
     return 0;
}

Now, I get the following run-time error at the printf line:

Unhandled exception at 0x773115de in Trial.exe: 0xC0000005: Access violation reading location 0x00000000.

Even though I'm not an C expert, it seemed legitimate to do this modifying. However, apparently it doesn't work. Isn't the syntax a little bit confusing? Do I miss something here?

Thanks for your helpful answers,

Sait.

Community
  • 1
  • 1
Sait
  • 19,045
  • 18
  • 72
  • 99

6 Answers6

5

temp gets a copy of the address of myArray, but then you assign some malloced memory to temp, so the original assignment was pointless and had no lasting effect. You then modify the malloced memory, but that doesn't change myArray at all. To change myArray in main, you have to assign

*myArray = temp;

at the end of FillArray.

void FillArray(int** myArray)
{
     int* temp;
     temp = (int*) malloc(sizeof(int) * 2);
     temp[0] = 1;
     temp[1] = 2;
     *myArray = temp;
}

does what you intend.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • @DanielFischer temp actually gets nothing (*myArray) on first line will raise Access Voilation, as myArray is NULL and cannot be dereferenced – Abid May 03 '12 at 21:38
  • @Abid No, he calls `FillArray(& myArray);`, the fact that `myArray` in `main` has the same name as the function parameter is confusing. In `FillArray`, `*myArray` is `NULL`, what `myArray` in `main` is. – Daniel Fischer May 03 '12 at 21:41
  • @DanielFischer oh!! I misread. I though he was passing a ** . thanks – Abid May 03 '12 at 21:43
1

You are assigning the malloc'd buffer to just a local variable on the stack, and then leaking that memory when you return. You are never affecting the pointer passed into the function.

TJD
  • 11,800
  • 1
  • 26
  • 34
0
int* temp = (*myArray);

temp is a local variable. What ever modifications made to it will not affect the original.

temp = (int*) malloc(sizeof(int) * 2);  // Will not affect *myArray

It's more or less like -

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

int *a ;
foo(a);   // Does `a` get allocated too ? NO.
Mahesh
  • 34,573
  • 20
  • 89
  • 115
0

This works for me:

#include <stdio.h>

void FillArray(int** myArray)
{
    (*myArray) = (int*) malloc(sizeof(int) * 2);
    (*myArray)[0] = 1;
    (*myArray)[1] = 2;
}

int main()
{
    int* myArray = NULL;
    FillArray(& myArray);  
    printf("%d\n", myArray[0]);
    printf("%d\n", myArray[1]);
    return 0;
}
0
int* temp = (*myArray);

This line is the root cause of the problem, let see what this line.

It says de-reference myArray and assign the result value to temp which is an int *, and since myArray is and int **, de-referencing would result in a int *. But you cannot de-reference a pointer that is not pointing anywhere, since when you passed myArray as the argument it was NULL and when you do (*myArray) it tries to access the memory location pointed to by myArray ( Which is no-where as myArray is NULL) hence Access violation reading location 0x00000000, meaning you are trying to de-reference a NULL pointer.

Apart from the above lets see what your code is doing The picture in the start is like this

myArray ======> NULL

after 1st line

int* temp = (*myArray); //pointing temp to (*myArray)

since myArray is NULL temp points no where hence the new picture

myArray =======> NULL
temp =======> NULL

line 2 temp = (int*) malloc(sizeof(int) * 2);

Allocating memory of two integers and pointing temp the newly allocated memory so the picture becomes

myArray =======> NULL
temp =======> [ ] [ ]

next two line store 1 and 2 on 1st and 2nd memory locations pointed to by temp

 temp[0] = 1;
 temp[1] = 2;

so now the picture is

myArray =======> NULL
temp =======> [1] [2]
Abid
  • 7,149
  • 9
  • 44
  • 51
-2

Malloc temp before you assign!

Drake Clarris
  • 1,047
  • 6
  • 10