Jump to content
Sign in to follow this  
Nonplussed

Class Member Pointers Acting Strangely

Recommended Posts

The class member (pMember) returns unexpected values when referenced.

 

#include <system.h>

char* pGlobal = 0x500;

class CTest
{

public:	

CTest()
{
	char* pReceipient = 0;

	char* pLocal = 0x500;


	pReceipient = (char*) &pGlobal[1]; // pReceipient = 0x501	

	pReceipient = (char*) &pLocal[1]; // pReceipient = 0x501	

	pReceipient = (char*) &pMember[1]; // pReceipient = Different values depending on size of build

	char* pDummy = pReceipient;

};



private:	

char* pMember = 0x500;

};

void main()
{
CTest Test;
}

Share this post


Link to post
Share on other sites

Wow, I just spent a week pinpointing a similar bug.

Here it is, if it sheds any light.

In a nutshell, all four methods are overwriting the first byte of the array, instead of writing into first, second, third, fourth.

 

 

class foo
{
public:

  char	*first()	{ return (char *)&data[0]; }
  char	*second()	{ return (char *)&data[1]; }
  char	*third()	{ return (char *)&data[2]; }
  char	*fourth()	{ return (char *)&data[3]; }

  char	 *getData()  { return data; }

private:

 char	 data[4];

};


void
main()
{
class foo f;

*f.first()	= 1;	// stuffs data[0]
*f.second() = 2;	// stuffs data[0], not data[1]
*f.third() = 3;	// stuffs data[0], not data[2]
*f.fourth() = 4;	// stuffs data[0], not data[3]

char	*data = f.getData();

char a = data[0];	// returns 4, instead of 1
char b = data[1];	// returns 0, instead of 2
char c = data[2];	// returns 0, instead of 3
char d = data[3];	// returns 0, instead of 4
}

 

Incidentally, if I change the function signature to:

 

   char	*first()		{ return &data[0]; }
  char	*second()	{ return &data[1]; }
  char	*third()	{ return &data[2]; }
  char	*fourth()	{ return &data[3]; }

 

I get a compiler errors:

 

 

mainloop.c(12): error: can't convert 'class foo' to 'unsigned char*'

mainloop.c(12:18): error: failed to generate expression

mainloop.c(13): error: can't convert 'class foo' to 'unsigned char*'

mainloop.c(13:19): error: failed to generate expression

mainloop.c(14): error: can't convert 'class foo' to 'unsigned char*'

mainloop.c(14:18): error: failed to generate expression

mainloop.c(15): error: can't convert 'class foo' to 'unsigned char*'

mainloop.c(15:19): error: failed to generate expression

 

 

Why its treating member 'data' as 'class foo' is also strange and may be a clue.

 

Any help appreciated.

Thanks,

Rob.

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoticons maximum are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×