skogorev / vc_vector Goto Github PK
View Code? Open in Web Editor NEWFast simple C vector implementation
License: MIT License
Fast simple C vector implementation
License: MIT License
Example Code from your readme segfaults on vc_vector_release. Checked on Clang / OS X and GCC / Ubuntu 16.04.
Code Below:
#include <stdlib.h>
#include "vc_vector.h"
struct Item {
int val1;
int val2;
};
int main() {
const int n = 10;
// Creates an empty vector with the reserved size for the 'n' elements
// and with custom deleter 'free'. Vector will contain pointers to 'Item'
vc_vector* v = vc_vector_create(n, sizeof(struct Node*), free);
if (!v) {
return 1;
}
struct Item* item = NULL;
const int count = n + 1;
// Vector automatically increases the reserved size when 'n + 1' will be added
for (int i = 0; i < count; ++i) {
// Creating item
item = (struct Item *) malloc(sizeof(struct Item));
if (!item) {
continue;
}
item->val1 = i;
item->val2 = 0;
// Pushing to the end of the vector
if (!vc_vector_push_back(v, item)) {
// If the item was not pushed, you have to delete it
printf( "not pushed\n");
free(item);
}
}
// ...
// Calls custom deleter 'free' for all items
// and releases the vector
vc_vector_release(v);
return 0;
}
Valgrind Output:
==15217== Memcheck, a memory error detector
==15217== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==15217== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==15217== Command: ./test2
==15217==
==15217== Invalid free() / delete / delete[] / realloc()
==15217== at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15217== by 0x40086D: vc_vector_call_deleter (vc_vector.c:40)
==15217== by 0x4008B0: vc_vector_call_deleter_all (vc_vector.c:45)
==15217== by 0x400A2D: vc_vector_release (vc_vector.c:96)
==15217== by 0x4007BE: main (test2.c:44)
==15217== Address 0x52034b8 is 8 bytes inside a block of size 120 free'd
==15217== at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15217== by 0x40086D: vc_vector_call_deleter (vc_vector.c:40)
==15217== by 0x4008B0: vc_vector_call_deleter_all (vc_vector.c:45)
==15217== by 0x400A2D: vc_vector_release (vc_vector.c:96)
==15217== by 0x4007BE: main (test2.c:44)
==15217== Block was alloc'd at
==15217== at 0x4C2FD5F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15217== by 0x4007FD: vc_vector_realloc (vc_vector.c:27)
==15217== by 0x401154: vc_vector_append (vc_vector.c:279)
==15217== by 0x4011E5: vc_vector_push_back (vc_vector.c:295)
==15217== by 0x400786: main (test2.c:33)
==15217==
==15217== Invalid free() / delete / delete[] / realloc()
==15217== at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15217== by 0x400A4A: vc_vector_release (vc_vector.c:100)
==15217== by 0x4007BE: main (test2.c:44)
==15217== Address 0x52034b0 is 0 bytes inside a block of size 120 free'd
==15217== at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15217== by 0x40086D: vc_vector_call_deleter (vc_vector.c:40)
==15217== by 0x4008B0: vc_vector_call_deleter_all (vc_vector.c:45)
==15217== by 0x400A2D: vc_vector_release (vc_vector.c:96)
==15217== by 0x4007BE: main (test2.c:44)
==15217== Block was alloc'd at
==15217== at 0x4C2FD5F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15217== by 0x4007FD: vc_vector_realloc (vc_vector.c:27)
==15217== by 0x401154: vc_vector_append (vc_vector.c:279)
==15217== by 0x4011E5: vc_vector_push_back (vc_vector.c:295)
==15217== by 0x400786: main (test2.c:33)
==15217==
==15217==
==15217== HEAP SUMMARY:
==15217== in use at exit: 88 bytes in 11 blocks
==15217== total heap usage: 14 allocs, 14 frees, 328 bytes allocated
==15217==
==15217== LEAK SUMMARY:
==15217== definitely lost: 88 bytes in 11 blocks
==15217== indirectly lost: 0 bytes in 0 blocks
==15217== possibly lost: 0 bytes in 0 blocks
==15217== still reachable: 0 bytes in 0 blocks
==15217== suppressed: 0 bytes in 0 blocks
==15217== Rerun with --leak-check=full to see details of leaked memory
==15217==
==15217== For counts of detected and suppressed errors, rerun with: -v
==15217== ERROR SUMMARY: 11 errors from 2 contexts (suppressed: 0 from 0)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.