Dynamically allocating an array in a function in C

Chen Sokolovsky

I am having a problem in a program I am writing. This small program is a short version just to show the problem.

For this example, I am defining a structure called point which has an X and Y. I want the function to calculate the number of points, In this example I am assuming always 5, but this is not constant in my real program.

#include <stdio.h>

typedef struct  point {
   int x;
   int y;
}point;


// suppose this is dynamic. it return a value according to some parameter;
int howManyPoints() {
   // for this demo assume 5.
   return 5;
}


int createAnArrayOfPoints(point** outArray,int* totalPoints) {

   // how many points?
   int neededPoints = howManyPoints();

   // create an array of pointers
   *outArray =malloc(neededPoints * sizeof(point*));

   // malloc memory of a point size for each pointer
   for (int i=0;i<neededPoints;i++) outArray[i] = malloc(sizeof(point));

   // fill the points with some data for testing
   for (int k=0;k<neededPoints;k++) {
      outArray[k]->x = k*10;
      outArray[k]->y = k*5;
   }

   // tell the caller the size of the array
   *totalPoints = neededPoints;

   return 1;
  }


int main(int argc, const char * argv[]) {

   printf("Program Started\n");

   point* arrayOfPoints;
   int totalPoints;
   createAnArrayOfPoints(&arrayOfPoints,&totalPoints);

   for (int j=0;j<totalPoints;j++) {
    printf("point #%d is at %d,%d\n",j,arrayOfPoints[j].x,arrayOfPoints[j].y);
   }

   printf("Program Ended\n");

   return 0;
}

My console output looks like this:

Program Started
point #0 is at 0,0
point #1 is at 0,0
point #2 is at 10,5
point #3 is at 0,0
point #4 is at 20,10
Program Ended

What am I doing wrong? I am expecting all 5 points to have values in them..

Thanks.

junix

You have a mismatch in your representation for the array: In your main you are expecting an array of points (point* arrayOfPoints;) that is one consecutive piece of memory. However, the way you allocate it in createAnArrayOfPoints is different:

In that function you let arrayOfPoints point at a piece of memory only carrying pointers to point and initialize it with pointers to memory of the size of point you allocated. This is one indirection too much and also yields accesses outside of the allocated memory when printing.

Instead you should have done something like this:

// Allocate enough memory to store an array of points of the expected size.
// To keep the code more readable, the resulting pointer is stored in a 
// intermediate variable.
points *theArray = malloc(neededPoints * sizeof(point));
if (theArray == NULL) {
    // do some error handling here as you apparently ran out of memory...
}

// fill the points with some data for testing
for (int k=0;k<neededPoints;k++) {
   theArray[k].x = k*10;
   theArray[k].y = k*5;
}

// Now save the pointer to the output-variable.
*outArray = theArray;

Let me also add a word of warning: You always should check whether the malloc was successful or not before using the return value. It might be that you ran out of memory and therefor won't get what you requested.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically allocating array explain

From Dev

Dynamically Allocating 2D Arrays in C

From Dev

c: issues when allocating 2d char array dynamically?

From Dev

Allocating an array of a class c++

From Dev

Reading file data to array of structures while allocating memory dynamically

From Dev

Dynamically allocating memory for changing array size starting with unknown size C++

From Dev

Allocating dynamic array within a function of a structure

From Dev

C segmentation fault when dynamically allocating 2d array

From Dev

Forward Declaring and Dynamically Allocating an Array of Pointers of that Declared Class?

From Dev

Dynamically allocating memory in a function WITHIN a function in C

From Dev

Dynamically allocating a 2D array in C

From Dev

Dynamically allocating a 2D string array

From Dev

C Programming: Reading data from a file, dynamically allocating memory, placing contents in struct array

From Dev

when allocating memory for an array dynamically (in C), what does the (int *) cast do?

From Dev

Allocating an array of a class c++

From Dev

Dynamically allocating threads in C

From Dev

Allocating memory to array of strings in c

From Dev

C - Dynamically allocating array for stdin without realloc

From Dev

Dynamically allocating array of struct from file C

From Dev

allocating a string inside a function inside a function in C

From Dev

dynamically allocating an int array inside a structure

From Dev

Dynamically Allocating Array With Datafile

From Dev

C Runtime Error: Dynamically Allocating Memory in a For Loop

From Dev

Allocating memory for array of pointers of int dynamically

From Dev

Trouble dynamically allocating memory for string array

From Dev

dynamically allocating my 2d array in c

From Dev

Allocating array of structures in function

From Dev

Dynamically allocating an array of a struct

From Dev

Allocating 2D array dynamically

Related Related

  1. 1

    Dynamically allocating array explain

  2. 2

    Dynamically Allocating 2D Arrays in C

  3. 3

    c: issues when allocating 2d char array dynamically?

  4. 4

    Allocating an array of a class c++

  5. 5

    Reading file data to array of structures while allocating memory dynamically

  6. 6

    Dynamically allocating memory for changing array size starting with unknown size C++

  7. 7

    Allocating dynamic array within a function of a structure

  8. 8

    C segmentation fault when dynamically allocating 2d array

  9. 9

    Forward Declaring and Dynamically Allocating an Array of Pointers of that Declared Class?

  10. 10

    Dynamically allocating memory in a function WITHIN a function in C

  11. 11

    Dynamically allocating a 2D array in C

  12. 12

    Dynamically allocating a 2D string array

  13. 13

    C Programming: Reading data from a file, dynamically allocating memory, placing contents in struct array

  14. 14

    when allocating memory for an array dynamically (in C), what does the (int *) cast do?

  15. 15

    Allocating an array of a class c++

  16. 16

    Dynamically allocating threads in C

  17. 17

    Allocating memory to array of strings in c

  18. 18

    C - Dynamically allocating array for stdin without realloc

  19. 19

    Dynamically allocating array of struct from file C

  20. 20

    allocating a string inside a function inside a function in C

  21. 21

    dynamically allocating an int array inside a structure

  22. 22

    Dynamically Allocating Array With Datafile

  23. 23

    C Runtime Error: Dynamically Allocating Memory in a For Loop

  24. 24

    Allocating memory for array of pointers of int dynamically

  25. 25

    Trouble dynamically allocating memory for string array

  26. 26

    dynamically allocating my 2d array in c

  27. 27

    Allocating array of structures in function

  28. 28

    Dynamically allocating an array of a struct

  29. 29

    Allocating 2D array dynamically

HotTag

Archive