[ad_1]
Here’s a laundry list of complaints I have about this code:
- You delete
buftwice. This is really the only item I can see that needs actual debugging. The fact that the name of the exercise isdouble_freeis 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.handprintf).You should use
ARRAY_SIZEin 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
ideclaration and just usefor (int i = ....Neither
resultnortestare changed, nor isbufused for anything, so your entire program boils down tostd::cout << "result: 0\ntest 5\n";.There’s little point to putting
argv/cin yourmaindeclaration 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
[ad_2]
solved can you guys help me to debug a simple c program with gcc and make it work? (student problems..) [closed]