-4

So, I have been spoilt by python and am trying to pick up C again. I am getting this seg fault and I think I understand the reason but not sure on why it is happening.

long head = 1; // atleast that is what I was hoping for..
long *localranks = (long*) malloc(n * sizeof(long)); // size_t n = 50;
localranks[head] = 0; // seg fault here..


printf("head is %lu %d\n", head, head);

gives head is 4294967297 1

So, while head should be 1.. I am guessing, it is being passed to that garbage value .. How do i fix this ?

frazman
  • 32,081
  • 75
  • 184
  • 269
  • 4
    Obligatory warning: [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). As for the actual bug, you should set a breakpoint on the problem line and inspect the values of `head`, `n` and `localranks`. – Paul R Sep 11 '15 at 07:06
  • @PaulR thanks.. but how? – frazman Sep 11 '15 at 07:10
  • 3
    To add to Paul's comment, make sure to check for the return value of `malloc`. From what I see, malloc must have returned `NULL` which you are trying to access. – Santosh A Sep 11 '15 at 07:10
  • 1
    It would probably give `seg fault` or would be UB when `n=1`. – Haris Sep 11 '15 at 07:12
  • @SantoshA: I am incredibly naive in C .. how do i check the return value of malloc ? – frazman Sep 11 '15 at 07:13
  • @Fraz: it depends on what tools you are using - if you're using an IDE then there's usually a simple GUI-based method to set a breakpoint and inspect variables - if you're building from the command line then use gdb. Or you could even just add some strategic `printf` statements and do your debugging "old skool". FWIW I suspect `head >= n`. – Paul R Sep 11 '15 at 07:13
  • working well on xcode 6.4 c++, maybe the moon is in the wrong place on yours. OR you are not giving the whole source code :) – ardhitama Sep 11 '15 at 07:14
  • Since `head` is a signed long, you probably should be using `%ld` instead of `%lu`. `%d` is probably fine here, for the low value of `head`. –  Sep 11 '15 at 07:16
  • 2
    Your output suggests that `long head = 1;` is not your real code. Perhaps you have some sort of calculation that you expect to generate `1` but is really generating `4294967297`. Please post *real code*. We don't have crystal balls. – M.M Sep 11 '15 at 07:29
  • if you are getting segmentation fault how `head is 4294967297 1` I mean how head is printing? – venki Sep 11 '15 at 08:30

1 Answers1

1

I am kind of not exactly able to reproduce it at all, but I suspect if seg fault is where you are pointing then probably reason should be a malloc failure, try below code and see if that's the case

 long head = 1; // atleast that is what I was hoping for..
    long *localranks = (long*) malloc(n * sizeof(long)); // size_t n = 50;
    if( localranks == NULL ) {
        printf("malloc fail ");
        exit (0);
    }
    localranks[head] = 0; // seg fault here..
    printf("head is %lu %ld\n", head, head);
asio_guy
  • 3,667
  • 2
  • 19
  • 35