Reputation: 85
I'm working on a math library.
create_vector create a vector of dimension n: (v1, v2, v3, ..., vn)
delete_vector free the memory.
struct Vector
{
unsigned int dimension;
double *components;
};
typedef struct Vector *vector_t;
vector_t create_vector(const unsigned int dimension)
{
if(!dimension)
return NULL;
vector_t vector = (vector_t)malloc(sizeof(struct Vector));
vector->dimension = dimension;
vector->components = (double *)calloc(dimension, sizeof(double));
return vector;
}
void delete_vector(vector_t *vector)
{
if(*vector == NULL)
return;
free((*vector)->components);
free(*vector);
*vector = NULL;
}
Main file:
int main()
{
vector_t vector1 = create_vector(3);
delete_vector(&vector1);
}
In main file, I use this two functions, but valgrind gives me these warnings. There isn't any memory leaks. How can i solve?
==6906== Invalid write of size 4
==6906== at 0x108800: create_vector (in /home/mrcoder/Scrivania/GitHub/libmath/test/testvector)
==6906== by 0x10877E: main (in /home/mrcoder/Scrivania/GitHub/libmath/test/testvector)
==6906== Address 0x4b3702c is 0 bytes after a block of size 4 alloc'd
==6906== at 0x483021B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==6906== by 0x1087DC: create_vector (in /home/mrcoder/Scrivania/GitHub/libmath/test/testvector)
==6906== by 0x10877E: main (in /home/mrcoder/Scrivania/GitHub/libmath/test/testvector)
==6906==
==6906== Invalid read of size 4
==6906== at 0x10882B: delete_vector (in /home/mrcoder/Scrivania/GitHub/libmath/test/testvector)
==6906== by 0x108790: main (in /home/mrcoder/Scrivania/GitHub/libmath/test/testvector)
==6906== Address 0x4b3702c is 0 bytes after a block of size 4 alloc'd
==6906== at 0x483021B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==6906== by 0x1087DC: create_vector (in /home/mrcoder/Scrivania/GitHub/libmath/test/testvector)
==6906== by 0x10877E: main (in /home/mrcoder/Scrivania/GitHub/libmath/test/testvector)
==6906==
==6906==
==6906== HEAP SUMMARY:
==6906== in use at exit: 0 bytes in 0 blocks
==6906== total heap usage: 2 allocs, 2 frees, 28 bytes allocated
==6906==
==6906== All heap blocks were freed -- no leaks are possible
==6906==
==6906== For counts of detected and suppressed errors, rerun with: -v
==6906== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Upvotes: 0
Views: 4785
Reputation: 27
You have vector_t
typedef
'd to be a struct Vector *
, so sizeof(vector_t)
effectively expands to sizeof(struct Vector *)
. All pointers are the same size, either 4 or 8 whenever working on modern hardware, so you're not really getting the size of the struct, but the size of a pointer to your struct.
Instead you should do malloc(sizeof(struct Vector))
to get enough space for your structure.
Upvotes: 0
Reputation: 32596
Are you sure you do not compiled with malloc(sizeof(vector_t));
rather than with malloc(sizeof(struct Vector));
?
block of size 4 alloc'd indicates you malloc only 4 bytes being the size of a pointer if you have a 32b CPU, the size of the struct is minimum 8 bytes
Upvotes: 1