1.i386架构之atol函数使用导致的业务程序错误:
情景:将框架传递的链接地址采用整形保存传输,在i386架构上导致地址比较大,采用atol转型可能导致数据被截断出现异常。
方案:采用atoll更大的数据类型进行处理即可避免该问题。
2.Json库使用注意long int问题导致编译不通过问题:
情景:
Json::Value result;
long int pid = 10001;
result["pid"] = pid;
如上编译会报错:
error: convert from 'long int' to 'const Json::Value' is ambiguous
方案:采用跟Json库携带定义的想通过的类型进行使用:
Json::Value result;
long int pid = 10001;
Json::UInt upid = pid;
result["pid"] = upid; // 规避出现如上问题,采用同等的类型
3.Centos7.2- opendir函数打开普通文件发现ptr->d_type 类型不等于8,反而等于0问题
方案:如果遇到该种情况,请使用stat方法判别文件类型,stat结构中定义了文件的相关节点类型值,在结合具体业务判别即可。
例如:
DT_REG == ptr->d_type
// 正常情况,文件类型 ptr->d_type == 8,
在dirent.h文件中定义.
centos7.2系统,ptr->d_type == 0,导致无法读取文件信息。可采用stat结构进行统计:
{
struct stat statbuf;
lstat(tmpPathfile, &statbuf);
if( S_ISREG(statbuf.st_mode)) // file
{
//to do
}
}
///***也可以使用stat方法,跟lstat方法有点差异,stat方法找到链接文件,会追踪到文件本身,而lstat就获取的链接文件。
4.动态库升级
问题:
通过dlopen打开一个动态库,会将对应的信息加载到内存当中,此时即便更新了so库,再次使用的时候,发现调用的信息仍旧是旧的动态库的信息。从而导致热升级失效。
解决方法:
升级过后,将旧的句柄关闭,再重新打开,重新加载对应的动态库。
5.centos8.0系统内核字符串长度方法使用:
argvlen = strlen_user(paramaddr);// 没有如此的函数,替换为如下:
argvlen = strnlen_user(paramaddr,INT_MAX);
6.c++string跟char*赋值-异常崩溃问题记录
使用char*给string赋值时,一定要避免char*为空的情况发生,因为char*为空时,会造成异常程序崩溃问题发生:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Aborted (core dumped)
导致如上问题:
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
bool test(const char* manu,const char* product)
{
std::string ddd(manu); //该处因为manu为空,导致程序异常崩溃
return false;
}
int main(){
char*a = NULL;
char*b = "jkjk";
test(a,b);
return 0;
}
使用 string跟char*之间赋值一定主意此问题,避免产生崩溃问题。