Heap corruption when initializing pointer

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
					//Title found.
					if(!strncmp(ID, "©nam", 4)) {

						unsigned long s = tag_size - 24;
						//Creates array to store info.
						metadata->title = new TCHAR[s+1];
						metadata->title[s] = 0;
						metadata->sizeID[0] = s;
					
						//Reads data.
						for(unsigned long i = 0; i < tag_size; i++)
							metadata->title[i] = buffer[i+20];

						goto SKIP;
					}
					//Artist found.
					if(!strncmp(ID, "©art", 4)) {

						unsigned long s = tag_size - 24;
						//Creates array to store info.
						metadata->artist = new TCHAR[s+1]; //This line causes heap corruption.
						metadata->artist[s] = 0;
						metadata->sizeID[1] = s;
					
						//Reads data.
						for(unsigned long i = 0; i < tag_size; i++)
							metadata->artist[i] = buffer[i+20];

						goto SKIP;
					}

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?

Regards,

Simon H.A.
Ignore me; Athar is totally on it.
Last edited on
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 %.

But thanks for finding my embarrassing mistake.

Regards,

Simon H.A.
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 goto used 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.
Last edited on
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.

Regards,

Simon H.A.
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.


If this is in a loop, you could just use continue; instead.

But I agree an else-if cascade would be better.
Last edited on
Topic archived. No new replies allowed.