[1] NDK series of basic data types and memory pattern

As an Android developer, how can it not NDK development, let's start our journey NDK

How to build environmental Leave aside, your heart is

Basic data types

1. Signed - Signed, may be modified char, int. Int is the default signed.
2. unsigned - unsigned, can be modified int, char

Integer byte Ranges Placeholder
int 4 -2,147,483,648 to 2,147,483,647 %d
unsigned int 4 0 to 4,294,967,295 in%
short 2 -32,768 to 32,767 %hd
unsigned short 2 0 to 65,535 % Hu
long 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long 4 0 to 4,294,967,295 % Lu
char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c

In order to obtain an exact size or type of a variable on a specific platform, using sizeof operator.

Expression sizeof (type) to give the object type or size in bytes of storage.

long int is actually a long integer int = long dispensed with

In the standard, predetermined int least as long and short, long, and at least as long int.

Exploring Questions:

Why are there long?

long int and early 16-bit PC 2 when the int bytes long. 4 bytes, and the computer to the present, 32, 64 in general, as long and int. And java analogy, then, java is a long long long 8 bytes

Formatted display

Float byte accuracy Placeholder
float 4 6 decimal places %f
double 8 15 decimals %lf
long double 8 19 decimals %Lf

O octal%
16 lowercase hexadecimal:% x uppercase: X-%
(0x) +16% # x front hexadecimal

Some other things

Previous C99 standard, C language which is not bool, C ++ which have,
C99 standard which defines bool type, header files need to introduce stdbool.h
bool type has only two values: true = 1, false = 0 .
So in fact bool is an int
so in c / c ++ in if follow a rule, non-zero is true, is non-empty to true;
NULL fact, that is to define 0

format

printf、sprintf等

Writing data formatted in the first parameter

char str[100];
sprintf(str, "img/png_%d.png", 1);
printf("%s", str);

//使用 0 补到3个字符
sprintf(str, "img/png_%03d.png", 1);
printf("%s", str);

And an array of memory layout

Array: Continuous memory

//java
int[] a

//c
//必须声明时候确定大小
int a[10]  
//或者 直接初始化 
int a[] = {1,2,3}

//大小
printf("%d",sizeof(a)/sizeof(int));

Stack memory limit linux: ulimit -a view
but not so much direct allocation, because the stack might save the parameters, return address information, etc.

Dynamic memory allocation

malloc
does not initialize the contents of memory, generally calling memset function to initialize the memory of this section.

calloc
application memory and data memory is initialized to NULL.

int pn = (int)calloc(10, sizeof(int));

realloc

Malloc memory for the application to adjust the size.

char *a = (char*)malloc(10);
realloc(a,20);

In particular:
alloca
in the application stack memory, so no release.

int *p = (int *)alloca(sizeof(int) * 10);

Physical memory
physical memory refers to memory space obtained by physical memory

Virtual memory is
a memory-management
programs running in the computer memory required by the implementation, if a large memory for program execution, it will lead to memory exhausted.
Virtual memory technology will be absorbed a portion of hard disk space to act as memory usage.

Process allocates memory mainly by the completion of two system calls: brk and mmap .

  1. brk is _edata (with a pointer to the stack position) to push the high address;
  2. mmap find a free virtual memory.

Herein by malloc function completes the memory (C standard library) provided glibc

less than 128k memory malloc, brk allocate memory using the address _edata pushed to a higher, greater than 128k using mmap

Storing execution codes (cpu instruction to be executed)

Is extended to lower the stack data structure address
stack is a data structure to the high address extension

Code

https://github.com/ddssingsong/AnyNdk

Guess you like

Origin blog.csdn.net/u011077027/article/details/92800334