0

I have a function within a file that takes in a pointer to an array represented by a double pointer. I want to copy the contents of a global array within the file to it. But when I dump the array, the data is not correct. What am I doing wrong?

Note: I have malloc'd enough memory for the double pointer to array and pass it into the function using its address

File1:

struct Data DataArray[10];    //global structure

CopyGlobalArray (void ***ArrayBuffer)
{
    memcpy(*ArrayBuffer, &DataArray[0], sizeof(DataArray);
}


File2:

function() {
   Data **MyArray = malloc(sizeof(Data) * 10);
   CopyGlobalArray(&MyArray);

   for (index =0; index<10 ; index++){
     printf(MyArray[Index]->FirstName);
     printf(MyArray[Index]->LastName);
   }
}
6
  • 1
    ArrayBuffer looks like triple pointer, not double. also ) is missing for the memcpy call. Please post your real code. Commented Mar 3, 2018 at 16:10
  • Ah, ArrayBuffer is a double pointer to void*. Commented Mar 3, 2018 at 16:12
  • What are you trying to do? What is the actual problem you try to solve? To really be able to help you we need to see a Minimal, Complete, and Verifiable Example (and know that you tried to debug it yourself, SO is not a free debugging service). And that memcpy call with the parameters you pass? That just seems wrong to me, *ArrayBuffer is not an array (or a pointer to the first element of an array) of Data elements. Commented Mar 3, 2018 at 16:15
  • 1
    Why the heck do you want to do that??? Just pass void * and memcopy. Commented Mar 3, 2018 at 16:15
  • You allocate memory for 10 * sizeof(struct Data) bytes. The malloc function returns a pointer to the first byte. A pointer, not a pointer to a pointer. And using the address-of operator to pass a pointer to MyArray makes no sense. Commented Mar 3, 2018 at 16:24

4 Answers 4

1

It might be hard to visualize, so lets draw the relationship:

The malloc function returns a pointer:

+-------------------+     +----------------------+
| pointer to memory | --> | The actual memory... |
+-------------------+     +----------------------+

However, you declare MyArray as a pointer to a pointer (do struct Data), which is more like

+---------+     +-------------------+     +----------------------+
| MyArray | --> | pointer to memory | --> | The actual memory... |
+---------+     +-------------------+     +----------------------+

The problem is that MyArray doesn't really point to a pointer, it points directly to the actual memory, which means the code will not be correct.

Sign up to request clarification or add additional context in comments.

1 Comment

This makes a little more sense thank you. Will make some modifications.
0

As long as the copy function isn't charged with alocating the memory for the copy, a single pointer is sufficient:

struct Data DataArray[10];    //global structure

CopyGlobalArray (void *ArrayBuffer)
{
     memcpy(ArrayBuffer, DataArray, sizeof(DataArray));
}

If the function is charged with allocating the memory for the copy, you need a double pointer or use the return value, the latter being prefered:

struct Data DataArray[10];    //global structure

void *CopyGlobalArray (void)
{
     void *copy;
     if (copy=malloc(sizeof(DataArray)))
         memcpy(copy, DataArray, sizeof(DataArray));
     return copy;
}

Comments

0

Why are you passing double pointer to void*.You can just do this by a single void*. You can do like this:

 File1:

struct Data DataArray[10];    //global structure

CopyGlobalArray (void *ArrayBuffer)
{
     memcpy(ArrayBuffer, DataArray, sizeof(DataArray);
}


File2:

function() {
    Data *MyArray = malloc(sizeof(Data) * 10);
    CopyGlobalArray(MyArray);
}

Comments

0

In you declare MyArray as a doble pointer

  Data **MyArray = malloc(sizeof(Data) * 10);

the result of malloc should be casted to the double pointer:

 struct Data **MyArray = (struct Data**) malloc(sizeof(Data) * 10);

But providing a single pointer to the array of struct Data should be sufficient for your needs:

void CopyGlobalArray (void *ArrayBuffer)
{
     memcpy(ArrayBuffer, DataArray, sizeof(DataArray));
}

This is how could you use it:

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

struct Data
{
    char * FirstName;
    char * LastName;
};

// global structure
struct Data DataArray[] = {  {.FirstName = "Fred",  .LastName= "Smith"}, {.FirstName = "Eva",  .LastName= "White"}, {.FirstName = "John",  .LastName= "Rock"} }; 

void CopyGlobalArray (void *ArrayBuffer)
{
     memcpy(ArrayBuffer, DataArray, sizeof(DataArray));
}

void print(struct Data *array )
{
    for(size_t i=0; i < sizeof(DataArray)/sizeof(DataArray[0]); i++)
    {
      printf("%s",  array[i].FirstName);
      printf("%s\n", array[i].LastName);
    }
}

struct Data *function()
{
   struct Data *MyArray = (struct Data *) (malloc (sizeof(DataArray) ) );

    CopyGlobalArray (MyArray);

    // Copied array
    printf("\nMyArray:\n");
    print(MyArray);

   return MyArray; 
}

int main(void)
{
     // Original 
    printf("\nDataArray:\n");
    print(DataArray);

    struct Data *MyArray1 = function();

    printf("\nMyArray1:\n");
    print(MyArray1);   

    free(MyArray1); // free the array allocated inside function()
    return 0;
}

Output:

DataArray:
FredSmith
EvaWhite
JohnRock

MyArray:
FredSmith
EvaWhite
JohnRock

MyArray1:
FredSmith
EvaWhite
JohnRock

Comments

Your Answer

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.