0

I cant seem to solve this error i have in the malloc line of code. The error is "SIGABRT". Please teach me how to solve this problem. Thank you.

typedef struct caminho{
       int nCient;
       struct caminho *next;
}Caminho;
Caminho *temp1 = (Caminho*) malloc(sizeof(Caminho));

Update:

typedef struct caminho{
       int nCient;
       struct caminho *next;
}Caminho;
Caminho *temp1 = malloc(sizeof(Caminho));

the only other structure in the program and the only other malloc:

typedef struct Cientista{
    int nCient;
    int nSignal;
    int profundidade;
    int distancia;
    struct caminho *next;
} cientista;

cientista* vectorCientistas;
scanf("%d %d", &maxCientista, &maxCaminhos);


vectorCientistas = malloc(sizeof(cientista*) * maxCientista);
  • First of all, try renaming the variable you're using (`}caminho;`) to something different. – ForceBru Mar 13 '15 at 16:49
  • I had a diferent name. the caminho you are refering to was Caminho. Thank for your asnwer =D – Andre Pimenta de Castro Mar 13 '15 at 17:00
  • Anyway i changed the name back to a diferent one. – Andre Pimenta de Castro Mar 13 '15 at 17:03
  • your error is not here. btw, don't cast malloc http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Jason Hu Mar 13 '15 at 17:10
  • Thank you for your asnwer. But if it is not here, and the gdb told me it stopped at this line, what tool should i use to find the error? – Andre Pimenta de Castro Mar 13 '15 at 17:14
  • actually it seems malloc will send a SIGABRT: http://stackoverflow.com/questions/3413166/when-does-a-process-get-sigabrt-signal-6. is your app multithreaded? – Jason Hu Mar 13 '15 at 17:17
  • or maybe you doubly freed a section. try to check how you corrupted the heap. – Jason Hu Mar 13 '15 at 17:20
  • No. What im trying to create is a program wich prepares a graph trough a certain input to be able to run BFS on that same graph. – Andre Pimenta de Castro Mar 13 '15 at 17:21
  • I have to learn how to find out how i corrupted the heap. Thank you HuStmpHrrr. – Andre Pimenta de Castro Mar 13 '15 at 17:25
  • As far as i could understand. It is caused by (putting it very simply) acessing memory you shouldnt be acessing. either by not being allocated, or by making a free of the memory u are trying to acess. If im wrong wich i problably am pls say. In my code i have no free functions since i use a very limited amount of memory. @HuStmpHrrr – Andre Pimenta de Castro Mar 13 '15 at 17:38
  • If `malloc()` is SIGABRT-ing, then it's most likely an issue somewhere earlier in your program that has corrupted the data structures that `malloc()`, `free()`, etc. use to track what memory has been allocated or not... – twalberg Mar 13 '15 at 17:53
  • did you see a segfault? if you are accessing memory pages that you are not supposed to access, this will happen. if not, then it's quite internal, it would just be a heap corruption. try to have a clean app with single line of malloc to see if the lib has problem, then try to use valgrind. – Jason Hu Mar 13 '15 at 18:22
  • I've donne wat @HuStmpHrrr said. Created all over again in a new file.c single malloc and the struct. It worked. So it means that twalberg was rigth, the only problem is where... that is the first time i use the Caminhos struct. I early in the programm use malloc to allocate memory for a vector of another kind of structs. Do u think that can interfere? – Andre Pimenta de Castro Mar 13 '15 at 18:34
  • always good to show the code. maybe you can show the code you think has something to do with it. – Jason Hu Mar 13 '15 at 18:38
  • @AndrePimentadeCastro `valgrind` can be useful to help track down out-of-bounds accesses and other similar types of errors; you may want to look into it. There are other tools as well, but that's one of the more common ones... – twalberg Mar 13 '15 at 18:40
  • @HuStmpHrrr i didnt want to spam code thats why i didnt put it out there. – Andre Pimenta de Castro Mar 13 '15 at 18:42
  • @twalberg i never used that tool. Thank you for your suggestion =) – Andre Pimenta de Castro Mar 13 '15 at 18:43

1 Answers1

0

PROBLEM FOUND!! had:

vectorCientistas = malloc(sizeof(cientista*) * maxCientista);

solution:

vectorCientistas = malloc(sizeof(cientista) * maxCientista;

Was allocating memory for a pointer and i wanted a struct.