user3692521
user3692521

Reputation: 2641

C struct pointer

I was trying to understand some code written by others. But I am not very familiar with C. Could anyone explain the following code? Thx ahead!

struct cell *ptr = (struct cell* ) malloc ( (input * 1024) * sizeof(struct cell));

Upvotes: 0

Views: 106

Answers (4)

Mauricio Trajano
Mauricio Trajano

Reputation: 2927

The function malloc is used to dynamically allocate memory, it is used when you have no prior knowledge of how large something will be beforehand. Its argument is the number of bytes you want.

Given an input, in this case input, it will give you that number of struct cells times 1024. Think of it like a matrix, where 1024 is the width and input is the height. Finally the function returns the pointer to the allocated memory block which is stored in ptr so that you can use it.

In a object oriented language it is the equivalent of creating an array of objects, the size of the "object" is the size of the cell, input would be the number of such "arrays of objects" you are creating, and 1024 is the array length.

Upvotes: 0

M.M
M.M

Reputation: 141544

This was a way of allocating memory in the 1970s which is still sometimes seen today, perhaps due to a "cargo cult" programming mentality.

The effect is to allocate a contiguous array which has 1024 * input number of elements, and each element is an (uninitialized) struct cell.

Today we would write:

struct cell *ptr = malloc( input * 1024 * sizeof *ptr );

or

struct cell *ptr = calloc( input * 1024, sizeof *ptr );

The latter will also initialize any integers in the struct to have value 0, and any char arrays to contain empty strings; and may be faster depending on the operating system.

The sizeof *ptr part is necessary because the malloc family of functions expect number of bytes instead of number of elements so we must multiply by the number of bytes per element; and if ptr points to an element, then *ptr is an element, so sizeof *ptr retrieves the size of that element.

For explanation of why the old style is no longer in favour, see here.

Upvotes: 2

Vallabh Patade
Vallabh Patade

Reputation: 5110

This code creates a pointer of type struct cell named ptr which points to a memory location- a sequence of (input*1024) number of blocks, where each block is of size sizeof(struct cell)

This is dynamic memory allocation. During runtime if there is not that much amount of free memory, it will return NULL and ptr will be pointing to NULL It always advised to check ptr for NULL value before playing with it.

As you have allocated the memory dynamically, it's your responsibility to free/reclaim it by calling free(ptr) once you are done.

Upvotes: 3

gsamaras
gsamaras

Reputation: 73366

This code dynamically allocates a number of struct cell's. The number is equal to input*1024. As a result ptr will be a 1D array of structs. Don't forget to free() it!

Also say to these others not to cast malloc().

Upvotes: 0

Related Questions