Note: This tutorial uses pointers pretty heavily. If don't understand pointers, please read this tutorial before you go on.
This has been bugging me for a little while, but I figured it out. I will share my knowledge to the rest of the world! If you need to take data from the user that could be any length, you could define a really big array like so:
int array[100000];
There are several problems with this. No matter how big the array is, the user could still have more input. If the user doesn't have that much input, you have wasted memory.
When you are using malloc(), realloc() and free() you need the following header file:
#include <stdlib.h>
First, I will show you how to allocate memory for a pointer. You can declare a pointer like so:
int *pointer;
The pointer can point to any location at first. You should always make it point to something or you can allocate some memory that your pointer will point to. To do this, you need to use the malloc() function. Use it like so:
pointer=malloc(2*sizeof(int));
malloc() returns a void pointer and takes an argument of how many bytes to allocate. Because pointer points to an integer, we use the 2*sizeof(int). Using malloc like the above is similar to doing this:
int array[2];
Error checking:
If the operating system can't allocate more memory for your program, malloc will fail and return a NULL value. It's always a good idea to make sure malloc is successful:
pointer=malloc(1*sizeof(*pointer)); if (pointer==NULL) { printf("Error allocating memory!\n"); //print an error message return 1; //return with failure }
Now I will show you how to use realloc(). You use realloc after you have used malloc to give a pointer more or less memory. Let's say you want to give a pointer 5 integers of memory. The code should look like this:
int *temp = realloc(pointer, 5*sizeof(int)); if ( temp != NULL ) //realloc was successful { pointer = temp; } else //there was an error { free(pointer); printf("Error allocating memory!\n"); return 1; }
This is just like malloc, except realloc takes two arguments. The first argument is the pointer you want to copy the data from. The above code copies pointer to temp, then copies temp back to pointer if everything goes correctly. You may have noticed a new function though, and that is free().
Free is used to free the memory you have allocated with malloc or realloc. All memory that you allocate should be freed when you are done using it. Free takes a pointer as an argument like so:
free(pointer);
Here is an example program that makes use of a dynamic array. Everything you need to know is in the comments.
#include <stdio.h> #include <stdlib.h> /* This program takes input and outputs everything backwards */ int main() { int *data,*temp; data=malloc(sizeof(int)); int c; /* c is the current character */ int i; /* i is the counter */ for (i=0;;i++) { c=getchar(); /* put input character into c */ if (c==EOF) /* break from the loop on end of file */ break; data[i]=c; /* put the character into the data array */ temp=realloc(data,(i+2)*sizeof(int)); /* give the pointer some memory */ if ( temp != NULL ) { data=temp; } else { free(data); printf("Error allocating memory!\n"); return 1; } } /* Output data backwards one character at a time */ for (i--;i>=0;i--) putchar(data[i]); /* Free the pointer */ free(data); /* Return success */ return 0; }
So that's it. Any suggestions for improvement are welcome. +Reputation is very much appreciated.
Edited by Roger, 29 September 2014 - 04:43 PM.
Improved wording