Introduction
Hey! Guyz.. Welcome to my very short tutorial explaining basic uses of "malloc" function in C language.
[NOTE : You need simple understanding about pointers to understand this tutorial]
Hey guyz see by reading this tutorial you'll not be a malloc expert ..
This tutorial is only for the very Basics..
So what are we waiting for lets get started..
About Malloc
Malloc() is a very powerfull function in C language. It is used in almost every powerful program or the any program which deals with pointers.
According to my understanding the best way to learn something in C is by example code so:-
#include<stdio.h> /* For standard input output */
#include<malloc.h> /* You need this if on UNIX or LINUX */
#include<string.h>
int main()
{
char words[]={"Hey this is a simple tutorial on malloc\n"};
char *p; /* char pointer for use with malloc() */
p = (char *)malloc(sizeof(words)); /* Explained in the following text */
strcpy(p,words);
printf("p = %s\n",p);
printf("words = %s\n",words);
return(0);
}
[mark=code]The Code[/mark]
Explanation:-
lines 1-3:-
include files needed
lines 7-8
Declarations already explained in comments.
line 10 /* the real stuff */Here come malloc():-
*************************************************************
first something about malloc :-
The prototype of malloc is as follows:-
void *malloc(size_t size);
malloc();
takes one argument as an input that is number of bytes to be "allocated" in memory as simple as that.
But hey what is that weird looking statement before malloc(); ..
That is called cast.
If you have read about cast in C you would be having no problem in understanding this .
But if not read about this now "Hey but after reading this tutorial".
**************************************************************
Something about sizeof();
prototype:-
sizeof(data);
1. You must be thinking what is that sizeof(words) in the "arg" of malloc.
2. The sizeof() takes input of data and returns an integer value that is number of bytes that data is .
eg:-
char = 1 bytes.
int = 4 bytes.
etc.
3. In this case it is the sizeof(words) here words is an array and the number of characters in 40 and now how many bytes we need
40 * 1;
because character is 1 byte and words contains 40 characters.
4. sizeof() ; function says it all . It means size of the "data" in this case "words".
You must be thinking where he is getting us with all this stuff .. but these were important.
***************************************************************
Ok now let's get to the output of the program.
Output :-
p = Hey this is a simple tutorial on malloc.
words = Hey this is a simple tutorial on malloc.
As expected.
Yeah! Guys you'll be thinking that what is the use of this function.
As you get into some experienced programming you'll get answer of that ..
as I said this function is very powerful and is very advanced it is basically used in linked lists and and some "structs I guess "
But i'd not touching that topic ..
Now you all have simple understanding of malloc().. "Say Yess "
References
No references guyz ... Only one my "brain" .. LOL..
[mark=ref]Contact me[/mark]
For reporting any errors or questions contact me on :-
SNIP
Further reading
Sizeof: http://www.ncl.ucar.edu/Document/Functions/Built-in/sizeof.shtml
Malloc: http://en.wikipedia.org/wiki/Malloc
Linked Lists : http://www.codeproject.com/KB/cpp/linked_list.aspx