1. 数组越界
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    #include<stdio.h>
    void sortArray(int* nums, int numsSize)
    {
    for(int i0=0;i0<numsSize;i0++)
    {
    int Min=nums[i0];
    int pos=i0;
    for(int j1=i0;j1<numsSize-1;j1++) //j1<numsSize-1 不然下面 if对比会导致数组越界: j1+1越结值为0,会让下面if条件 跟0做对比!
    {
    if(nums[j1]>nums[j1+1])//j1+1
    {
    Min=nums[j1+1];
    pos=j1+1;
    }
    }
    int temp=nums[i0];
    nums[i0]=Min;
    nums[pos]=temp;
    // for(int it=0;it<numsSize;it++)
    // {
    // printf("[%d] ",nums[it]);
    // }
    // printf("\n");
    }
    }
    int main()
    {
    int arr[8]={3,4,5100,23,4,5,0,-1};
    sortArray(arr,8);
    printf("\nresult-------->\n");
    for(int it=0;it<8;it++)
    {
    printf("[%d] ",arr[it]);
    }
    printf("\n");
    return 0;
    }

类指针

  1. 指针对象为NULL,为什么仍然可以调用成员函数

参考文章1_对象指针为NULL,为什么还是可以调用成员函数

参考文章2——值为NULL的对象指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include<iostream>
#include<string.h>
using namespace std;
class A
{
public:
static void f1(){ cout<<"f1"<<endl; }
void f2(){ cout<<"f2"<<endl; }
void f3(){ cout<<num<<endl; }
virtual void f4() {cout<<"f4"<<endl; }
public:
int num;
};

int main(int argc,char* argv[])
{
A* pa = NULL;
pa->f1(); //正常
pa->f2(); //正常
pa->f3(); //错误,提示段错误
pa->f4(); //错误,提示段错误
return 0;

}

类的成员函数并不与具体对象绑定,所有的对象共用同一份成员函数体,当程序被编译后,成员函数的地址即已确定,这份共有的成员函数体之所以能够把不同对象的数据区分开来,靠的是隐式传递给成员函数的this指针,成员函数中对成员变量的访问都是转化成”this->数据成员”的方式。因此,从这一角度说,成员函数与普通函数一样,只是多了一个隐式参数,即指向对象的this指针。而类的静态成员函数只能访问静态成员变量,不能访问非静态成员变量,所以静态成员函数不需要指向对象的this指针作为隐式参数。

指针不是基础类型

指针是

其他概念

数组不能直接复制给指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39


#include<stdio.h>
void take(int (* arr)[105])
{
arr[0][1]=34;



}
int main()
{


int columnArray[2999][105]={1,23,4};
printf("columnArray=%d\n",**columnArray);


//int (* ptr)[105]=columnArray;// ok



int **ptr=columnArray;//Wwarning: initialization of ‘int **’ from incompatible pointer type ‘int (*)[105]’ [-Wincompatible-pointer-types]
//ptr[0][1]=11; // Error
printf("ptr[0][0]=%d\nm",ptr[0][0]); ///Segmentation fault (core dumped)



//columnArray[0][1]=34;
//printf("columnArray[0][1]=%d\n",columnArray[0][1]);


take(columnArray);
printf("columnArray[0][1]=%d\n",columnArray[0][1]);

return 0;
}


  1. int *q;只有地址,没有内存空间。这个地址是随机地址

  2. 但只要是变量就有地址,就可以定义一个指针变量存放它:

  • QT程序崩溃:Segmentation fault

描述:

1
2
3
4
The inferior stopped because it received a signal from the operating system.

Signal name: SIGSEGV
Signal meaning: Segmentation fault

出错代码:

c/c++:回调函数

  1. 函数是一种function-to-pointer方式, 其会自动转换成指针的类型, &fun是该函数的地址, 为指针类型, fun是一个函数, 会转换成其指针类型, 而对于*fun, 由于fun已经变成了指针类型,

http://blog.csdn.net/callmeback/article/details/4242260/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include<iostream>
void printWelcome(int len)
{
printf("欢迎欢迎 -- %d/n", len);
}

void printGoodbye(int len)
{
printf("送客送客 -- %d/n", len);
}

void callback(int times, void(*print)(int))
{
int i;
for (i = 0; i < times; ++i)
{
print(i);
}
printf("/n我不知道你是迎客还是送客!/n/n");
}
void main(void)
{
callback(10, printWelcome);


std::cout << std::endl<< "--------------------------------------------------------------------------------------------" << std::endl;




callback(10, printGoodbye);
std::cout << std::endl << "--------------------------------------------------------------------------------------------" << std::endl;
printWelcome(5);
}
  1. 注册回调函数
    https://blog.csdn.net/mrailence/article/details/52251201

  2. 回调函数demo

  3. 回调函数demo2

  4. 关于回调函数

名词解释

  • LSB
    英文: Least Significant Bit
    中文: 最低有效位
    介绍: 对于一个给定的数据串(整数),如二进制的1001或者十进制351,其最低有效位就是拥有最小单位数值的那一位。比如二进制1001的最右一位,拥有数值1,在该整数中代表最低位,该位的值可以决定整数是奇数(为1)还是偶数(为0),十进制数同理。

一般LSB就是一个整数的最右一位,所以似乎该概念有些多余,==但是凡事都有例外,某些数据传输或是处理器恰恰相反,最左一位是LSB==,所以在计算领域就定义了这个最低有效位以明确说明数据格式。
注:相应的也有MSB - 最高有效位。

Fork me on GitHub