compiler construction - C - program throwing segmentation fault -
i trying write compiler program in c (ubuntu, gcc) using jack crenshaw's tutorial http://compilers.iecc.com/crenshaw/. however, written in pascal, , relatively new c, tried write 1 best could.
i need help. segmentation fault occuring. see valgrind's output:
==3525== invalid read of size 1 ==3525== @ 0x80484c0: getchar (in /home/spandan/codes/compiler_1) ==3525== 0x8048aad: init (in /home/spandan/codes/compiler_1) ==3525== 0x8048acd: main (in /home/spandan/codes/compiler_1) ==3525== address 0x0 not stack'd, malloc'd or (recently) free'd ==3525== ==3525== ==3525== process terminating default action of signal 11 (sigsegv) ==3525== access not within mapped region @ address 0x0 ==3525== @ 0x80484c0: getchar (in /home/spandan/codes/compiler_1) ==3525== 0x8048aad: init (in /home/spandan/codes/compiler_1) ==3525== 0x8048acd: main (in /home/spandan/codes/compiler_1)
i post part of code here relevant valgrind's stack trace. rest can found at: http://pastebin.com/kbhyrc1n.
please explanation. according me pointers correctly used. want provide command-line input program, if don't, still segfaulting.
#include<stdio.h> #include<stdlib.h> static char *look; static int lookp = 0; //read new character input stream char getchar(){ char x; x= look[lookp]; lookp++; return x; } // initializer function void init(char *c){ look=c; getchar(); //skipwhite(); } int main(int argc, char *argv){ init(argv[1]); //assignment(); if (look[lookp] != '\r'){ // expected('newline'); } return 0; }
the signature of main()
wrong. should int main(int argc, char **argv){
(add 1 more *
before argv
)
also should check number of command line arguments before using them.
Comments
Post a Comment