我的C程序中似乎有内存损坏。我用_ASSERTE( _CrtCheckMemory( ) );
找到问题语句,它在它前面的一行上断开了scep_conf->engine_str = NULL;
。所以,如果我理解正确,那么之前的malloc会断开一些东西,对吧?
因此,这是导致问题的代码部分:
scep_conf = (SCEP_CONF *) malloc(sizeof(scep_conf));
scep_conf->engine = (struct scep_engine_conf_st *) malloc(sizeof(struct scep_engine_conf_st));
scep_conf->engine_str = NULL;
标题中的定义:
typedef struct {
struct scep_engine_conf_st *engine;
char *engine_str;
} SCEP_CONF;
struct scep_engine_conf_st{
char *engine_id;
char *new_key_location;
int storelocation;
char *dynamic_path;
char *module_path;
int engine_usage;
};
SCEP_CONF *scep_conf;
基本上我不明白为什么它会破坏我在这里的记忆。我是C的新手,所以可能有一些明显的东西我没有看到。
任何帮助都将不胜感激,谢谢。
这是不正确的:
scep_conf = (SCEP_CONF *) malloc(sizeof(scep_conf));
因为它只为SCEP_CONF*
而不是SCEP_CONF
分配足够的内存。应该是:
scep_conf = malloc(sizeof(*scep_conf)); /* cast unnecessary. */
值得一读我铸造malloc的结果吗?