Re: Segmentation fault (Assignment 1, part 2)

121 views
Skip to first unread message
Message has been deleted

Arrvindh Shriraman

unread,
Sep 20, 2020, 1:21:11 PM9/20/20
to cmpt-295-sfu
1 your github link is wrong
2. You bug is not due to the for loop. It could be due to how you use it.


On Saturday, September 19, 2020 at 9:02:00 PM UTC-7, Shiyuan Miao wrote:
SFU ID: 301415720
Github username: sean698
Expected behavior: Trying to eliminate the first element in the pre[] array.

char pre[] = 'abc', and I am trying to eliminate the first element. When pre[i]=pre[i+1] I got a segmentation fault. I am so confused. The following is my code in the build_tree() function.

  for(int i = 0; i < 2; i++) {
    pre[i]=pre[i+1];
  }

Ryan Tio

unread,
Sep 20, 2020, 2:47:02 PM9/20/20
to cmpt-295-sfu
Don't you have to declare the variable before you use it?
int i;
for(i = 0; i < 2; ++i){
...

Arrvindh Shriraman

unread,
Sep 20, 2020, 8:37:54 PM9/20/20
to cmpt-295-sfu
C99 supports declaration of i within for-loop and gcc supports C99.
Another compiler may complain; either way that would be a compile-time error, not a segfault.
Reply all
Reply to author
Forward
0 new messages