malloc() realloc() memset() free()



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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
       vector.h //header
             

  #ifndef vECTOR_h
#  define vECTOR_h

#  include <string.h>
#  include <stdio.h>
#  include <stdlib.h>

typedef int iterator;
#define iterator_begin 0
#define iterator_end -1

typedef struct vector_struct {

    size_t e_sz;
    char e_type;

#define V_INT 1
#define V_DOUBLE 2
#define V_CHAR 3

	unsigned no_e;
	unsigned cur_cap;
	void* e_array;

}* vector_type;

#define vector_size(v) ((v)->no_e)
#define vector_capacity(v) ((v)->cur_cap)

/*** TO BE OPTIONALLY CHANGED START ***/

  /*** optionally add the definition of V_FLOAT type ***/

#define vector_int_at(v,i) (*((int*)v_at(v,i)))
#define vector_double_at(v,i) (*((double*)v_at(v,i)))
#define vector_char_at(v,i) (*((char*)v_at(v,i)))
#define vector_string(v) ((char*)v_at(v,0))

/*** TO BE OPTIONALLY CHANGED END ***/


extern inline void* check_a(void*);
extern inline void fail(const char*);
extern vector_type v_create_empty(char);
extern void v_destroy(vector_type);
extern void* v_at(vector_type, iterator);
extern void v_push_back(vector_type,void*);
extern void v_pop_back(vector_type);
extern void v_insert_n(vector_type,iterator,unsigned,void*);
extern void v_erase_range(vector_type,iterator,iterator);

#endif
      
       


==============================================================================

        vector.c


#include "vector.h"

/*
#define DEBUG
*/


inline void fail(const char* msg){
    fprintf(stderr,"\nERROR: %s\n\n",msg);
    exit(1);
}


inline void * check_a(void*p){
    if ( p == NULL )
        fail("check_a(): NULL pointer");
    return p;
}


vector_type v_create_empty(char type){
    vector_type my_v = (vector_type)check_a(malloc(sizeof(struct vector_struct)));

    my_v->e_sz = (type == V_INT ? sizeof(int) : type == V_DOUBLE ? sizeof(double) : 1);


    my_v->e_type = type;
    my_v->no_e = my_v->cur_cap = 0;
    my_v->e_array = NULL;
    return my_v;
}


void v_destroy(vector_type my_v){
    if ( my_v == NULL )  return;
    
    free((void*)my_v);
}


void* v_at(vector_type v, iterator i){
    if ( v == NULL )
    	fail("v_at() Error: no such vector");
    if ( i == iterator_end )
    	i = v->no_e -1;
    if ( i < 0 || i >= v->no_e )
    	fail("v_at() Error: no such element in this vector");


    return( (void*)(char*(v->e_array) + i* v->e_sz)) ;


}


void v_push_back(vector_type v, void* new_val){
    if ( v->no_e >= v->cur_cap ) {
    	/*** reallocate a larger array ***/
    	v->cur_cap += (v->cur_cap)? v->cur_cap : 2;

    v->e_array = realloc(v->cur_cap * v->e_sz);

      }
    /*** copy new_val in the array at index v->no_e ***/


     memcpy(v->e_array + v->no_e,new_val, sizeof new_val);
   

    (v->no_e)++;
}


void v_pop_back(vector_type v){
    if ( v == NULL )
    	fail("v_pop_back(): no such vector");
    if ( v->no_e == 0 )
    	return;
    if ( --(v->no_e) < ((v->cur_cap)>>1) ) {
    	/*** reallocate a smaller array ***/
    	(v->cur_cap) >>= 1;

    v= realloc(v,v->cur_cap);

      }
}

==============================================================================
        
        test_vector.c

#include "vector.h"
#include <stdio.h>
#include <stdlib.h>

main(){
    vector_type vi1, vd1, vc1, vc2;
    char c1;
    int i1;
    double d1;

    vi1 = v_create_empty(V_INT);
    vd1 = v_create_empty(V_DOUBLE);
    vc1 = v_create_empty(V_CHAR);
    vc2 = v_create_empty(V_CHAR);

    c1 = 'c'; v_push_back(vc1,(void*)&c1);
    c1 = 'i'; v_push_back(vc1,(void*)&c1);
    c1 = 'a'; v_push_back(vc1,(void*)&c1);
    c1 = 'o'; v_push_back(vc1,(void*)&c1);
    c1 = 0; v_push_back(vc1,(void*)&c1);

    printf("Vector vc1 now contains %d characters (capacity=%1d): %s\n",
           vector_size(vc1),vector_capacity(vc1),vector_string(vc1));         

  
}

       
Last edited on
Topic archived. No new replies allowed.