Page 1 of 1

C++ Warnings

Posted: Sat May 26, 2007 7:25 am
by senaus
Hi, I'm getting a rather annoying warning when compiling my C++ kernel with GCC:

Code: Select all

include/kernel/process.hpp: In static member function ‘static cThread* cThread::GetFromNano(tNanoThread*)’:
include/kernel/process.hpp:96: warning: invalid access to non-static data member ‘cThread::NanoThread’ of NULL object
include/kernel/process.hpp:96: warning: (perhaps the ‘offsetof’ macro was used incorrectly)
The code is correct and necessary. Does anyone know how to disable this warning?

Thanks,
Senaus

Re: C++ Warnings

Posted: Sat May 26, 2007 7:54 am
by Solar
senaus wrote:

Code: Select all

include/kernel/process.hpp: In static member function ‘static cThread* cThread::GetFromNano(tNanoThread*)’:
include/kernel/process.hpp:96: warning: invalid access to non-static data member ‘cThread::NanoThread’ of NULL object
include/kernel/process.hpp:96: warning: (perhaps the ‘offsetof’ macro was used incorrectly)
The code is correct and necessary.
I doubt that. A static function - i.e., one that is connected to the class but not a specific instance of the class, accesses a non-static data member - of which instance?

Are you trying to take the offset?

Posted: Sat May 26, 2007 8:31 am
by senaus
Yes, I'm trying to take the offset. The intention is to convert a pointer to a tNanoThread (a member of cThread) into a pointer to cThread. Here is the code:

Code: Select all

static cThread *	GetFromNano(tNanoThread *p_t)
{
	return reinterpret_cast<cThread*>
		(TYPE_ENTRY(p_t, cThread, NanoThread));
};
and

Code: Select all

#define TYPE_ENTRY(ptr, type, member) \
    ((type *)( (u8 *)(ptr) - (tAdr)(& (((type *)NULL)->member) ) ))
Thanks,
Senaus

Get Pointer To Parent Object Of Pointer To Member Object

Posted: Sat May 26, 2007 7:05 pm
by Kevin McGuire
You mean something like this:

Code: Select all

class A{
	public:
	uint32_t	m;
};
class B{
	public:
	uint32_t	m;
	A mA;
};

static B nothing;

B* GetBFromMemberAInObjectBWithOnlyPointerToMemberA(A *b)
{
	return reinterpret_cast<B*>(((uintptr_t)b) - (((uintptr_t)&nothing.mA) - ((uintptr_t)&nothing)) );
}

int main(int argc, char *argv[])
{
	B myB;
	B *b;
	b = GetBFromMemberAInObjectBWithOnlyPointerToMemberA(&myB.mA);
	printf("myB:%x b:%x\n", &myB, b);
        return 1;
}
Or just like the function name says:

Get Object B From Pointer To Member A in Object B When Only Having Pointer To Member A. You know the address of Object A in Object B, but not the address of Object B?

And do you think the warning message could be from trying to use the reference NULL as a identifier or something? I got strange results when I named my object NULL and rectified it when I changed the name to nothing. And, Yes - the reasoning of why is quite obvious.

Posted: Wed May 30, 2007 8:17 am
by Solar
senaus wrote:

Code: Select all

#define TYPE_ENTRY(ptr, type, member) \
    ((type *)( (u8 *)(ptr) - (tAdr)(& (((type *)NULL)->member) ) ))
What is tAdr?

Try using the offsetof() makro instead of the homegrown above.

The functionality of taking the offset of a member cannot be done generically and standard-compliant, i.e., each compiler must offer some way to do it, but there is no "right" way to pull it off. Thus, the offsetof() implementation that comes with your compiler (in <stdlib.h>) is the definite authority on it.

Posted: Sat Jun 09, 2007 5:00 am
by senaus
Sorry for the delay in reply, I've been a little distracted lately.
Get Object B From Pointer To Member A in Object B When Only Having Pointer To Member A. You know the address of Object A in Object B, but not the address of Object B?
Yes, that's exactly what I'm doing.
And do you think the warning message could be from trying to use the reference NULL as a identifier or something?
No, NULL is just a #define to 0 in my kernel. I'll try using a static (or const?) variable as in your example, see if that gets rid of the warnings.
What is tAdr?
tAdr is the address type. Pretty much this:

Code: Select all

typedef unsigned long tAdr;
I'm not using the built-in headers, so the offsetof() macro is undefined. I'll try and find it though, to see if I can amend mine.

Thanks everyone,
Senaus