c malloc array of struct

user2286339

So far, I have dealt a bit with pointers and structs, but I'm not sure how to allocate an array of a structure at runtime - see below.

N.B. "user_size" is initialized at runtime.

typedef struct _COORDS
{
    double x;
    double y;
    double area;
    double circumference;
    int index;
    wchar_t name[16];
} COORDS, *PCOORDS;

PCOORDS pCoords = (PCOORDS)malloc(sizeof(COORDS)* user_size);
// NULL ptr check omitted 

After that, can I just access pCoords[0] to pCoords[user_size-1] as with an ordinary array of ints?

More to the point: I don't understand how the compiler superimposes the layout of the structure on the alloc'ed memory? Does it even have to or am I overthinking this?

Jongware

The compiler does not super-impose the structure on the memory -- you tell it to do so!

An array of structures is accessed by multiplying the index of one element by its total size. pCoords[3], for example, is "at" pCoords + 3*sizeof(COORDS) in memory.

A structure member is accessed by its offset (which is calculated by the sizes of the elements before it, taking padding into account). So member x is at an offset 0 from the start of its container, pCoords plus sizeof(COORDS) times the array element index; and y is sizeof(x) after that.

Since you tell the compiler that (1) you want a contiguous block of memory with a size for user_size times the size of a single COORD, and (2) then access this through pCoords[2].y, all it has to do is multiply and add, and then read the value (literally) in that memory address. Since the type of y is double, it reads and interprets the raw bytes as a double. And usually, it gets it right.

The only problem that can arise is when you have multiple pointers to that same area of memory. That could mean that the raw bytes "at" an address may need interpreting as different types (for instance, when one pointer tells it to expect an int and another a double).

With the provisio that the valid range is acutally 0..user_size - 1, your code is fine.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

c++ malloc for array in struct

From Dev

Malloc 'ing array of struct in struct

From Dev

Malloc an array inside a struct

From

malloc for struct and pointer in C

From Dev

Trouble with struct malloc in C

From Dev

Using malloc with struct in C

From Dev

What's the default value in an array after using malloc on a struct in C

From Dev

C - dynamic array of typedef struct with in-function malloc

From Dev

malloc of array in struct passed as argument

From Dev

Malloc and memcpy struct plus array

From Dev

malloc with C struct in C++

From Dev

C, How to malloc the correct amount of space for an array of a struct inside another struct?

From Dev

C - malloc in a loop with struct with pointer

From Dev

malloc sizeof a typedef struct in C

From Dev

How to correctly malloc a struct in C

From Dev

C malloc segmentation fault struct

From Dev

Problem in C with pointers, struct in struct and malloc

From Dev

Passing malloc struct array through a function

From Dev

how to do malloc to array from struct

From Dev

Dynamic array inside struct and malloc fail

From Dev

Problem with setting the array of pointers in the struct using malloc

From Dev

how to write in a file a struct that has an array with Malloc

From Dev

malloc vs array in C

From Dev

To malloc a huge array in C

From Dev

malloc and array in C

From Dev

How do I properly allocate an array within a struct with malloc and realloc in C?

From Dev

Beginner to C - Malloc Not Required For String In Struct

From Dev

Array of struct within struct (C)

From Dev

C program malloc with array of strings