-
Notifications
You must be signed in to change notification settings - Fork 13
completed assignment #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| Drew French | ||
|
|
||
| 1) malloc() takes size_t & number of bytes of space and allocates that amount of bytes; you then need to fill the space. Calloc takes size_t, number of bytes per element, and number of elements and allocates [bytes per element] for each element. This space is filled by these elements. | ||
|
|
||
| 2) Maybe iterate through it to fill it with integers and multiply the number of integers by 4 to get the amount of bytes? | ||
|
|
||
| 3) Heap resets only when the computer is restarted; stack resets when the program ends. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you're right that the stack resets for each program, but you're not quite right about the heap! |
||
|
|
||
| 4) If it's NULL, there is no space. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right! so you should test for this to make sure it's been properly allocated. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* Drew French */ | ||
|
|
||
| /* Does stuff with | ||
| * linked lists. | ||
| */ | ||
|
|
||
| /* Did not finish; trying | ||
| * to figure out why what | ||
| * there is so far is | ||
| * getting errors. | ||
| */ | ||
|
|
||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| typedef struct node | ||
| { | ||
| int n; | ||
| struct node *next; | ||
| } nodeType; | ||
|
|
||
| void printList(nodeType *head) | ||
| { | ||
| nodeType *current = head; | ||
| while(current != NULL) | ||
| { | ||
| printf("%d\n", current->n) | ||
| current = current->next; | ||
| } | ||
| } | ||
|
|
||
| void clear(nodeType *head) | ||
| { | ||
| nodeType *current = head; | ||
| while(current != NULL) | ||
| { | ||
| free(*current); | ||
| } | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| nodeType *head = NULL; | ||
|
|
||
| head = malloc(sizeof(nodeType)); | ||
| head->n = 3; | ||
| head->next = malloc(sizeof(nodeType)); | ||
| head->next->n = 1; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these steps should have been done through the |
||
| head->next->next = malloc(sizeof(nodeType)); | ||
| head->next->next->n = 4; | ||
| head->next->next->next = malloc(sizeof(nodeType)); | ||
| head->next->next->next->n = 1; | ||
| head->next->next->next->next = malloc(sizeof(nodeType)); | ||
| head->next->next->next->next->n = 5; | ||
| head->next->next->next->next->next = malloc(sizeof(nodeType)); | ||
| head->next->next->next->next->next->n = 9; | ||
| head->next->next->next->next->next->next = NULL; | ||
|
|
||
| printList(*head); | ||
|
|
||
| clear(*head); | ||
|
|
||
| return 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the space is actually initialized with 0s in calloc!