Here's my program. What it does is irrelevant. The problem is that it finishes all of its operations and then immediately crashes. Another problem is that it only crashes part of the time, performing the same static operations. There are no random numbers used or anything that would cause the operation to be so erratic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "BitSequence.cpp"
usingnamespace std;
int main(){
unsignedint * array = static_cast<unsignedint*>(calloc(2, 4));
*(array) = 42345234;
*(array + 1) = 64;
BitSequence * b = new BitSequence(array, 64, 0);
BitSequence * c = (*b).getBits(10, 5);
cout << "foo";
delete c; //Adding this delete line sometimes makes it work.
delete b; //This delete line doesnt change anything.
return 0;
}
The printout from this code is always "foo", and then a crash occurs. The weird part is, the program crashes after printing, even if I get rid of lines 14 and 15.
But it gets weirder. If I put in the first line "delete c;", the program compiles. But only sometimes. I'd say that its a 50/50 chance of crashing. Adding the "delete b;" Changes nothing.
I'm attaching the relevant BitSequence code, but I doubt that it has anything to do with the problem, because the program always prints "foo".
// say, for example, bitLengthToIntLength returns 4
data = static_cast<unsignedint*>(calloc(bitLengthToIntLength(arrayLength), 4)); // 4 members allocated
constunsignedint endArray = bitLengthToIntLength(arrayLength)-1; // endArray = 4-1 = 3
//...
*(data+endArray+1) = *(array + endArray + 1) << offset; // *(data+endArray+1) =
// data[endArray+1] = data[3 + 1] = data[4] = past the end of the buffer (data[3] is the last valid element)
A few other unrelated notes:
-) Why on earth are you using calloc? mixing new and malloc family functions isn't really a good idea. Plus you don't even seem to be using the zero-memory feature of calloc anyway.
-) Why so afraid of [brackets]? The pointer math approach makes the code much harder to follow.
I had a very long and courteous post typed out, but this website seems to log me out constantly, and so when I submitted it, it erased all of the data and wouldn't let me log in.
So long story short, thank you very much for your help. I realized my error, and will try to correct my irregular coding practices. Thank you especially for the resource on header files.
This is probably not something I would have discovered for a long time, and I really appreciate your help.
Edits:
Having said that, there are justifications for using malloc.
- Since it doesn't clear the allocated memory, it *can be* faster than new. Free *may* also faster than delete.
- The memory block can be resized with realloc.
This is probably not something I would have discovered for a long time, and I really appreciate your help.
No problem ^^. Glad I could help.
- Since it doesn't clear the allocated memory, it *can be* faster than new. Free *may* also faster than delete.
calloc is almost undoubtedly always slower than new for basic memory types because it zero's the memory after it's allocted (whereas new does not alter the memory after allocation).
You might be thinking of malloc, which just gives you an unaltered allocated buffer.
But I doubt there's any performance difference between new/delete and malloc/free for basic types.