SE250:HTTB:Pointers:sizeof, malloc and free
<html>
<image src="http://www.rajithaonline.com/SE250/httb/pointers/final/sizeof_head.png" width="490" height="92" alt="Sizeof Header Logo" /> |
</html>
Previous Page | Contents | Next Page |
The sizeof Operator
This operator determines the size of an array in terms of bytes during program compilation. When applied to the name of an array, this would return the total number of bytes in the array as an integer.
NB: Variable of type float are normally stored in 4bytes of memory, and array is defined to have 20 elements. Therefore, there are a total of 80 bytes in array.
Number of elements can also be determined by sizeOf by some tricker math: Eg.
int num[24]; sizeOf(num)/sizeOf(num[0]);
Since we saw in the labs that different machines give the size of pointers to be different, I consulted a textbook(C-How to Program, Dietel), for some values.
- Char = 1 byte
- Short = 2 bytes
- Integer = 4 bytes
- Long = 4 bytes
- Float = 4 bytes
- Double = 8 bytes
When the sizeOf operator is called on a variable name, it will return the number of bytes used to store the specific type of variable.
Malloc & Free
Since a pointer just "points" to another memory location, we often need to create an area of memory and then make our pointer "point" to this memory area. This is done using the malloc function declared in the C standard library stdlib.h.
The code below shows a simple example of using the malloc function:
int *p; p = (int*) malloc(sizeof(int));
Lets look at the above code line by line:
- In the first line, we declare a pointer "p" which points to an int.
- In the second line, we create a new area of memory in the "malloc" memory space and make our pointer "p" point to this area.
- The "malloc" function takes a single argument which is a number that represents the size of the area of memory you want to create.
- The "sizeof" operator in C is accessed similar to a function, using brackets as shown in the code, with a single argument which can be any built-in or user-defined data type. i.e. "sizeof(int)" returns the size of an int data type in C, which is usually 4 bytes in a 32 bit operating system.
- The final part of this line is the cast to an "int pointer". This is done using "(int*)". We have to do this because the malloc function returns a "void" pointer - a pointer which points to an object of any type. This type of pointer cannot be deferenced, hence we have to cast it to a pointer of type "pointer to int" to be able to use our pointer.
There are also other variations of the "malloc" function declared in the C library stdlib.h:
It is important to free an area of memory after you are done using it. This is so that the malloc (also calloc and realloc) function knows which areas of memory are used and which are not. The code for freeing an area of memory previous allocated by malloc is very easy:
int *p; p = (int*)malloc(sizeof(int)); /* Use the pointer in some way. And when finished, use the line below. */ free( p );
Previous Page | Contents | Next Page |