Hello,
I'm working with a metadata library for audio files. But I have a problem with some heap corruption. But the funny thing is that it happens when I initialize my pointer so it can store an array of characters.
The pointers are inside a structure which I KNOW is initialized correctly. Every pointer is set to NULL to avoid deleting twice and my destructor for the structure works excellent because all my other functions works fine except this one.
But here is a snippet of my code:
Before the if-statements I get the ID for the tag. Then I simply just calculates the size, creates the pointer array and writes the string to the pointer.
The first one with the title works excellent but the other fails. But the funny thing is, that if I initialize the array outside the if-statement or do it in the first if-statement I don't get the heap corruption. Any ideas to what is wrong?
You're allocating size-23 characters, but are writing size characters. That can't go well, now can it?
Such errors are to be expected if you mess with C strings, use goto and have no error checking. I suggest rewriting your program from scratch and not using any C libraries this time.
Okay, that is pretty embarrassing. It works now. Sorry for all the trouble for such a foolish mistake.
But I'm not agreeing that it's wrong to use goto's. I have a lot of if-statements in the complete code and by using a goto I can avoid processing all of the if-statements and therefore improve speed significantly especially when you have to use the function a lot of times. Using the goto improved my code speed by almost 5 %.
I have a lot of if-statements in the complete code and by using a goto I can avoid processing all of the if-statements and therefore improve speed significantly especially when you have to use the function a lot of times.
That could have been accomplished by an if-else cascade.
It's very unusual to find a programme like this, a metadata library for audio files, that is time critical and must squeeze an extra 5% out of running time at the expense of well-structured code.
I've seen goto used appropriately in control code for ejector seats in aircraft, and some other safety equipment where meeting a ceratin condition absolutely necessitated immediately running a line of code without taking the time to exit functions properly (which not only takes time, but carries a risk of failing in some way before getting to the critical safety code - in this case it was part of a reaction to detecting a fire).
It used to be common in C code to see gotoused as a way of enforcing a common exit routine from a section, usually involved with error-detection, but in C++ code there are far better ways of doing such things.
When I use the if-else cascade it still run through all of the else if statements when I debug the program in VC++. I don't know if that is correct it is just what the debugger does.
I know goto is not good to use in C++ but I need speed for my functions. But I believe it is fair enough to use it in my loop. All it does is it skips the rest of the if-statements and goes to the last part of my loop where some data are handled etc.