Issues with pointer and freeing its associated memory
Code:
I am implementing a vector in C and am having trouble with the delete function.
1) here is my vector struct:
typedef struct {
void* elems;
int elemSize;
int logLength;
int allocLength;
VectorFreeFunction vfree;
} vector;
2) here is the function from which i call the delete function (in a test file):
static void TestInsertDelete(vector *alphabet)
{
char ch = '-';
int i;
for (i = 3; i <= VectorLength(alphabet); i += 4) // Insert dash every 4th char
VectorInsert(alphabet, &ch, i);
fprintf(stdout, "\nAfter insert dashes: ");
VectorMap(alphabet, PrintChar, stdout);
for (i = 3; i < VectorLength(alphabet); i += 3) // Delete every 4th char
VectorDelete(alphabet, i);
fprintf(stdout, "\nAfter deleting dashes: ");
VectorMap(alphabet, PrintChar, stdout);
ch = '!';
VectorInsert(alphabet, &ch, VectorLength(alphabet));
VectorDelete(alphabet, VectorLength(alphabet) - 1);
fprintf(stdout, "\nAfter adding and deleting to very end: ");
VectorMap(alphabet, PrintChar, stdout);
}
3) Finally, here is my vector delete implementation that is causing an error (an "abort (core dumped)" error, it also says that there is an invalid pointer):
//VectorDelete will use the client-supplied free function to free the
//actual memory pointed to by the elements in the vector.
void VectorDelete(vector *v, int position)
{
fprintf(stdout,"gets to delete \n");
assert (position >=0);
assert (position <= v->logLength-1);
assert (v != NULL);
void* movept = (char*) v-> elems + position * v->elemSize;
assert (movept != NULL);
free(movept);
if(v->vfree == NULL)free(movept);
else v->vfree(movept);
memmove(movept, (char*) movept + v-> elemSize, v-> logLength* v-> elemSize - position*v->
elemSize);
v->logLength--;
}
ERROR GENERATED:
*** glibc detected *** ./vector-test: free(): invalid pointer: 0x0804c00b ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7eaf7cd]
/lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb7eb2e30]
./vector-test[0x80497f3]
./vector-test[0x8048a6c]
./vector-test[0x8048c7c]
./vector-test[0x80492f9]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xdc)[0xb7e5debc]
./vector-test[0x8048601]
======= Memory map: ========
08048000-0804b000 r-xp 00000000 00:13 1354564242 /afs/ir.stanford.edu/users/l/i/lizb1/myassn3/assn-3-vector-hashset/vector-test
0804b000-0804c000 rw-p 00002000 00:13 1354564242 /afs/ir.stanford.edu/users/l/i/lizb1/myassn3/assn-3-vector-hashset/vector-test
0804c000-0806d000 rw-p 0804c000 00:00 0 [heap]
b7d00000-b7d21000 rw-p b7d00000 00:00 0
b7d21000-b7e00000 ---p b7d21000 00:00 0
b7e47000-b7e48000 rw-p b7e47000 00:00 0
b7e48000-b7f83000 r-xp 00000000 08:01 1176036 /lib/tls/i686/cmov/libc-2.5.so
b7f83000-b7f84000 r--p 0013b000 08:01 1176036 /lib/tls/i686/cmov/libc-2.5.so
b7f84000-b7f86000 rw-p 0013c000 08:01 1176036 /lib/tls/i686/cmov/libc-2.5.so
b7f86000-b7f89000 rw-p b7f86000 00:00 0
b7f89000-b7f94000 r-xp 00000000 08:01 1172800 /lib/libgcc_s.so.1
b7f94000-b7f95000 rw-p 0000a000 08:01 1172800 /lib/libgcc_s.so.1
b7f95000-b7f98000 rw-p b7f95000 00:00 0
b7f98000-b7fb1000 r-xp 00000000 08:01 1172757 /lib/ld-2.5.so
b7fb1000-b7fb3000 rw-p 00019000 08:01 1172757 /lib/ld-2.5.so
bffb2000-bffc8000 rw-p bffb2000 00:00 0 [stack]
ffffe000-fffff000 r-xp 00000000 00:00 0 [vdso]
Abort (core dumped)
pod8:~/myassn3/assn-3-vector-hashset>