better zeroing of realloced space

master
Dvir Volk 9 years ago
parent 24f72b897d
commit 7b685c95e6

@ -3,7 +3,7 @@ ifndef RM_INCLUDE_DIR
RM_INCLUDE_DIR=../ RM_INCLUDE_DIR=../
endif endif
CFLAGS = -g -fPIC -lc -lm -O3 -std=gnu99 -I$(RM_INCLUDE_DIR) CFLAGS = -g -fPIC -lc -lm -O3 -std=gnu99 -I$(RM_INCLUDE_DIR) -Wall
CC=gcc CC=gcc
OBJS=util.o strings.o sds.o vector.o OBJS=util.o strings.o sds.o vector.o
@ -17,7 +17,7 @@ librmutil.a: $(OBJS)
ar rcs $@ $^ ar rcs $@ $^
test_vector: test_vector.o vector.o test_vector: test_vector.o vector.o
$(CC) -o test_vector vector.o test_vector.o -lc -O0 $(CC) -Wall -o test_vector vector.o test_vector.o -lc -O0
@(sh -c ./test_vector) @(sh -c ./test_vector)

@ -45,7 +45,7 @@ int Vector_Resize(Vector *v, int newcap) {
v->cap = newcap; v->cap = newcap;
v->data = realloc(v->data, v->cap*v->elemSize); v->data = realloc(v->data, v->cap*v->elemSize);
int offset = v->top*v->elemSize; int offset = oldcap*v->elemSize;
// put all zeros at the newly realloc'd part of the vector // put all zeros at the newly realloc'd part of the vector
memset(v->data + offset, 0, v->cap*v->elemSize - offset); memset(v->data + offset, 0, v->cap*v->elemSize - offset);
return v->cap; return v->cap;

Loading…
Cancel
Save