C Programming Tutorial # 33 - malloc() and free() - Dynamic Memory Allocation - Part 1 [HD]

Loading...

Sign in or sign up now!
Alert icon
Upgrade to the latest Flash Player for improved playback performance. Upgrade now or more info.
13,911
Loading...
Alert icon
Sign in or sign up now!
Alert icon

Uploaded by on Apr 21, 2010

Visit http://www.CPlusPlusTutor.com
In this tutorial we learn about dynamic memory allocation using malloc(). We also learn about freeing up the dynamically allocated memory. We also see why it is not a good idea to create long local variables since they are created on stack.

This tutorial is part of the following series:
http://www.youtube.com/view_play_list?p=CB9F975ECF01953C

  • likes, 3 dislikes

Link to this comment:

Share to:

Uploader Comments (Learnorama)

  • Could you use malloc to allocate memory for a "fixed number" of bytes that is "specified" by the user? Lets say, you're storing 100 different types of words. Instead of creating a character array such as:

    char pWords[10000];

    which in this video, you stated is bad, since it might overwrite a different program's stack (which may result in a crash).

    Hence, could you do malloc(strlen(charactervariabl­earrayhereorsomething))? Or it doesn't really matter how much memory you allocate at all?

  • @itsdannyftw Yes, you can use malloc to allocate any number of bytes. Just specify the number. And this number can be input from the user, or it could be the length of a string, whatever.

see all

All Comments (16)

Sign In or Sign Up now to post a comment!
  • when i do it in dev-c++ the compiler declare an error (invalid conversion ffrom ' void*' to 'int*'

    please can you help me

  • good tutorial. i was worried because they complain about part 1 not being here.

  • @pithikoulis The newest C standard allows this, I believe; arrays on the stack without a constant length used to be illegal and so many people heard it and think it's wrong

  • Thank you for the tutorial. I was wondering what is this stack and why would i need to ever use malloc.

  • His voice gets increasingly loud xD

    Very good tutorial btw, for learning malloc(), free() etc.

  • your voice reminds me of a prankcaller in @OwnagePranks :D

  • @lepeanut

    What's your point? It's still an industry standard.

  • Where is part #2? -___-

  • @pithikoulis maybe in declaration but certainly not in operations , its down to the compiler

  • @amino0o i just tryied that code and it works..

Loading...

Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more