This sort of error is caused by code overwriting the beginning (or end) of a heap block, and corrupting the heap control structures. One way to find such a problem is to wrap each of your blocks in a "signature" (see below), and periodically check that the "signature" is valid.
Note that you can use the constructor to initialize the signature, and the destructor to check whether the signatures have changed. Your methods can check the validity of the signatures before performing any operations. You can also modify the signatures in the destructor, in order to catch accesses to freed memory.
A library of this sort is a basic tool for any C++ programmer. If you don't have such a library in your toolbox - write one!
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
This is usually sourced by writing to an array with an out of bound index.
So you must check all your allocated arrays (which may include arrays that are allocated by library functions). Starting with your own ones, search for any call to new as array; e.g.:
type arrayName = new type[size];
Then for each array check the accesses for invalid indexes (index < 0 or >= size). You can do this manually by reading your source or by guarding all accesses with assertions in debug builds:
assert(index >0 && index < arrayNameSize);
arrayName[index] = someValue;
Your posted code does not contain the allocation of most. So it can't be checked by us. But there are many candidates: boolArray and all arrays used inside the 'while (i < numHapticDevices)' loop (e.g. hapticDevices).
So itoa is really NOT "integer to ASCII" conversion, right?
I knew it was a silly question.
I suppose I can use some kind of lookup method the get from random index # to character array of alphabet.
I'll check stdio.
I have access to array of pixel data (size:512x512x8bpp) from a framegrabber which I currently assign to a 8bpp bitmap which is assigned to a GDI+ graphics object later on.
Now, I would like to convert my array of 8bpp pixel data into 24bpp (BGRA, but I have no use for Alpha channel) and assign it to my bitmap and then modify individual pixel data to different colors depending on other conditions.
When I say efficient, I want it to be done in real-time without much delay.
Although I use C# most of the time, I try to use C for small exercises -- to keep my C sharp!
Anyway... I don't recall having to pass arrays back when I was using C full-time (in the 90s), so I don't know which technique is preferred.
As I see it, there are two basic techniques as precedents in C:
0) Passing the length of the array with the array -- e.g. main ( int argc , char *argv )
1) Having a "special value" at the end of the array to indicate the end -- e.g. NULL-terminated strings.
So, if I want to pass an array of structs (four ints), which technique is preferred by the C/C++ community?
I'm actually considering door number...
2) Using a linked list instead. It just seems cleaner, though memory usage is actually increased by 25% in this case.
Again, this is just an exercise, but practicing poor technique can be worse than not practicing at all.
So, what say you? If you have to pick door 0 or door 1, which do you prefer?
Which method you use depends on what your data looks like. If you have an array of int, then there may not be a special value that can be the sentinel for the end of the array. On the other hand if you have an array of pointers to something, then maybe a NULL pointer is a good choice as a sentinel. Not always, though. If you had an sparse array of pointers, it would be conventional for the empty elements to be NULL, so you could not use that as the sentinel.
Advantages to passing the length are that you can work your way forwards or backwards through the array without having to count how many first, and you know the bounds, so you shouldn't have any Undefined Behaviour from trying to access outside the array.
With these particular structs, there is a field in which a 0 (or anything less than 1) could easily be used to indicate that the item is the terminator.
And in this particular exercise, I need to visit each item once per call, so knowing the length would only keep me from going off the end, not aid in navigation.
I'll give door 1 a try. I think that will require only that I allocate one more item and make a small change to the for loop I would otherwise use.
I pulled out my old VAX C book for reference, and all I see is passing structs in as parameters, but it seems to say it's limited to 1020 bytes. If that's true, it must apply to return values as well -- they're still on the stack, yes?
i believe the struct returning behavior is compiler dependent (OS dependent?). some will return the struct in a register, if the struct is small enough, or on the stack. some compilers are smart enough to use the caller's struct directly so as to avoid making a copy.
that struct i showed is sizeof(int *) + sizeof(int). so, 8 or 12 bytes. nothing to worry about.