C++ Warnings

Programming, for all ages and all languages.
Post Reply
senaus
Member
Member
Posts: 66
Joined: Sun Oct 22, 2006 5:31 am
Location: Oxford, UK
Contact:

C++ Warnings

Post 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

Code: Select all

-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M/MU d- s:- a--- C++++ UL P L++ E--- W+++ N+ w++ M- V+ PS+ Y+ PE- PGP t-- 5- X R- tv b DI-- D+ G e h! r++ y+
------END GEEK CODE BLOCK------
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: C++ Warnings

Post 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?
Every good solution is obvious once you've found it.
senaus
Member
Member
Posts: 66
Joined: Sun Oct 22, 2006 5:31 am
Location: Oxford, UK
Contact:

Post 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

Code: Select all

-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M/MU d- s:- a--- C++++ UL P L++ E--- W+++ N+ w++ M- V+ PS+ Y+ PE- PGP t-- 5- X R- tv b DI-- D+ G e h! r++ y+
------END GEEK CODE BLOCK------
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Get Pointer To Parent Object Of Pointer To Member Object

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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.
Every good solution is obvious once you've found it.
senaus
Member
Member
Posts: 66
Joined: Sun Oct 22, 2006 5:31 am
Location: Oxford, UK
Contact:

Post 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

Code: Select all

-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M/MU d- s:- a--- C++++ UL P L++ E--- W+++ N+ w++ M- V+ PS+ Y+ PE- PGP t-- 5- X R- tv b DI-- D+ G e h! r++ y+
------END GEEK CODE BLOCK------
Post Reply