Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Topics

Recent Status Updates

View All Updates

- - - - -

malloc(); in C [Short tutorial]

linked list malloc

  • Please log in to reply
2 replies to this topic

#1 lionaneesh


    CC Regular

  • Just Joined
  • PipPipPip
  • 42 posts

Posted 08 May 2010 - 09:54 PM


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 */

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 */


printf("p = %s\n",p);
printf("words = %s\n",words);



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);
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();
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 .
char = 1 bytes.
int = 4 bytes.
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 "


No references guyz ... Only one my "brain" .. LOL..

For reporting any errors or questions contact me on :-

lionaneesh@gmail.com /* I answer all my mails ..LOL..*/
Further reading

Sizeof: sizeof
Malloc: malloc - Wikipedia, the free encyclopedia
Linked Lists : How to create Linked list using C/C++ - CodeProject

Edited by Roger, 19 February 2013 - 03:35 PM.

  • 0

#2 Ancient Dragon

Ancient Dragon

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 378 posts

Posted 09 May 2010 - 02:07 PM

The only problem I see is that in C language the return value from malloc() is not typecast.
  • 0
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#3 Dumaju


    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 15 May 2010 - 01:10 PM

Nice tutorial. My only qualm is that I can't see you showing how to free memory that you allocated, which is a pretty important concept to grasp when working with malloc.
  • 0

Also tagged with one or more of these keywords: linked list, malloc

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download