diff --git a/assignment9.txt b/assignment9.txt new file mode 100644 index 0000000..024ae7e --- /dev/null +++ b/assignment9.txt @@ -0,0 +1,10 @@ +Mariposa Lee + +1. malloc() allocates a chunk of memory, returning a pointer. calloc() allocates memory for an array and sets the elements to zero. malloc() requires one parameter (specified number of bytes) and calloc() requires two parameters (the size of the number of elements multiplied by the size of the element). If successful, both malloc() and calloc() return a pointer to the allocated memory. + +2. If the memory was already allocated, I don't think there is a way to determine its size. However, if you're determining the size of memory to allocate, calloc() multiplies size and elements to allocate the portion of memory. + + +3. Memory that is stored in stack is managed automatically, which means memory is allocated and freed without the user having to do anything. However, memory stored in heap using dynamic memory allocation requires the user to manually allocate memory and free it. + +4. You have to test the return value from malloc to make sure it does not return NULL. That would show there was a memory leak. When a memory leak happens, you lose the pointer to the memory-allocated region and it becomes impossible to free memory. This is a problem because the heap can hold limited space. diff --git a/linkedlist.c b/linkedlist.c new file mode 100644 index 0000000..3dd34c9 --- /dev/null +++ b/linkedlist.c @@ -0,0 +1,46 @@ +/*Mariposa Lee, creates a node structure and implements various functions. + Incomplete, most is done through research.*/ +#include +#include +#include + +typedef struct node { + int data; + int cu; + struct node *next; +} node; /*declares node struct*/ + +struct node *head = NULL; /*setting heads*/ +struct node *current = NULL; + +void ins(int cu, int data) { + struct node *link = (struct node*)malloc(sizeof(struct node)); + link -> cu= link; + link-> data= data; + link->next = head; + head = link;} /*inserts and creates link, points at new nodes. Did it as a function so it would be easier in main()*/ + + +void printList() +{ + struct node *ptr = head; + printf("\n[ "); + while(ptr != NULL){ + { + printf("(%d,%d)", ptr->cu, ptr->data); + ptr= ptr->next; + } + printf("]"); + } + +}/*an attempted printlist*/ + +int main () { + +printList(); +int q, w; +ins(1, 5); +ins(2,6); + +return 0; +}