Here’s a laundry list of complaints I have about this code:
- You delete
buf
twice. This is really the only item I can see that needs actual debugging. The fact that the name of the exercise isdouble_free
is a dead giveaway to experienced coders that this is the issue.
On top of that:
-
You should either be a C coder or a C++ coder, not a C+ coder (that strange variant that doesn’t appear to have fully made the transition). Other than code meant to compile in both C and C++ environments, there’s no reason a C++ program should be using the legacy C headers and functions (such as
stdio.h
andprintf
). -
You should use
ARRAY_SIZE
in all places where you need the size of that array, otherwise you risk changing it in one place and not the other. -
Variables should be scoped as tightly as possible. In other words, get rid of the current
i
declaration and just usefor (int i = ...
. -
Neither
result
nortest
are changed, nor isbuf
used for anything, so your entire program boils down tostd::cout << "result: 0\ntest 5\n";
. -
There’s little point to putting
argv/c
in yourmain
declaration if you don’t use them, just useint main()
. -
It’s a good idea to move away from raw pointers, and start using smart pointers – these can automagically deallocate themselves when they go out of scope, leading to easier memory management.
1
solved can you guys help me to debug a simple c program with gcc and make it work? (student problems..) [closed]