Learning to Program in C (Part 09)

preview_player
Показать описание
This is part 9 in our tutorial on learning to program in C. In this lesson we take a look at how to do dynamic memory allocation in C. We look at malloc(), calloc(), and free() and then we write a simple linked list to demonstrate how they are used.
Рекомендации по теме
Комментарии
Автор

just finished the whole playlist in one sitting, Thanks for taking the time to do this really appreciate it !

ibrahimazzouzi
Автор

Yes, thank you for posting these videos. All the source code presented in the first 8 videos seems to work just fine.

I don't see this posted already, so here's my comment. In order to get the code for video 9 to work properly, I had to do the following.

(1) LIFO was not defined for use after the 'else' in insertLifo. I inserted the following near the top of the file.
#define LIFO LifoNode

(2) The correction of (ptr!NULL) to (ptr != NULL) took place off screen just before moving to next part of source code in destroyLifeQueue, so it's on the video but difficult to catch. It won't compile without that fix.

(3) This is a runtime error. The 2nd conditional in insertLifo is to test whether the queue is completely empty. The conditional is (queue->data != NULL) but it needs to be (queue->data == NULL) in order to populate the original empty node. When the conditional is wrong, the main function retrieves element 3, but then generates a segmentation fault.

(4) I was testing with a for loop in main rather than the while loop in main. With the while loop, in order to avoid a segmentation fault after the last item was successfully retrieved, I needed to move "dptr = queue->data;" to after "queue->data = NULL;" after the 'else' in removeLifo in order to get proper termination.

That said, troubleshooting the code was instructive to me. I've used MATLAB for many years, but I've never had formal training in C, so this is a good overview.

PoqVaUSA
Автор

Jonathan sir is really an awesome teacher.... Thanks sir...

thegreatmarathahindu
Автор

(14:47) Hi Jonathan, great tutorial! One tip: I think it's confusing you're using LifoQueue in initLifoQueue and *newNode in insertLifo. This is simpler to understand:

LifoQueue queue = malloc(sizeof(LifoNode));
LifoQueue newNode = malloc(sizeof(LifoNode));

JimClermonts
Автор

Very nice tutorial. Thank you. My personal opinion is that its difficulty grew exponentially, to the point where I couldn't understand much of the exercises from the last two videos. Thank you anyway.

damonargyres
Автор

Great set of tutorials, helped clarify alot. One criticism: this last video might be easier to understand if you clarify that this is a stack.

charleswallace
Автор

I like the tutorials that you've done in this playlist! They are all great up until the second part of this one - it just seems rushed.

hspilkov
Автор

Thanks for your 9 tutorials took me a few days to go view them all. Even coded some programs right along with you and they worked exactly like yours did. Although I had more errors than you did so I had to go back fix them it worked in the end. Will you be making anymore videos about C?

kylecox
Автор

Very nice course. Thank you.

Can you recommend a good book to learn more about C? Also, I was wondering if I should just learn C ++ or do you think it best to learn C first?

klr
Автор

My list only prints the last element "tomatoes" and not the other ones. Why? Code and compiler are the same ...

OmegaSlaughter
Автор

Hello ..! Thank you for an excellent Teaching. Now i want to ask you that <Is there only 9 part about Learn to program in c ? Is there more ?> and < Will you continue to c++ or not ? > . waiting for your

johnboy
Автор

Hi jonathan, all of the videos you made on c were on awesome. I am guessing this video is the last one you made on C programming?

wicqedeyebot
Автор

Hello,
First of all, Thanks lot for your tutorials, they've helped me alot.
I wanted to ask you a quesion:
your code is not working for me, it prints just the last element.
Hope you can help me with this issue.
Thanks.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct lifonode {
void* data;
struct lifonode *next;
} LifoNode;

typedef LifoNode *LifoQueue;

LifoQueue initLifoQueue()
{
LifoQueue queue =
queue->next = NULL;
return queue;
}

void destroyLifoQueue(LifoQueue queue)
{
LifoNode *ptr = queue;
while(ptr!=NULL)
{
queue = ptr->next;
free(ptr);
ptr = queue;
}
}

bool insertLifo(LifoQueue queue, void *data)
{
bool retval = false;

{
if(queue->data!=NULL)
{
queue->data = data;
queue->next = NULL;
retval = true;
}
else
{
LifoNode* newNode =
if(newNode!=NULL)
{
newNode->data = queue->data;
newNode->next = queue->next;
queue->data = data;
queue->next = newNode;
retval = true;
}
}
}
return retval;
}

void* removeLifo(LifoQueue queue)
{
void *dptr = NULL;
if(queue!=NULL)
{
if(queue->next!=NULL)
{
LifoNode* savePtr = queue->next;
dptr = queue->data;
queue->data = queue->next->data;
queue->next = queue->next->next;
free(savePtr);
}
else
{
dptr = queue->data;
queue->data = NULL;
queue->next = NULL;
}
}
return dptr;
}


typedef struct mystuff {
char *name;
int val;
} MyStuff;

int main(int argc, char **argv)
{
LifoQueue myQueue = initLifoQueue();

MyStuff fruit1 = {"Watermellon", 1};
MyStuff fruit2 = {"Grapes", 2};
MyStuff fruit3 = {"Melon", 3};

insertLifo(myQueue, &fruit1);
insertLifo(myQueue, &fruit2);
insertLifo(myQueue, &fruit3);

printf("Lifo Queue has: \n");
MyStuff *ptr = removeLifo(myQueue);
while(ptr!=NULL)
{
printf("%s (%i)\n", ptr->name, ptr->val);
ptr = removeLifo(myQueue);
}

destroyLifoQueue(myQueue);
return 0;
}

abuali
Автор

is this the last one.. or is there more coming..??

chakri