版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011068702/article/details/82431209
1 问题
在open ssl加载引起里面部分我自己写了一个get和set方法,然后我在其它地方调用使用了Info类型的声明
extern Info info;
先初始化info,然后
info->setA(&info, value);
char value[100];
但是我立马
info->getA(&info, value, sizeof(value));
LOGI("chenyu", "value is %s", vlaue);
竟然拿不到值
2 分析
在open ssl加载引起里面部分代码如下
typedef struct st_info {
int (* init)(struct st_info *info);
int (* setA)(struct st_info *info, const char * value);
int (* getA)(struct st_info *info, char path[], int size);
/**
一系列函数指针
**/
pthread_mutex_t lock;
char key[128];
}Info;
static int setA(Info *info, const char * value);
static int getA(Info *info, char path[], int size);
static int init(Info *info);
static int init(KeyInfo *info) {
assert(info != NULL);
memset(info->key, 0 sizeof(info->key));
info->init = init;
info->setA = setA;
info->getA = getA;
/**
**/
}
static int setA(Info *info, const char * value)
{
assert(value != NULL);
pthread_mutext_lock(&info->lock);
strncpy(info->key, value, sizeof(info->key) - 1);
pthread_mutext_unlock(&info->lock);
}
static int getA(Info *info, char values[], int size)
{
if (!info || !pin) {
return -1;
}
pthread_mutex_lock(&info->lock);
strncpy(values, info->key_pin, size - 1);
*(values + size - 1) = 0;
pthread_mutex_unlock(&info->lock);
return 1;
}
然后在声明的地方我也有这个成员变量是函数指针的结构体
typedef struct st_info {
int (* getA)(struct st_info *info, char path[], int size);
int (* init)(struct st_info *info);
int (* setA)(struct st_info *info, const char * value);
/**
一系列函数指针
**/
pthread_mutex_t lock;
char key[128];
}Info;
extern Info info;
只不过这里我的st_info结构体里面的函数指针的顺序不同
然后我一开始不知道是什么原因,编译也没问题,然后我在setA方法里面加了日志,也能打印,但是在getA方法里面也加了日志
但是不能打印,最后请教了同事,原来是st_info这个结构体的函数指针顺序写过了,要和之前的保持一致,我之前用extern的时候,一般都是用std::string类型,但是这里也可以理解Info为一种类型,所以里面的东西不能改动,不然就会有问题,下次要注意