【问题标题】:C++ data structure list, erro in my codeC++ 数据结构列表,我的代码中的错误
【发布时间】:2012-10-11 14:32:48
【问题描述】:

您好,我尝试编译我的代码,但出现访问冲突错误。我试图制定一个议程,我可以使用列表插入值。我的代码有什么错误?

#include <stdio.h>
#include <iostream>

using namespace std;

typedef struct ap_agenda{
    char *name;
    char *telefone;
    struct ap_agenda *proximo;
};

void init(ap_agenda* lista){
    lista = NULL;
}

void insere(char *nome, char *telefone, ap_agenda* lista){
    ap_agenda *p;
    p = (ap_agenda*) malloc(sizeof(ap_agenda*));
    p->name = nome;
    p->telefone = telefone;

    if(lista == NULL){
        lista = p;
    }else{
        lista->proximo = p;
    }
}

void imprime(ap_agenda *lista){
    cout << lista[0].name << endl;
}

int main(){
    ap_agenda agenda;

    init(&agenda);
    insere("test","123456",&agenda);
    imprime(&agenda);

    system("pause");
}

谢谢!

您好,感谢您的回答!我更改了我的代码,现在它“工作”了,但是当我尝试打印列表时,它跳了一行。

void insere(std::string nome, std::string telefone, ap_agenda* lista){
ap_agenda *p = new ap_agenda;

p->name = nome;
p->telefone = telefone;
p->proximo = NULL;

if(lista == NULL){
    lista = p;
}else{
    while(lista->proximo != NULL)
        lista = lista->proximo;

    lista->proximo =  p;
    }
}

void print(ap_agenda* lista){
    ap_agenda *p;
    for(p=lista; p!=NULL; p=p->proximo)
        cout << p->name.c_str() << endl;
}

输出是:
[空白电话]
测试1
测试2

【问题讨论】:

  • 在调试器中运行,看看程序在哪一行崩溃,贴在这里。
  • 除了不正确的malloc,查看std::list(或std::forward_list)。无需重新发明轮子并在 C++ 中手动实现链表。

标签: c++ list data-structures


【解决方案1】:

很高兴看到实际的编译器错误,看看是哪一行导致了问题。

如果没有编译器输出,我可能会猜测问题出在

p = (ap_agenda*) malloc(sizeof(ap_agenda*));

应该是

p = (ap_agenda*) malloc(sizeof(ap_agenda));

或者,甚至更好,

p = new ap_agenda;

因为,目前,您只是 malloc() 为指针提供了足够的大小,而不是为实际的结构。

【讨论】:

    【解决方案2】:

    多个错误 - 首先你不是在编写 C++ 代码,而是 C 代码。

    void init(ap_agenda* lista){
        lista = NULL;
    }
    

    初始化为NULL 临时lista。在外面,lista 没有改变。

    除此之外:

    ap_agenda *p;
    p = (ap_agenda*) malloc(sizeof(ap_agenda*));
    

    只分配指针大小的内存,而不是对象。你使用malloc 而不是new。太可怕了。

    你也永远不会释放内存。

    阅读一本好的 C++ 书籍!!!

    【讨论】:

    【解决方案3】:

    访问冲突可能来自您对insere 的调用,它没有像您想象的那样工作。

    int main(){
        ap_agenda agenda; //<-- local variable lives on the stack
    
        init(&agenda); //<-- passes the address of the local variable
    

    当 this 被传递给 init 时:

    void init(ap_agenda* lista){ // lista is a temporary variable that contains a
                                 // copy of the address
        lista = NULL; //<-- this overwrites the value in the temporary variable.
    } // when this function returns, the temporary variable is destroyed.
    

    此时agenda 尚未以任何方式修改或初始化。现在您将agenda 的地址传递给insere

        insere("test","123456",&agenda);
    

    insere 已定义

    void insere(char *nome, char *telefone, ap_agenda* lista){
        ap_agenda *p;
        p = (ap_agenda*) malloc(sizeof(ap_agenda*)); // you allocate a new `ap_agenda`
                                                     // pointer. not enough for a struct
        p->name = nome; // initialize name (probably ok but not what you expect)
        p->telefone = telefone;  // initialize telefone (possible access violation)
    
        if(lista == NULL){ // since lista is the address of a stack variable it won't
                           // be NULL here
            lista = p;
        }else{
            lista->proximo = p; // this sets the allocated struct to the `proximo` member
                                // of the stack variable that was passed in
        }
    }
    

    注意当这个返回时,堆栈变量agendanometelefone还没有被初始化。

        imprime(&agenda);
    

    当堆栈变量agenda 的地址传递给imprime 时,它会尝试打印尚未初始化的name 的值。

    void imprime(ap_agenda *lista){
        cout << lista[0].name << endl; // possible access violation
    }
    

    如果相反,您传入agendaproximo 成员,该成员在insere 中初始化,您将看到打印的name 值。

        imprime(agenda->proximo);
    

    但是,正如其他人指出的那样,此代码中还有很多其他问题。

    【讨论】:

      【解决方案4】:
       p = (ap_agenda*) malloc(sizeof(ap_agenda*));
      

      在这里您分配指针的大小,而不是结构的大小!!!! 所以任何对 p->xxx 的访问都可能导致内存访问错误。

      p = (ap_agenda*) malloc(sizeof(ap_agenda));
      

      我猜会解决你的问题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-13
        • 1970-01-01
        相关资源
        最近更新 更多