Reputation: 409
I think I have some memory leak here.
Maybe someone can show me the right way how to handle leaks in situations like this.
#include <stdio.h>
#include <stdlib.h>
struct adresse {
char *name;
int nummer;
};
int main() {
int size = 2;
struct adresse *a = (struct adresse *) malloc(sizeof(struct adresse) * size);
for (int i = 0; i < size; i++) {
a[i].name = "Testname";
a[i].nummer = 123;
}
for (int i = 0; i < size; i++) {
printf("%s, %d\n", a[i].name, a[i].nummer);
}
free(a);
return 0;
}
Upvotes: 1
Views: 809
Reputation: 84579
I think I have some memory leak here. Maybe someone can show me the right way how to handle leaks in situations like this.
In any code you write that dynamically allocates memory, you have 2 responsibilities regarding any block of memory allocated: (1) always preserve a pointer to the starting address for the block of memory so, (2) it can be freed when it is no longer needed.
If you allocate it, track it, and free
it when it is no longer needed. If you didn't allocate it, you can't free
it. If you have already freed it, you can't free
it again.
" if the argument does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined."
C11 §7.22.3.3 The free function [p2] (n1570 draft)
The only thing you allocate is a
, you then later free a
-- you don't have a memory leak.
Example Memory Use/Error Check
You can easily confirm whether you have a memory leak or not by using a memory usage/error checking program (such as valgrind
on Linux -- there are similar programs for each OS). They are simple to use, just run your program through it:
$ valgrind ./bin/leak
==23745== Memcheck, a memory error detector
==23745== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==23745== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==23745== Command: ./bin/leak
==23745==
Testname, 123
Testname, 123
==23745==
==23745== HEAP SUMMARY:
==23745== in use at exit: 0 bytes in 0 blocks
==23745== total heap usage: 1 allocs, 1 frees, 32 bytes allocated
==23745==
==23745== All heap blocks were freed -- no leaks are possible
==23745==
==23745== For counts of detected and suppressed errors, rerun with: -v
==23745== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Always confirm that you have freed all memory you have allocated and that there are no memory errors.
(note, depending on your OS and it's implementation of valgrind
, it may show memory allocated by the system for your process still in use at program exit. Not all implementations of valgrind
provide proper suppression files to mask all memory allocated by the OS for your process and not you. As long as you confirm that the memory you allocate has been freed -- you have done your job.)
As you can see from the above valgrind
output, you have no errors and all memory allocated has been freed. (good job!)
Upvotes: 3
Reputation: 156
You should malloc the Testname string as well. It makes it a little more complicated to free everything because you'll need to free all the Testname strings first, and then free the overall struct memory.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct adresse {
char *name;
int nummer;
};
int main() {
int size = 2;
struct adresse *a = (struct adresse *) malloc(sizeof(struct adresse) * size);
for (int i = 0; i < size; i++) {
char *str = "Testname";
size_t len = strlen(str);
a[i].name = malloc(len+1);
memset(a[i].name, 0, len+1);
memcpy(a[i].name, str, len);
a[i].nummer = 123;
}
for (int i = 0; i < size; i++) {
printf("%s, %d\n", a[i].name, a[i].nummer);
free(a[i].name);
}
free(a);
return 0;
}
Upvotes: -1