Structures, Commons, and C++ Foundation 1

Structures, Commons, and C++ Foundation 1

1. Structures

Structures are user-defined data types in C programming, similar to Java Beans

//Student is equivalent to the class name
//Student and a can be undefined, representing structural variables, also variables of type Student
struct Student
	char name[50];
	int age;
} student,a;
//Use typedef definition
typedef struct{
    char name[50];
    int age;
} Student;

Dynamic memory requests are used when the structure requires too much memory. The number of bytes used by the structure is related to the field in the structure. The pointer takes up 4/8 bytes of memory, so it is more efficient to pass a pointer than to pass a value.

struct Student *s = (Student*)malloc(sizeof(Student));
printf("%d\n", s->age);

byte alignment

Memory space is divided by bytes, and you can theoretically access any type of variable from any starting address. However, in practice, when accessing a particular type of variable, access often starts at a specific memory address, which requires that all types of data be spatially arranged according to certain rules, rather than sequentially stored one after another, which is alignment.

The problem with byte alignment is primarily with structs.

struct MyStruct1
	short a;  
	int b;	
	short c; 
struct MyStruct2
     short a;
	short c; 
	int b;
//natural alignment
//1. The offset of the starting position of a variable from the starting position of the structure is an integer multiple of the number of bytes of the variable;
//2. The total number of bytes occupied by the structure is an integer multiple of the number of bytes of the variable with the longest number of bytes in the structure.
// short = 2 Complement 2
// int = 4
// short = 2 Complement 2
sizeof(MyStruct1) = 12
// Two short s together make up a four 
sizeof(MyStruct2) = 8
#pragma pack(2)//Specify 2-byte alignment
struct MyStruct1
	short a;  
	int b;	
	short c; 
#pragma pack() 	// Unaligned
//short = 2
//int = 4
//short = 2

A reasonable use of bytes can save storage space effectively

Unreasonable can waste space, reduce efficiency and even cause errors. (Accessing int, short, and other data from odd addresses can cause errors for some systems)

2. Commons

Store different data types in the same memory location

Commons should use enough memory to store the largest members in a common body

//Occupy 4 bytes
union Data
	int i;
	short j;
union Data data;
data.i = 1;
//i Data corruption
data.j = 1.1f;



C Use printf Output information to terminal
C++Standard Output Stream is provided 
#include <iostream>
using namespace std;
char *name = "David";
int time = 8;
cout << "David:" << time << "spot," << "The way of heaven never ends"<< endl;

Function Symbol Compatibility

The first lesson says that most of the code for C can be used directly in C++, but there are still things to note.

//If you need to call the methods in the library of the C implementation in C++.
extern "C" //Instructs the compiler to compile this part of the code in C instead of C++.

As we all know, C is a process-oriented language with no function overload.

void func(int x, int y);

For func functions compiled by the compiler of C, the name in the function library may be func (no parameter symbol), while C++ compilers produce names like funcii.

//main.c / main.cpp
int func(int x,int y){}
int main(){return 0;}

gcc main.c -o mainc.o
gcc main.cpp -o maincpp.o

nm -A mainc.o 
nm -A maincpp.o 



The problem with this is: C. The func function is defined in the h header file, then. The symbols used to implement this function in the C source file are func, which is then used in C++. The corresponding function symbols in the h file are compiled into another one, which does not match the symbols in the library, so the implementation in the library cannot be invoked correctly.

Therefore, for the C library, you can:

#ifdef __cplusplus
extern "C"{
void func(int x,int y);
#ifdef __cplusplus    

//_u cplusplus is a macro defined by the c++ compiler to indicate that you are currently in a c++ environment

The extern keyword can be used before a variable or function to indicate that it is actually defined in another file, and the compiler will look for it in another module when it encounters this keyword


Reference is a new type of C++ definition

//Declare formal parameters as references
void change(int& i) {
	i = 10;
int i = 1;
printf("%d\n",i); //i == 10

Reference and pointer are two things

Reference: A variable name is a label attached to a memory location, and a second label can be set

Simply put, a reference variable is an alias that represents another name of a variable

Character string

C String

Strings are actually one-dimensional character arrays terminated with the NULL character'\0'.

//Character Array=String
char str1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char str2[] = "Hello";

String Operation

strcpy(s1, s2);Copy string s2 to string s1.
strcat(s1, s2);Connection string s2 to the end of string s1.
strlen(s1);Returns the length of the string s1.
strcmp(s1, s2);Returns 0 if s1 and S2 are the same; Returns less than 0 if s1 < s2; Return greater than 0 if s1 > S2
strchr(s1, ch);Returns a pointer to the first occurrence of the character ch in string s1.
strstr(s1, s2);Returns a pointer to the first occurrence of string s2 in string s1.

Description: strcmp: Two strings are compared character by character from left to right (by ASCII value size)

C++ string class

The C++ Standard Library provides string class types that support all of the above, plus additional functionality.

#include <string>
//string is defined in std command space
usning namespace std;
string str1 = "Hello";
string str2 = "World";
string str3("The Way of Heaven");
string str4(str3);
// str1 splicing str2 combining new string s
string str5 = str1 + str2;
// Alteration of stitching str2 str1 after str1
//Get c-style string
const char *s1 = str1.c_str();
//String Length
//Is the length zero


Namespace namespace is equivalent to package

namespace A{
    void a(){}

error : a();
// :: Domain Operator
 Correct: A::a();

//Nested, of course
namespace A {
	namespace B{
		void a() {};

//You can also use the using keyword
using namespace A;
using namespace A::B;

When a global variable has the same name as one of the variables in a local function, it can be distinguished by:

int i;
int main(){
    int i = 10;
    printf("i : %d\n",i);
    //Operating on global variables
    ::i = 11;
    printf("i : %d\n",::i);

Tags: C++ NDK programming language

Posted by Shygirl on Mon, 18 Jul 2022 22:24:40 +0530