的C指针和内存分配:realloc的数组和指针传递 [英] C Pointer and Memory Allocation: Realloc Arrays and Pointer Passing

查看:172
本文介绍了的C指针和内存分配:realloc的数组和指针传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于那些用C经验丰富,这将是一个简单的内存分配/引用问题:

For those experienced with C, this will be a simple memory allocation/referencing problem:

下面是我的数据结构:

struct configsection {
    char *name;
    unsigned int numopts;
    configoption *options;
};
typedef struct configsection configsection;

struct configfile {
    unsigned int numsections;
    configsection *sections;
};
typedef struct configfile configfile;

下面是我对初始化configsection或CONFIGFILE程序,并添加configsection到CONFIGFILE:

Here are my routines for initializing a configsection or configfile, and for adding a configsection to a configfile:

// Initialize a configfile structure (0 sections)
void init_file(configfile *cf) {
    cf = malloc(sizeof(configfile));
    cf->numsections = 0;
}
// Initialize a configsection structure with a name (and 0 options)
void init_sec(configsection *sec, char *name) {
    sec = malloc(sizeof(configsection));
    sec->numopts = 0;
    sec->name = name;
    printf("%s\n", sec->name);
}
// Add a section to a configfile
void add_sec(configfile *cf, configsection *sec) {
    // Increase the size indicator by 1
    cf->numsections = cf->numsections + 1;
    // Reallocate the array to accommodate one more item
    cf->sections = realloc(cf->sections, sizeof(configsection)*cf->numsections);
    // Insert the new item
    cf->sections[cf->numsections] = *sec;
}

我相信我的init_sec()函数,我的问题起源。下面是一个例子:

I believe my problem originates in my init_sec() function. Here is an example:

int main(void) {

// Initialize test configfile
configfile *cf;
init_file(cf);

// Initialize test configsections
configsection *testcs1;
init_sec(testcs1, "Test Section 1");
// Try printing the value that should have just been stored
printf("test name = %s\n", testcs1->name);

虽然的printf() init_sec()成功打印我只是存储在configsection的名字,试图在同样的事情的printf() 主要()产生分段错误。此外, addsec()产生分段错误。

Although the printf() in init_sec() successfully prints the name I just stored in the configsection, attempting the same thing in the printf() of main() produces a segmentation fault. Further, addsec() produces a segmentation fault.

推荐答案

这个程序应该是

void init_file(configfile **cf) { 
    *cf = malloc(sizeof(configfile)); 
    (*cf)->numsections = 0;
    (*cf)->sections = NULL; // You forgot to initialise this.
}

即。通过 init_file称为(安培; myconfigfilepointer); 所以的malloc 返回值被传递回

需要做的同样的伎俩init_sec

此功能是不正确 - 这里是一个修正版本

This function is incorrect - here is a corrected version

void add_sec(configfile *cf, configsection *sec) {     
    // Increase the size indicator by 1     
    // Reallocate the array to accommodate one more item     
    cf->sections = realloc(cf->sections, sizeof(configsection)*(1 + cf->numsections));     
    // Insert the new item     
    cf->sections[cf->numsections] = *sec; // Since arrays start at 0     
    cf->numsections = cf->numsections + 1;     
} 

您则需要调整呼叫

这篇关于的C指针和内存分配:realloc的数组和指针传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆