Pointers and their Application.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

1.11 - heap management

Post by avansc »

1.11 - heap management

this section i will tell you about the functions C provide for memory allocation. all of the return the void* pointer that is typeless.

1.11.1 - allocation

there are 2 primarty functions malloc() and calloc(). both functions return a void*:

Code: Select all

void *malloc(size_t requestedSize);
void *calloc(size_t requestedCount, size_t requestedSize);
both functions return a pointer to a contiguous block of memory, or NULL if there was not enough memory available.

- malloc() gets a block of size requestedSize
- calloc() gets a block of size requestedSize*requestedCount and initializes this block to 0

if you have a pointer of type T* (just generic for my examlple)

Code: Select all

T *p;
you would use:

Code: Select all

p = malloc(sizeof(T));
p = calloc(1, sizeof(T));
this is portable, also you should allways check to see if your call was successful.

Code: Select all

int *p;

if((p = malloc(sizeof(int))) == NULL)
     exit(EXIT_FAILURE);

*p = 12;

note that i use an explicit cast when i use malloc and calloc and it looks like this.

Code: Select all

p = (int*)malloc(sizeof(int));
my time is running out, but i'll do a quick example.

Code: Select all

#define SIZE 3;
int *p;

if((p = (int*)malloc(SIZE * sizeof(int))) == NULL)
     exit(EXIT_FAILURE);
now we have a pointer p* that has space for 3 integers.
there are many ways to store and get information

Code: Select all

p[0] = 10;
p[1] = 11;
p[2] = 12;
will work fine.

to be continued...


1.11.2 Memory Deallocation


note i do not always do this which is bad. but its good practice to allways deallocate memory once you have completed the task. especially if it is a big program.
if you dont deallocate it that memory can be used again.

Code: Select all

int *p;

if((p = (int*)malloc(SIZE * sizeof(int))) == NULL)
     exit(EXIT_FAILURE);

*p = 12;
free(p);
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

1.12 - Pointer Arithmatic

Post by avansc »

1.12 - Pointer Arithmatic

in this section i will describe pointer arithmetic and show you valid operations.

1.12.1 - the sum of a pointer and an integer

once you assign a pointer p to a memory block, it is important to remember that the pointer does not describe the entire block, but rather just the beginning of that block. it is important and neccisary to be able to access other object in that block. you can do that by using the following expression

p + n

where n is an integer. this expression yields a a pointer to n'th object beyond where p is currently pointing. (remember it does not hold a value, but a pointer, to get the value you still have derefferance the pointer.)

note that if you have a pointer p, you can allso use the p[n] expression to get to the nth object.

stop: remember that p[1] does not point to the first object, p[0] points to the first object.

... to be continued ...

1.12.2 - Difference of a pointer and an integer


1.12.3 - pointer comparison : <, <=, > ,>= <==, !=


1.12.4 - pointer subtraction
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Post Reply