push_back(this) pushes wrong pointer onto vector - c++

I have a vector of UnderlyingClass pointers stored in another object, and inside a method in UnderlyingClass I want to add the "this" pointer to the end of that vector. When I look at the contents of the vector immediately after the push_back call, the wrong pointer is in there. What could be going wrong?
cout << "this: " << this << endl;
aTextBox.callbacks.push_back(this);
cout << "size is " << aTextBox.callbacks.size() << endl;
cout << "size-1: " << aTextBox.callbacks[aTextBox.callbacks.size()-1] << endl;
cout << "back: " << aTextBox.callbacks.back() << endl;
cout << "0: " << aTextBox.callbacks[0] << endl;
cout << "this: " << this << endl;
cout << "text box ptr: " << &aTextBox << endl;
cout << "text box callbacks ptr: " << &(aTextBox.callbacks) << endl;
Here's the output:
this: 0x11038f70
size is 1
size-1: 0x11038fa8
back: 0x11038fa8
0: 0x11038fa8
this: 0x11038f70
text box ptr: 0x11039070
text box callbacks ptr: 0x11039098
By the way, callbacks is a vector of WebCallback pointers, and UnderlyingClass implements WebCallback:
std::vector<WebCallback*> callbacks;
class UnderlyingClass
:public WebCallback
Copied from comments: (see Answer below)
output:
this: 0x6359f70
size is 1
size-1: 0x6359fa8
back: 0x6359fa8
0: 0x6359fa8
this: 0x6359f70
WebCallback This: 0x6359fa8
text box ptr: 0x635a070
text box callbacks ptr: 0x635a098
okay, so that explains why the pointers don't match up.
My real question, then, is this:
how do I get the correct version of a method to be called? Specifically, WebCallback stipulates that a function onWebCommand() be implemented, and right now callbacks[0]->onWebCommand() is not causing the onWebCommand() that I wrote in UnderlyingClass to be executed.

This can happen with multiple inheritance, if your layout looks like this:
class UnderlyingBase {
char d[56];
};
class UnderlyingClass
:public UnderlyingBase,
public WebCallback {
};
Then the layout can be like this, for each object involved. The last one is the complete object containing the first two ones as base-class sub-objects, and that you take the pointer of, and which will be converted to WebCallback*.
[UnderlyingBase]
> char[56]: 56 bytes, offset 0x0
[WebCallback]
> unknown: x bytes, offset 0x0
[UnderlyingClass]
> [UnderlyingBase]: 56 bytes (0x38 hex), offset 0x0
> [WebCallback]: x bytes, offset 0x38
Now since your vector contains WebCallback*, the compiler adjusts the pointer to point to the WebCallback sub-object, while when it would point to UnderlyingClass or UnderlyingBase, it would start 0x38 (56) bytes earlier.

Add this to your print out:
cout << "this: " << this << endl;
cout << "WebCallback This: " << dynamic_cast<WebCallback*>(this) << endl;
I bet this is what you are looking for.

Related

What is the relationship between an array and its address?

The following code:
#include<iostream>
int main (void) {
int lista[5] = {0,1,2,3,4};
std::cout << lista << std::endl;
std::cout << &lista << std::endl;
std::cout << lista+1 << std::endl;
std::cout << &lista+1 << std::endl;
std::cout << lista+2 << std::endl;
std::cout << &lista+2 << std::endl;
std::cout << lista+3 << std::endl;
std::cout << &lista+3 << std::endl;
return (0);
}
Outputs:
0x22ff20
0x22ff20
0x22ff24
0x22ff34
0x22ff28
0x22ff48
0x22ff2c
0x22ff5c
I understood that an array is another form to express a pointer, but we cannot change its address to point anywhere else after declaration. I also understood that an array has its value as the first position in memory. Therefore, 0x22ff20 in this example is the location of the array's starting position and the first variable is stored there.
What I did not understand is: why the other variables are not stored in sequence with the array address? I mean, why lista+1 is different from &lista+1. Should not they be the same?
In pointer arithmetic, types matter.
It's true that the value is the same for both lista and &lista, their types are different: lista (in the expression used in cout call) has type int* whereas &lista has type int (*)[5].
So when you add 1 to lista, it points to the "next" int. But &lista + 1 points to the location after 5 int's (which may not be a valid).
Answering the question as asked:
std::cout << &lista+1 << std::endl;
In this code you take the address of array lista and add 1 to obtained answer. Given the sizeof of the array is sizeof(int) * 5, which means when you increment a pointer to it by 1 you add sizeof(int) * 5 to the pointer address, you end up with a number you see.

strange behavior by delete function (mixed C and C++)

I'm debugging a program where I found some data being changed where they shouldn't. I traced the program using gdb and I found the target data is changed in a delete function of some other data!
At first I figured there was some memory overlapping between both areas, but then I checked the start and end addresses of both areas and they do not overlap! that only leaves the delete line!
this is the function where this happens, the data that shouldn't change is freemap and the data being freed is synthops:
void BasicBlock::free() {
cout << "freemap 2 : " << this->mfnlo_loc.chunk->freemap[2] << "\n";
cout << "freemap 59 : " << this->mfnlo_loc.chunk->freemap[59] << "\n";
cout << "freemap : " << &(this->mfnlo_loc.chunk->freemap) << "\t" << sizeof(this->mfnlo_loc.chunk->freemap)+&(this->mfnlo_loc.chunk->freemap) << "\n";
cout << "synthops : " << synthops << "\t" << synthops+sizeof(uopimpl_func_t)*count << "\n";
if (synthops)
{
delete[] synthops;
}
cout << "freemap 2 : " << (this->mfnlo_loc.chunk->freemap[2]) << "\n";
cout << "freemap 59 : " << this->mfnlo_loc.chunk->freemap[59] << "\n";
synthops = NULL;
::free(this);
}
the output is like this:
freemap 2 : 1
freemap 59 : 1
freemap : 0x3319a50 0x3319a90
synthops : 0x3319d50 0x331acd0
freemap 2 : 0
freemap 59 : 0
It is shown that freemap changes after the delete line, It also shows that they both don't overlap in memory.
synthops is allocated in another function like this:
bb.synthops = new uopimpl_func_t[bb.count];
why does this happen? the code is a mix of C and C++ which means there is a mix of new and malloc (but used consistently, no delete with malloc for example). is that the reason for this? or is it something else?
My psychic debugging skills tell me that you didn't follow the rule of three for BasicBlock, specifically you omitted the copy constructor. Then you (shallow) copied that object (and specifically the synthops member), and that then resulted in double deletion at which point all bets are off.

Why value of object's attribute returns zero after adding its pointer to a map in c++

So I have this:
//sons is an attribute of the object node that is a vector<Node*> that is initialized before
map<string,Node*> nodes;
string node_id = "node";
string son_id = "son";
Node *node = new Node(node_id, matrix, son_id, Prim);
cout << "Before " << node << endl;
cout << "Value of sons before map: " << node->sons[0] << endl;
nodes[node_id] = node;
cout << "After: " << nodes.find(node_id)->second << endl;
cout << "Value of sons after map: " << nodes.find(node_id)->second->sons[0];
I'm getting this output (with varying memory positions from execution to execution):
Before: 0x9dfdda8
Value of sons before map: 0xbff1a774 // consistant with memory position with created obj
After: 0x9dfdda8
Value of sons after map: 0
Why is this happening and how can I fix it?! I've been searching for the solution and trying to figure this out for 4 hours now...
cout << "After: " << nodes.find(root_id)->second << endl;
cout << "Value of sons after map: " << nos.find(root_id)->second->sons[0];
Why is the second line refering to nos and the first one nodes? Is it just a typo?
If those are really different objects, that might explain why you see inconsisten results
You are adding node to the map with the key node_id and then looking it up later with root_id and not even checking if it found one, so you're probably getting some undefined behaviour by accessing end(nodes). You need to access the map with the same key to get the same object.
Also you appear to have some confusion with your variables, using no and nos when you are apparently needing to access either node or the map's variable.

Weird Pointer Address for Individual Struct Data Member

I observe some weird behavior today , the code is as follow :
The Code :
#include <iostream>
struct text
{
char c;
};
int main(void)
{
text experim = {'b'};
char * Cptr = &(experim.c);
std::cout << "The Value \t: " << *Cptr << std::endl ;
std::cout << "The Address \t: " << Cptr << std::endl ; //Print weird stuff
std::cout << "\n\n";
*Cptr = 'z'; //Attempt to change the value
std::cout << "The New Value \t: " << *Cptr <<std::endl ;
std::cout << "The Address \t: " << Cptr << std::endl ; //Weird address again
return 0;
}
The Question :
1.) The only question I have is why cout theAddress for the above code would come out some weird value ?
2.)Why I can still change the value of the member c by dereferenncing the pointer which has weird address ?
Thank you.
Consider fixing the code like this:
std::cout << "The Address \t: " << (void *)Cptr << std::endl ;
There's a std::ostream& operator<< (std::ostream& out, const char* s ); that takes a char* so you have to cast to void* to print an address, not a string it "points" to
I think the "weird" stuff shows up because cout thinks it's a cstring, i.e. a 0-terminated character array, so it doesn't print the address as you expected. And since your "string" isn't 0-terminated, all it can do is walk the memory until it encounters a 0. To sum it up, you're not actually printing the address.
Why I can still change the value of the member c by dereferenncing the
pointer which has weird address
The address isn't weird, as explained above. In your code Cptr points to a valid memory location and you can do pretty much anything you want with it.

C++ How to store element in deque without copying

I want to store pointers in a deque that looks like this:
deque<ofImage *> pointerDeque;
void testApp::iTakeAPointer(ofImage * &pointer) {
cout << "iTakeAPointer " << &pointer << endl;
pointerDeque.push_back(pointer);
cout << "pointerDeque.back() " << pointerDeque.back() << endl;
}
When I run it I get (function is called on keypress):
keyPressed 0xbffff240
iTakeAPointer 0xbffff240
pointerDeque.back() 0x9f2e08
Because deque.push_back() creates a copy. My question is: how do I get the reference, in this case 0xbffff240, in the deque?
EDIT:
Thanks. Sometimes you don't see the wood for the trees.
Your diagnostics are incorrect. This is printing the address of the pointer, not the address the pointer holds (which is what you want):
cout << "iTakeAPointer " << &pointer << endl;
&pointer is of type ofImage**. Just use pointer:
cout << "iTakeAPointer " << pointer << endl;
Note, it must be the case that the ofImage instances contained in pointerDeque exist for its lifetime. If pointerDeque now owns the ofImage instances then consider using std::unique_ptr. If it does not own it, consider using std::shared_ptr (or boost::shared_ptr).
cout << "iTakeAPointer " << pointer << endl;
Print the pointer itself, and not its address!
void testApp::iTakeAPointer(ofImage * &pointer) {
change to
void testApp::iTakeAPointer(ofImage * pointer) { // remove ref