Pointer alignement

Can someone help me on this

Suppose I have a pointer to a NODE object
1
2
3
NODE *ptr = new NODE();

ptr = ptr | 0x01


After this if I do, ptr->method(). It works and if I do ptr->dataMember it doesn't work.

Can someone explain me this alignment issue?
Thanks in advance.
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>

using namespace std;

class Node
{
public:
	char ch;
	int key;

	int getKey()
	{
               cout<<"inside getKey()"<<endl;
		return key;
	}

	char getChar()
	{
                cout<<"inside getChar()"<<endl;
		return ch;
	}
};

int main()
{
	Node* node;

	node = new Node();
	node->key = 10;
	node->ch = 'a';

	cout<<"Before:"<<endl;
	cout<<node->key<<endl;
	cout<<node->ch<<endl;

	cout<<node->getKey()<<endl;
	cout<<node->getChar()<<endl;

	cout<<node<<endl;
	node = (Node*)((unsigned)node | 0x01);
	cout<<node<<endl;

	cout<<"After:"<<endl;
	cout<<node->key<<endl;
	cout<<node->ch<<endl;

	cout<<node->getKey()<<endl;
	cout<<node->getChar()<<endl;

	return 0;
}


This is a sample code, I am talking about. Functions are called correctly but data members gives incorrect value.

I think its because functions are independent of class objects but data members are not? Can someone give some more insight on this?
You've asked for a new NODE to be created on the heap and you get a pointer to it back.

You then change that pointer (possibly), so you're now pointing to something else which is undefined.

You've gone on to access that uninitialised data.

Can you see that it's wrong to change the pointer in that way?

What alignment do you require?
Here for example since Node is a object with size 8 bytes(2 int). So suppose starting address of the allocated block be

0x1000 so after changing the pointer address to 0x1001. I am still pointing to the block.

So, I understand being not able to access the data members but then why am I correctly able to call the member functions? Is it because member functions are common to all the objects of a class and are independent to where the object pointer is pointing to??

Or is there any other reason?
member functions are common to all the functions, otherwise there would be a lot of wasted memory, and that's why sizeof() only returns the size of the data members
Correct. The object's address will vary for each instance of the class, but the functions are always the same code.
So all member functions are are functions that have an implicit parameter that is a pointer to the instance's data members. If the member function is non-virtual, the call to the member function can be made successfully by the compiler even if the pointer is completely bogus. What causes the crash is when the member function tries to access the instance data through that pointer.

To illustrate, try the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

class A {
  public:
      void foo() { cout << "foo called" << endl; }
      void bar() { x = 4; }
  private:
      int x;
};

int main() {
    A* a = 0;
    a->foo();
    cout << "Calling bar..." << endl;
    a->bar();
}


and then as a second test make foo virtual and note that the program crashes even before "foo called" is printed.
Topic archived. No new replies allowed.