Jump to content


Photo

Class Member Pointers Acting Strangely


1 reply to this topic

#1 Nonplussed

Nonplussed

    Newbrie

  • EstablishedMember
  • 5 posts

Posted 18 March 2011 - 12:45 PM

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

#include <system.h>

char* pGlobal = 0x500;

class CTest
{
	
public&#58;	

	CTest&#40;&#41;
	{
		char* pReceipient = 0;
			
		char* pLocal = 0x500;
				
			
		pReceipient = &#40;char*&#41; &pGlobal&#91;1&#93;; // pReceipient = 0x501	
		
		pReceipient = &#40;char*&#41; &pLocal&#91;1&#93;; // pReceipient = 0x501	
		
		pReceipient = &#40;char*&#41; &pMember&#91;1&#93;; // pReceipient = Different values depending on size of build
		
		char* pDummy = pReceipient;
			
	};
	
	
	
private&#58;	
		
	char* pMember = 0x500;
	
};

void main&#40;&#41;
{
	CTest Test;
}


#2 rbairos

rbairos

    Newbrie

  • EstablishedMember
  • 5 posts

Posted 14 April 2011 - 01:34 AM

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&#58;

   char	*first&#40;&#41;	{ return &#40;char *&#41;&data&#91;0&#93;; }
   char	*second&#40;&#41;	{ return &#40;char *&#41;&data&#91;1&#93;; }
   char	*third&#40;&#41;	{ return &#40;char *&#41;&data&#91;2&#93;; }
   char	*fourth&#40;&#41;	{ return &#40;char *&#41;&data&#91;3&#93;; }

   char	 *getData&#40;&#41;  { return data; }
				
private&#58;

  char	 data&#91;4&#93;;

};


void
main&#40;&#41;
{
	class foo f;

	*f.first&#40;&#41;	= 1;	// stuffs data&#91;0&#93;
	*f.second&#40;&#41; = 2;	// stuffs data&#91;0&#93;, not data&#91;1&#93;
	*f.third&#40;&#41; = 3;	// stuffs data&#91;0&#93;, not data&#91;2&#93;
	*f.fourth&#40;&#41; = 4;	// stuffs data&#91;0&#93;, not data&#91;3&#93;

	char	*data = f.getData&#40;&#41;;

	char a = data&#91;0&#93;;	// returns 4, instead of 1
	char b = data&#91;1&#93;;	// returns 0, instead of 2
	char c = data&#91;2&#93;;	// returns 0, instead of 3
	char d = data&#91;3&#93;;	// returns 0, instead of 4
}

Incidentally, if I change the function signature to:

char	*first&#40;&#41;		{ return &data&#91;0&#93;; }
   char	*second&#40;&#41;	{ return &data&#91;1&#93;; }
   char	*third&#40;&#41;	{ return &data&#91;2&#93;; }
   char	*fourth&#40;&#41;	{ return &data&#91;3&#93;; }

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.



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users