2012-10-11 31 views
0

你好,我试图编译我的代码,但我得到了访问冲突错误。我试图制定一个议程,我可以使用列表插入值。我的代码中有什么错误?C++数据结构列表,我的代码中的错误

#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; 
} 

的输出是:
[blankline]
TEST1
TEST2

+2

在调试器中运行它,查看程序在哪一行崩溃并在此处发布。 – piokuc

+0

除了不正确的'malloc',请看'std :: list'(或'std :: forward_list')。不需要在C++中重新创建轮子和手工链接列表。 –

回答

1

访问冲突可能来自您致电insere,这不正常工作,就像您认为的那样。

int main(){ 
    ap_agenda agenda; //<-- local variable lives on the stack 

    init(&agenda); //<-- passes the address of the local variable 

当这传递给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 
    } 
} 

注意,当这个返回,nome和堆栈变量agendatelefone没有被初始化。

imprime(&agenda); 

当堆栈的变量的agenda地址被传递到imprime它试图打印尚未初始化的name值。

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

相反,如果你在agendaproximo件,其在insere初始化你会看到打印的name值传递。

imprime(agenda->proximo); 

但是,正如其他人指出的,这段代码还有很多其他问题。

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

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

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

将解决你的问题,我想

1

的多个错误 - 首先,你不是在写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++书!

+0

同意,这里是一些由SO社区推荐的书籍链接http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – andre

4

很高兴看到实际的编译器错误,以查看哪条线路导致问题。

没有编译器的输出,我可以猜测的问题是与

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

这大概应该是

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

,或者甚至更好,

p = new ap_agenda; 

,因为在现在,你只有malloc()足够大的指针,而不是实际的结构。