1 Attachment(s)
[c] segmentation fault- request help
Was reading c guides on dynamic memory allocation functions.. and tried to write a program to create a linked list, to back up the knowledge... and i failed miserably :p can't figure out why it keeps giving me segmentation faults >.<
i figured the scanf is the cause.. but don't understand why ;o
I'd appreciate if someone could point out whats wrong
here's a piece of the code to blame : (i attached the full source code just in case)
Code:
// new entry and head are both links to struct record type ofc
new_entry=(struct record*)malloc(sizeof(struct record));
printf("Name:\n");
scanf("%s",new_entry->name);
printf("Age:\n");
scanf("%i",new_entry->age); // <<-- is this correct?
new_entry->next=NULL;
head=new_entry;
Re: [c] segmentation fault- request help
I have no idea what causes the mentioned error, but what's your struct record like? What I mean is, do the variables, especially name, have any memory reserved?
Re: [c] segmentation fault- request help
sorry, missed out the upper part of the full source code somehow lol, here it is :
Code:
#include <stdio.h>
#include <stdlib.h>
struct record{
char name[15];
int age;
struct record *next;
};
Re: [c] segmentation fault- request help
I think you're passing the latter scanf an int instead of int*
Re: [c] segmentation fault- request help
Ya, i figured my scanf's passing argument is wrong.. but i just couldn't figure how to correct it ;/
if i tried giving it a & thingy to "age".. would give me "error: syntax error before '&' token"
Re: [c] segmentation fault- request help
Re: [c] segmentation fault- request help
bleh, this code really IS a miserable failure..the & operator did fix that prob... i think lol.. but then it just bumped into the next one...and i am even more clueless now .. it skips the scanf for the answer line.. and makes the while loop endless.. wth >.<
to to make it short :
Code:
while(answer!='n'){
if(!head){
blah blah blah;
}
else{
yada yada yada;
}
printf("Another entry? [y/n] \n");
scanf("%c",&answer);
}
once i complete the first entry..it just prints the above , skips the answer scan and proceeds asking the name/age for the next entry.. endlessly ;/
+ more segmentation faults await ahead lol..screw this forsaken code! ;p
Thanks for your help and time btw =)
Guess i'll move on .. gonna try to learn the getopt_long function ...
Re: [c] segmentation fault- request help
You say it skips scanf, but does it also skip printf? If it doesn't I'm really amazed why it behaves like that..
I told you to use a debugger!
Quote:
Originally Posted by
Negata
now if I have a struct A with a member B, an a of type A*, why is a->B not correct? That's exactly the case here and exactly what is said on the page you referred to.
I think it has something to do with scanf expecting the address of a struct member instead of its value. Anyway &(a->B) is the correct precedence, not (&a)->B.
P.S. I seem to have found your problem:
Code:
@004010EF>u,1
004010EF E9 68FFFFFF jmp 0040105C
@004010EF>s
@0040105C>gr MSVCRT.scanf l 3 r
Name:
longcat
100000
@0040109F>u,15
0040109F FF75 00 push dword ptr ss:[ebp]
004010A2 53 push ebx
004010A3 68 30104000 push 00401030
004010A8 FFD7 call edi
004010AA 53 push ebx
004010AB FF15 0C104000 call dword ptr ds:[MSVCRT.free]
004010B1 68 18104000 push 00401018
004010B6 FFD7 call edi
004010B8 8D4424 3F lea eax, dword ptr ss:[esp+3F]
004010BC 50 push eax
004010BD 68 14104000 push 00401014
004010C2 FFD6 call esi
004010C4 83C4 34 add esp, 34
004010C7 807C24 13 6E cmp byte ptr ss:[esp+13], 6E
004010CC 74 1B jz 004010E9
@0040109F>g 4010b8
longcat is 100000 years old
Another entry?[y/n]
@004010B8>s
@004010BC>ra
EAX : 0063FE3B
@004010BC>d 63fe3b l 1
0063FE30 79 y
@004010BC>s
@004010BD>s
@004010C2>p
@004010C4>d 63fe3b l 1
0063FE30 0A .
@004010C4>q
The carriage return from the previous input was not picked up by a previous scanf() so it becomes the input of this.
Re: [c] segmentation fault- request help
Eek.. doesn't look/sound like something i can comprehend for now, thanks though :p
Have to put C aside for a bit... got "algorithms and datastructures" exam approaching.. gonna be reading material for it