Dynamic memory allocation.

This is a discussion on Dynamic memory allocation. within the C Programming forums, part of the General Programming Boards category; Hello I want to learn dynamic memory allocation and linked lists. So I started with dynamic memory allocation. I tried ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Dynamic memory allocation.

    Hello

    I want to learn dynamic memory allocation and linked lists. So I started with dynamic memory allocation. I tried to look for example programs but as new to this topic I did'nt understand the programs that I found and I'am not keen on blind copying. I would appriciate it if someone can put an example of a simple and easy to understand program that actually does something practicle and use dynamic memory allocation to do it.

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are plenty of posts on the topic. Just search for said topic. In short:

    1 - Pointers must point at something to be used.
    2 - If you don't have a variable to point at, allocate space dynamically.
    ... 2a - malloc will allocate a number of bytes.
    ... 2b - calloc does the same thing, except it allocates a number of blocks, each a specificed size.
    ... 2c - realloc is used to "resize" what the memory you've allocated by 2a or 2b.
    3 - free what you allocate when you're done. Always.

    That's pretty much it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Jaguar
    Join Date
    Sep 2006
    Posts
    12
    Hey,

    Linked lists are little bit trickier for a beginner. Try to get a knowledge about pointers. Then it'll be easier than you think.
    Dynamic memory allocation is nothing but just allocatin memory for a pointer.
    This is the simple example for dynamic memory allocation.

    int *pA;
    pA = malloc( sizeof(int) );

    The first line creates an integer pointer and pointed nowhere. So you can't use it for storing data etc. The second line allocates memory for the integer pointer.
    Just read the man page for malloc.

    Good luck.

    Jag

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Quote Originally Posted by jaguar23
    Dynamic memory allocation is nothing but just allocatin memory for a pointer.
    This remark is incorrect. Memory allocation rather allocates a designated size of memory and then returns you a pointer to that memory (aka a way to access it.). It does not allocate memory for a pointer.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 04:59 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21