Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » Make THISFN simpler and more powerful (by taking advantage of some new c++ feature)
Re: Make THISFN simpler and more powerful [message #60922 is a reply to message #59158] Tue, 08 October 2024 18:38 Go to previous messageGo to previous message
Lance is currently offline  Lance
Messages: 656
Registered: March 2007
Contributor
OK, this is not even c++ language related. But c++ is to be blamed.

Strange padding.

#include <iostream>

struct A{
    int a;
    unsigned b:16; // for demonstration purpose only
    bool c:1;
};

int main()
{
   std::cout<<"sizeof(A) is "<<sizeof(A)<<std::endl;
}


You would expect the result be 8, but on windows MSBT22x64 and CLANG64 will give 12. (CLANG64 and GCC64 on linux give 8, which is naturally and as expected)

If you think that's not weired enough, it goes further.
#include <iostream>

struct A{
	int a;
	union{
		int dummy;
		struct{
		    unsigned v1:16;
		    bool     v2:1;
		};
	};
};


int main()
{
	A a;
	a.v2 = true;
	a.dummy = 0;
   
	std::cout<<"a.v2 is "<<a.v2<<std::endl;
}


The output is true. The compiler is smart enough to disregard the union request and put dummy and the unnamed struct object side by side, instead of start from the same address.

Maybe there is something I did wrong?
 
Read Message icon10.gif
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Math - GaussJordan function
Next Topic: Clang cannot find DLLs
Goto Forum:
  


Current Time: Sat May 10 00:41:25 CEST 2025

Total time taken to generate the page: 0.01171 seconds