what is dynamic memory allocation

This is a discussion on what is dynamic memory allocation within the C Programming forums, part of the General Programming Boards category; hi everyone, I have a hard time understanding structs, dynamic memory allocation, and linked list. Please explain to me what ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    23

    what is dynamic memory allocation

    hi everyone,
    I have a hard time understanding structs, dynamic memory allocation, and linked list.
    Please explain to me what is going on in the following code.

    Also the question ask to me to continue writing the function so that it reads the numbers of an input array and create a linked list whose elements are NUMBERS. It said i should dynamically allocate the memory of each NUMBER INSTANCE.

    The array used in this question is {4,9,22,45,89,100}.

    thanks in advance.

    (Note: this is not my homework.....its a test question im trying to study).



    Code:
      struct number
    {
    	int a_number;
    	struct number* next;
    };
    
    typedef struct number NUMBER;
    
    NUMBER* load(int[] input_array, int size)
    {
    NUMBER* head = NULL;
    	NUMBER* current = head;
    	int i = 0;
         size = size -1;
    	if(size>=0)   // here is the generation of the head node
    	{
    		head = (NUMBER*)malloc(sizeof(NUMBER));
    		head->a_number = input_array[i++];
    		head->next = NULL;
    	}

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Using malloc would be an example of dynamic memory allocation. In that regard, they simply want you to allocate enough memory to hold everything without allocating too much or too little.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    23
    how would i do that? because i am totally lost with this stuff.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Read your C book, if you have one. Also, google for C language tutorials. There are many of them on the net, including our forum's own.

    As with any skill, you need to practice a great deal to become skilled at it, yourself. There is a huge difference between reading up on something, and being able to use it yourself, without errors.

    It's not enough to read a student's try at programming for his assignment, and saying "Oh that's not very good". Keep on going with it, and ask yourself "How would I do it?", and then try it if it seems challenging.

    Good luck!

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    23
    the thing is im running out of time, my test is tomorrow. and i tried doing this already. this is what i came up but im getting segmentation fault error.

    Code:
    	 while (i<=size)
    	 {
              current->a_number = input_array[i++];
              current = current->next;
          }
    	return head;

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    Quote Originally Posted by dbzx View Post
    the thing is im running out of time, my test is tomorrow.
    Then you've set yourself up for failure.

    "I'm running out of time" reads as "I'm too lazy to bother learning to program, I just want a free pass".
    Programming is not like singing - you can't fake it by lipsynching. You actually have to make the time to learn. It really isn't that hard to learn how to write a linked-list if you're committed to doing it.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by iMalc View Post
    Programming is not like singing - you can't fake it by lipsynching.
    Is this akin to the story involving the movie WarGames in which actor Matthew Broderick allegedly programmed the computer in the movie to type out all of the commands for him without error?

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    i am totally lost with this stuff.
    I'm afraid what everyone else has said is true, regardless of how much people on this board help you. Nevertheless, here's a quick explanation:

    You program basically needs to request resources from the operating system. There's a number of programs running, and the operating system needs to make sure each one has enough memory, and that they don't run into each other or have access to each other's resources (unless both programs explicitly set it up so that they can share resources).

    When your program runs, it has a certain amount of space called the stack. It's already there for you, you don't need to do anything to get it. So if you need an integer, you say
    Code:
    int i = 0;
    and 4 bytes of the stack space is assigned to the name 'i'. Then you can use the name i to do your operations. Your program can do this because the system knows what an 'int' is, and it already gave you enough space.

    But let's say you need a complex structure that the OS has never heard of (like maybe... a family tree), and you don't know how big it is (what if you need to add or take away generations?) So you define a struct, that contains a person's name, their birth and death dates, and of course, pointers to their parents, and pointers to their children. If you don't understand pointers - you need to go learn about them - they're crucial.

    So you defined a struct for your custom data type, and now you use malloc to allocate additional space in memory (more than what the OS already gave you). This is done on the 'heap'. Malloc returns a pointer with enough space for your struct. Then you can assign that pointer as a child or parent of an existing person. If you need to delete them, you "free" that pointer, and that lets the OS know that you no longer need the memory, and if needs be, it can be given to another program.

    I hope that helps - but this is no substitution for having done it yourself. Programming concepts have to be learned long before a test so you have time to try it yourself and REALLY understand how it works.

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, 07:54 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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