cppTest-3.0:指针相关-3(new&delete)

本文详细介绍了 C++ 中指针与 new/delete 的使用方法,包括单个对象和数组的动态分配与释放,以及多维数组的内存管理。通过具体的代码示例展示了正确与错误的操作方式。
/**
 *cppTest-3.0:指针相关-3(new&delete)
 *
 *author 炜sama
 */
#include<iostream.h>
void main()
{
    int *pi;
	pi=new int;
	*pi=888;
	cout<<*pi<<endl;
	delete pi;

	int *pi2=new int(123);
	cout<<*pi2<<endl;
	delete pi2;

	int *pis=new int[2];
	pis[0]=0;
	pis[1]=1;
	cout<<pis[0]<<endl;
	cout<<pis[1]<<endl;
	*pis=100;
	*(pis+1)=200;
	cout<<pis[0]<<endl;
	cout<<pis[1]<<endl;
	cout<<*(pis+1)<<endl;
	//cout<<pis<<endl;
	delete []pis;//[]是必须的!它告诉编译器pis指向的是数组而不是单个对象。
	//delete pis;//这种释放方式是错的!即使运行时没错,但是有可能无法释放pis指向的空间!
	
	//delete pis[];//这种释放方式是错的!

	//不能在new的同时给数组初始化!
	//int *pis2=new{1,2};
	//int *pis3=new int{1,2};
	//int *pis4=new int[]{1,2};
	//int *pis4=new int[2]{1,2};
	int is[2]={1,2};

	/*int *pis2=new int[];//虽然不指定数组大小没有编译报错,但是运行时报错,故不建议这么做!
	pis2[0]=0;
	pis2[1]=1;
	cout<<pis2[0]<<endl;
	cout<<pis2[1]<<endl;
	delete []pis2;*/

	//new多维数组时,左值一定要指定每一维大小,并且右值至多最高维可以不指定大小。不然编译报错!

	//int (*piss0)[]=new int[][];//报错!
	//int (*piss1)[]=new int[][2];//报错!
	//int (*piss2)[]=new int[2][];//报错!
	//int (*piss3)[]=new int[2][2];//报错!
	//int (*piss4)[2]=new int[][];//报错!
	int (*piss5)[2]=new int[][2];
	//int (*piss6)[2]=new int[2][];//报错!
	int (*piss7)[2]=new int[2][2];

	int (*pisss)[2][2]=new int[][2][2];
	//delete [][]pisss;//错误!!!
	delete []pisss;

	int i=2,j=2,k=2;
	int (*pisss2)[2][2]=new int[i][2][2];//只有第一维可以是变量表达式!!
	//int (*pisss3)[2][2]=new int[i][j][2];//错误!
	//int (*pisss4)[2][2]=new int[i][j][k];//错误!

	//const int *pci=new const int(128);
}

<think>根据用户提供的ldd命令输出,显示`libcpptestruntime64.so`已正确链接到指定路径:`/home/zhouweixiang/cpptest/cpptest/bin/engine/lib/libcpptestruntime64.so`。这表明动态链接器能够找到该库文件,但之前运行时仍报错,问题可能不在库路径配置上,而是其他原因。以下是进一步分析和解决方案: ### 问题排查方向 1. **文件权限问题** 检查库文件是否具有可执行权限: ```bash ls -l /home/zhouweixiang/cpptest/cpptest/bin/engine/lib/libcpptestruntime64.so ``` - 若权限不足,添加读权限: ```bash chmod +r /home/zhouweixiang/cpptest/cpptest/bin/engine/lib/libcpptestruntime64.so ``` 2. **库文件损坏** 验证库文件完整性: ```bash file /home/zhouweixiang/cpptest/cpptest/bin/engine/lib/libcpptestruntime64.so ``` - 正常应输出:`ELF 64-bit LSB shared object, ...` - 若损坏,需重新安装Parasoft或从备份恢复 3. **架构兼容性** 确认程序与库的架构一致(均为64位): ```bash # 检查程序架构 file my_projectTest.exe # 检查库架构 file /home/zhouweixiang/cpptest/cpptest/bin/engine/lib/libcpptestruntime64.so ``` 4. **依赖缺失** 检查该库的次级依赖: ```bash ldd /home/zhouweixiang/cpptest/cpptest/bin/engine/lib/libcpptestruntime64.so | grep "not found" ``` 5. **SELinux限制** 临时禁用SELinux测试: ```bash setenforce 0 # 需root权限 ./my_projectTest.exe ``` - 若可运行,需配置SELinux策略 ### 具体操作步骤 **步骤1:验证权限** ```bash # 查看库权限(第三组应有r--) ls -l /home/zhouweixiang/cpptest/cpptest/bin/engine/lib/libcpptestruntime64.so # 修复权限(如果需要) sudo chmod 755 /home/zhouweixiang/cpptest/cpptest/bin/engine/lib/libcpptestruntime64.so ``` **步骤2:检查依赖链** ```bash # 查看库的完整依赖 ldd /home/zhouweixiang/cpptest/cpptest/bin/engine/lib/libcpptestruntime64.so # 示例输出中若出现"not found",需安装对应包 ``` **步骤3:运行调试** 使用`strace`跟踪系统调用: ```bash strace -e openat ./my_projectTest.exe 2>&1 | grep cpptest ``` - 观察是否有访问拒绝(EACCES)或文件不存在(ENOENT)错误 ### 预防措施 - 在部署环境中统一设置文件权限(如755) - 使用`checksec`工具验证二进制安全性配置 - 在容器化环境中确保所有依赖完整打包 §§相关问题§§ 1. Linux系统中如何诊断动态链接库的次级依赖缺失问题? 2. 使用strace进行运行时错误诊断的关键步骤有哪些? 3. SELinux如何影响应用程序对共享库的访问权限? 4. 如何修复因文件权限配置错误导致的共享库加载失败? 5. 在多架构系统中如何确保程序与依赖库的架构一致性?</think>根据您提供的 `ldd` 输出,系统已成功找到并链接了 `libcpptestruntime64.so` 库文件: ``` libcpptestruntime64.so => /home/zhouweixiang/cpptest/cpptest/bin/engine/lib/libcpptestruntime64.so (0x00007f7ec0576000) ``` 这表明**库文件路径配置正确**,但之前运行时仍报错,问题可能转向其他方向。以下是针对此情况的深度排查方案: ### 关键排查方向 1. **文件权限验证** ```bash # 检查库文件权限 ls -l /home/zhouweixiang/cpptest/cpptest/bin/engine/lib/libcpptestruntime64.so # 检查可执行文件权限 ls -l ./my_projectTest.exe ``` - 需要至少 `-r--r--r--`(644)权限 - 修复命令:`chmod 755 libcpptestruntime64.so` 2. **库文件完整性检查** ```bash # 验证ELF文件头 file /home/zhouweixiang/cpptest/cpptest/bin/engine/lib/libcpptestruntime64.so # 检查文件是否损坏 strings libcpptestruntime64.so | head -n 20 ``` 3. **依赖库的依赖关系** ```bash # 检查libcpptestruntime64.so的次级依赖 ldd /home/zhouweixiang/cpptest/cpptest/bin/engine/lib/libcpptestruntime64.so | grep "not found" ``` 4. **运行时环境差异** ```bash # 检查当前环境变量 printenv | grep LD_ # 清除可能冲突的环境变量 unset LD_PRELOAD ``` 5. **文件系统错误** ```bash # 检查文件系统错误 ls -lL /home/zhouweixiang/cpptest/cpptest/bin/engine/lib/libcpptestruntime64.so ``` ### 高级诊断方法 1. **使用`strace`追踪系统调用** ```bash strace -e trace=open,read,stat ./my_projectTest.exe 2>&1 | grep cpptest ``` 2. **GDB调试加载过程** ```bash gdb ./my_projectTest.exe (gdb) catch load (gdb) run ``` 3. **检查内核日志** ```bash sudo dmesg | grep cpptest ``` 4. **验证GLIBC兼容性** ```bash # 检查可执行文件要求的GLIBC版本 objdump -p ./my_projectTest.exe | grep -A10 "Version References" ``` ### 终极解决方案 如果以上排查均未解决问题: 1. **重新生成测试程序** 在Parasoft C++test中清理并重新构建测试套件 2. **更新Parasoft运行时库** ```bash # 从原始安装包提取库文件 sudo cp /path/to/installer/libcpptestruntime64.so /home/zhouweixiang/cpptest/cpptest/bin/engine/lib/ ``` 3. **静态链接选项** 在C++test配置中启用静态链接(增加二进制体积但避免动态依赖)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值