开发者

class member function fails when at least two instances of class declared before

开发者 https://www.devze.com 2023-03-07 07:44 出处:网络
I have two classes, BTLeafNode and BTNonLeafNode, each of which is derived from my class BTreeNode. BTreeNode has a开发者_StackOverflow中文版 protected data member buffer which is a 1024 byte characte

I have two classes, BTLeafNode and BTNonLeafNode, each of which is derived from my class BTreeNode. BTreeNode has a开发者_StackOverflow中文版 protected data member buffer which is a 1024 byte character array. BTreeNode has a template function inserttemp which stores int-T pairs in the buffer, where T is the type that the function is called with. Each class has its own function insert which calls inserttemp. BTNonLeafNode stores int-PageId pairs (PageId is basically an int) and BTLeafNode stores int-RecordId pairs (Record Id consists of a PageId and an int) into the buffer. I haven't tested BTNonLeafNode yet, but for some reason when I there are only two instances of BTLeafNode and I call the insert function it works fine, but for any instance of BTLeafNode which was declared after at least two instances of BTLeafNode were declared, it screws up. The part of memory that should store the int in RecordId instead stores the int of the next int-RecordId pair (these two pieces of data are stored next to each other in the buffer).

I'm really confused because I don't understand why declaring an instance would mess up the function. There aren't any global variables. You don't even need to do anything with the declared instances, as long as you declare them it messes up the function.


At this point there is no code posted, so we can't exactly see what's going on, but if you have not declared any data-member as a static data-member, and each derived instance of a BTreeNode owns it's own private buffer, then I can pretty much guarantee that the problem has nothing to-do with inheritance or how many instances of the derived object you declare, but is most likely a problem with your insertion algorithm. It may be a subtle bug which does not present itself in every case, hence the reason why some insertions work, but again, since each instance object has it's own memory buffer, then the only way that an instance object could have it's memory buffer screwed up is if the algorithm in the member function that is accessing the instance-object's array has a problem.

Also you said there is a 1024-byte character array in each instance of a BTreeNode, but you're storing pairs using a template function ... are you doing some type of cast of the pair to a unsigned char* and using memcpy() to allocate the pair structure in the buffer? If you are, then there's a lot that can go wrong if you're not careful about how you increment and cast your pointers.

0

精彩评论

暂无评论...
验证码 换一张
取 消