0

Hello i tried to compile my code but i get acess violation error. Im trying to make an agenda that i can insert values using a list. What is the error in my code?

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

Thanks !

Hello, thanks for the answers! I changed my code and now its "working" but when i try to print the list, its jump one line.

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

The output is:
[blankline]
test1
test2

  • 2
    Run it in a debugger, see in which line the program crashes and post it here. – piokuc Oct 11 '12 at 14:36
  • Besides the incorrect `malloc`, look into `std::list` (or `std::forward_list`). There is no need to reinvent the wheel and hand-implement linked lists in C++. – Daniel Gehriger Oct 11 '12 at 14:46

4 Answers4

4

It would be nice to see the actual compiler error to see which line is causing the problem.

Without the compiler output, I might guess that the issue is with

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

which should probably be

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

or, even better,

p = new ap_agenda;

because, at the moment, you are only malloc()ing enough size for a pointer, not for the actual struct.

Dave Rager
  • 8,002
  • 3
  • 33
  • 52
user124850
  • 1,392
  • 1
  • 9
  • 3
1

Multiple errors - first off you're not writing C++ code, but C code.

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

initializes to NULL the temporary lista. Outside, lista doesn't change.

That aside:

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

allocates memory only the size of a pointer, not the object. And you use malloc instead of new. Awful.

You also never free the memory.

Read a good C++ book!!!

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Agreed, here is a link some recommended books by the SO community http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – andre Oct 11 '12 at 14:50
0
 p = (ap_agenda*) malloc(sizeof(ap_agenda*));

here you allocate the size of the pointer, not of the structure !!! so any access to p->xxx may lead to a memory access error.

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

would solve your problem i guess

dweeves
  • 5,525
  • 22
  • 28
0

The access violation probably comes from your call to insere which isn't working like you think it does.

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

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

When this is passed to 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.

At this point agenda had not been modified or initialized in any way. Now you pass the address of agenda off to insere.

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

insere is defined

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

Note that when this returns, nome and telefone of the stack variable agenda have not been initialized.

    imprime(&agenda);

When the address of the stack variable agenda is passed to imprime it tries to print the value of name which has not been initialized.

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

If instead, you passed in the proximo member of agenda which was initialized in insere you would see the name value printed.

    imprime(agenda->proximo);

However, as others have pointed out, there are a lot of other issues in this code.

Dave Rager
  • 8,002
  • 3
  • 33
  • 52