杂项 1

it2026-08-19  6

对于一些恶心人的调试失败原因 我深感自己菜的很。 真的菜

在堆中开辟一个4×5的二维int数组 //#include <stdio.h> //#include <malloc.h> //int** p; //int i, j; //void main() { // p = (int**)new int*[4]; // if (NULL == p) return; // for (i = 0; i < 4; i++) { // p[i] = (int*)new int [5]; // if (NULL == p[i]) return; // } // // for (i = 0; i < 4; i++) { // delete []p[i]; // } // delete[] p; //} //在堆中开辟一个4×5的二维int数组 #include <stdio.h> #include <malloc.h> #include<iostream> using namespace std; int** p; int i, j; int main() { p = (int**)new int* [10];//分配好的内存直接使用就好 if (NULL == p) return 0; for (i = 0; i < 10; i++) { p[i] = (int*)new int[10]; if (NULL == p[i]) return 0; } int a = 10; int w = 100; int y = 1000; int ww = 1; //指针定义后不分配内存 就是个野指针指向某个未知内存 很危险可以指向null 以免不可补救 //写入访问权限冲突 为nullptr 此时需要进行内存分配 /**p = &a; p = &*p;*/ //**p = a; //(*p)++; //**p = w; /**p = &w; p = &*p;*/ //(*p)++; //**p = y; /**p = &y; p = &*p;*/ /*因为 a[n] 本质上是 *(&a[0] + n) 的语法。 当你使用 &a[4] - &a[3] 时,实际上是:(&a[0] + 4) - (&a[0] + 3)= 1 而不是你想象中的 a第四个元素 的地址减去 第三个元素的地址 int b = &a[0]; int c = &a[1]; printf("%d\n", c-b); 把 a[0] , a[1] 的地址强制另存为整数,然后相减,即可*/ //** p = a;//指针指向的 地址赋值a的值 记着二级指针指向堆 p[0]只存放p[0][i]首地址 然后p[0]+0,p[0]+1...... //堆里面存着一级指针p[i]的集合 一级p[i]可以指向其他指针也可以存放变量的地址 int* wc,*cw,ccc; //wc = &p;//错误不能让int***类型的值赋给wc(int*类型)指针(或者不能存放二级指针地址) wc = cw;//指向其他指针 wc = &ccc;//存放ccc的地址 //这里注意 数据按边界对齐 起始地址能被自身长度整除 地址*8位/数据类型长度位数。 cout << "&p: " << &p << endl; //1 cout << "&**p: " << &**p << endl; //2 同4 cout << "&a: " << &a << endl; //3 cout << "&p[0][0]:" << &p[0][0] << endl; //4 cout << "&p[0]: " << &p[0] << endl; //5 地址+4同7 cout << "&p[1][0]:" << &p[1][0] << endl; //6 cout << "&p[1]: " << &p[1] << endl; //7 cout << "&p[2]: " << &p[2] << endl; //10 cout << "&p[2][0]:" << &p[2][0] << endl; //11 cout << "&p[3][0]:" << &p[3][0] << endl; //12 cout << "&p[4][0]:" << &p[4][0] << endl; //12 cout << "&p[5][0]:" << &p[5][0] << endl; //12 cout << "&p[6][0]:" << &p[6][0] << endl; //12 cout << "p[1][0]-p[0][0]<<" << (&p[1]+0)- (&p[0]+0) <<" = (&p+1)-(&p+0)"<<(&p+1)-(&p+0)<< endl; int ii, jj; jj = (int)&p[0][0]; ii = (int)&p[1][0]; cout << "ii: " << ii << "jj : " <<jj<< endl; cout << "p[1][0]-p[0][0]<<" << ii-jj<< endl; cout << "&p: " << &p << endl; //8 p++;//地址就混乱了 cout << "p++后&p[0][0]: " << &p[0][0] << endl;//8 变为p[1][0]的地址了 **p = w; cout << "&w " << &w << endl;//地址+C与y地址同 p++; cout << "p++后&p[0][0]: " << &p[0][0] << endl;//9 变为p[2][0] 的地址了 **p = y; cout << "&y " << &y << endl; ww++; int kk = ww; while (kk--) { p--; cout << &p[0][0] << endl; } cout << p << ":是下面那个数地址的地址" << **p << "前面数的地址:" << *p << endl; kk = ww; while (ww--) { cout << p << ":是下面那个数地址的地址" << **p << "前面数的地址:" << *++p << endl; } while (kk--) { p--; } for (int i = 0; i < 10; i++) { for (int h = 0; h< 10; h++) { cout << "&p[" << i << "][" << h << "] : " << &p[i][j] <<" "; } cout << endl; } // 动态二级指针法 它指向的数组的多少可变,指向内存大小可变, //内存可以不连续,但可以以连续的方式访问 //delete p; //指示调用函数具有不一致的数组运算符分配内存 new [] ,但用标量运算符释放了该函数 delete 。 此缺陷可能会导致泄漏、内存损坏,以及在重写操作员时崩溃的情况下。 如果内存是通过数组分配的 new [] ,则通常应通过数组来释放内存 delete[] for (int i = 0; i < 10; i++) { cout << p[i] << " "; delete[]p[i]; } delete[] p; return 0; }
最新回复(0)