18-3-22
今日使用C的 malloc 函数 创建一个 Char* 并分配空间无问题,但是在传递的时候出现了问题多出来一堆乱码,后发现需要多一位长度用于存放 '/0' 以免传递之后调用strlen()函数后 导致内存块错误。
char temporaryCA[5];
temporaryCA[4] = 0x00;//最后位置0 Todo 否则上方 strlen 使用后 内存块 可能出现问题
再调用下述函数时,若上述temporary数组为4,即未空出一位存放 '/0',则再调用下述函数之后数组所指向的内存块会出现错误。
//convert char[] to Int
int ConvertCharArrayToInt(const char ca[])
{
//注意ca 的最后位 要 '/0' 否则 strlen 传递之后会出问题
int Cnum = 0;
printf("转换数组长度:%d\n", strlen(ca));
for (int i = 0; i < strlen(ca); i++)
{
int p = 1;
for (int j = 0; j < strlen(ca)-1-i; j++)
{
p *= 10;
}
Cnum += (ca[i] - '0')*p;
}
printf("转换结果:%d\n", Cnum);
return Cnum;
}
18-5-28
在使用C/C++ 指针创建数据链表的时候,如果某个指向数组的指针超界了,那么会导致后续的相关数据的内存块出现问题,表现层为:xx=-574641145....
18-6-4
Linux使用线程时--->注意点
gcc 编译会有 undefined reference to 'pthread_create' ('pthread_join')
reason:pthread 非 Linux库,gcc 编译时需要加 -lpthread
完整编译:gcc thread.c(file name) -o thread(exe name) -lpthread
18/7/12
long int byte 的转换
例如:
byte c;
long d;
d = c*256*256;//可能d 会有负值出现 因为值转换的原因
改为d=(long)(c*256*256); 即可解决这个问题
18/7/27
C++ Vector and Struct
Add a Struct to Vector
Error e1;
for (int i = 0; i < 10; i++)
{
e1.type = i;
e1.top = i;
e1.bottom = i;
errorVector.push_back(e1);
}
for (int j = 0; j < errorVector.size(); j++)
{
cout << errorVector[j].type << "\t" << errorVector[j].top << "\t" << errorVector[j].bottom << endl;
}
Remove a Item from Vector
//vector <Error>::iterator Iter; //创建容器的迭代器 通过迭代器得到容器对应位置的下标 调用erase 方法去删除它
for (vector<Error>::iterator it = errorVector.begin(); it != errorVector.end(); )
{
if ((*it).type %3 == 0)
{
it = errorVector.erase(it); //不能写成arr.erase(it);
}
else
{
++it;
}
}