-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex7_vector.c
102 lines (83 loc) · 2.92 KB
/
ex7_vector.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Include the system headers we need */
#include <stdlib.h>
#include <stdio.h>
/* Include our header */
#include "ex7_vector.h"
/* Define what our struct is */
struct vector_t {
size_t size;
int *data;
};
/* Utility function to handle allocation failures. In this
case we print a message and exit. */
static void allocation_failed() {
fprintf(stderr, "Out of memory.\n");
exit(1);
}
/* Bad example of how to create a new vector */
vector_t *bad_vector_new() {
/* Create the vector and a pointer to it */
vector_t *retval, v;
retval = &v;
/* Initialize attributes */
retval->size = 1;
retval->data = malloc(sizeof(int));
if (retval->data == NULL) {
allocation_failed();
}
retval->data[0] = 0;
return retval;
}
/* Create a new vector with a size (length) of 1 and set its single component to zero... the
right way */
/* TODO: uncomment the code that is preceded by // */
vector_t *vector_new() {
/* Declare what this function will return */
// vector_t *retval;
/* First, we need to allocate memory on the heap for the struct */
// retval = /* YOUR CODE HERE */
/* Check our return value to make sure we got memory */
// if (/* YOUR CODE HERE */) {
// allocation_failed();
// }
/* Now we need to initialize our data.
Since retval->data should be able to dynamically grow,
what do you need to do? */
// retval->size = /* YOUR CODE HERE */;
// retval->data = /* YOUR CODE HERE */;
/* Check the data attribute of our vector to make sure we got memory */
// if (/* YOUR CODE HERE */) {
// free(retval); //Why is this line necessary?
// allocation_failed();
// }
/* Complete the initialization by setting the single component to zero */
// /* YOUR CODE HERE */ = 0;
/* and return... */
return NULL; /* UPDATE RETURN VALUE */
}
/* Return the value at the specified location/component "loc" of the vector */
int vector_get(vector_t *v, size_t loc) {
/* If we are passed a NULL pointer for our vector, complain about it and exit. */
if(v == NULL) {
fprintf(stderr, "vector_get: passed a NULL vector.\n");
abort();
}
/* If the requested location is higher than we have allocated, return 0.
* Otherwise, return what is in the passed location.
*/
/* YOUR CODE HERE */
return 0;
}
/* Free up the memory allocated for the passed vector.
Remember, you need to free up ALL the memory that was allocated. */
void vector_delete(vector_t *v) {
/* YOUR CODE HERE */
}
/* Set a value in the vector, allocating additional memory if necessary.
If the extra memory allocation fails, call allocation_failed(). */
void vector_set(vector_t *v, size_t loc, int value) {
/* What do you need to do if the location is greater than the size we have
* allocated? Remember that unset locations should contain a value of 0.
*/
/* YOUR CODE HERE */
}