0

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));
2
  • 4
    It is a basic dynamic allocation of an array of struct cell values, and the size of the array is input * 1024 element. The cast is not necessary in C; it is necessary if you were misguided enough to be using malloc() in C++. The value in ptr should be checked before it is used just in case the allocation failed (in which case, ptr is null). Commented Nov 9, 2014 at 22:38
  • Do not... Um. Tell them not to cast the result of malloc(). Commented Nov 9, 2014 at 23:02

4 Answers 4

3

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.

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

Comments

2

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.

Comments

0

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().

Comments

0

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.

2 Comments

There is truth and confusion in this answer. The first two sentences are OK. The discussion of kilobytes is confused, I think. The purpose here seems to be allocate an array of cells, and if the number entered in input is (for sake of example) 8, then 8192 struct cell values will be allocated in the array. In an OO language, this would be equivalent to creating an 'array of an object type', not just 'a new object', and input is not the number of objects, but 1 / 1024 of the number of objects in the array.
@JonathanLeffler you are right, the 1024 threw me off, will update my answer

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.