Coder Social home page Coder Social logo

vc_vector's People

Contributors

augfab avatar skogorev avatar wcastello avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

vc_vector's Issues

Example Code Segfaults

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.