Summary of C-language method of mixed compilation under C + +

Source: Internet
Author: User

Recently read SGI STL source code, feel the learning of C + + is very helpful, before the generic, iterator, traits and so on various characteristics of the concept is very vague, through the two days of pondering, coupled with the "STL Source Analysis" help, to C + + that weird grammar is no longer afraid.

Some of the problems encountered in it are summarized as follows:

1. C + + blank base class optimization (EBO)

Reference: http://www.xuebuyuan.com/1610977.html

If there is an empty class, the following. The space occupied is not 0byte, but usually 1 byte.

Class a{};

If there is a class, the following. While a accounts for 1 byte, while int accounts for 4bytes, the class occupies 8bytes due to the alignment of the compiler.

Class B {public:  A;  int x;    }

and Class C, as follows. Because the space base class is optimized for 4, the compiler automatically optimizes if there is no data in the inherited Class (1 bytes in space).

Class C:public a{public:int x;};

2. C C + + compile-language functions

In C + + in the implementation of the new function, its underlying is still using the C language of malloc, the implementation of the following ideas (see: http://songpengfei.iteye.com/blog/1100239).

Files Test_extern_c.h and Test_extern_c.c define a function thisistest ()

File:test_extern_c.h

#ifndef __test_extern_c_h__
#define __test_extern_c_h__

#ifdef __cplusplus

extern "C" {

#endif

extern int thisistest (int a,int b);

#ifdef __cplusplus
}

#endif

#endif

And the function is implemented as follows:

#include "test_extern_c.h" int thisistest (int a,int b) {return a * b;}

The main.cpp call is as follows:

#include "test_extern_c.h" #include "stdio.h" #include "Stdlib.h" Class Foo{public:int bar (int a,int b) {printf ("result=% I\n ", Thisistest (A, b));}; int main (int argc,char **argv) {int a = Atoi (argv[1]); int b = atoi (argv[2]); Foo *foo = new Foo (); Foo->bar (A, B); return (0);}

The Mac GCC compiles as follows:

Gcc-c TEST_EXTERN_C.C//generated the target file

A target file can be generated by the following command.

g++ main.cpp TEST_EXTERN_C.O

3. Implementation mechanism of New/delete in C + + language

4. Mixed programming of assembly language in C + +

Summary of C-language method of mixed compilation under C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.