OT: What causes the Segfault in the following?

Andrew Edwards via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 3 18:12:29 PDT 2017


Andrew Edwards wrote:
> int main()
> {
>     //int wierd[4];
>     struct nk_color str = nk_rgba_hex("#deadbeef");
>     //int wierd[4];
>     char *s;
>     //int wierd[4];
>     nk_color_hex_rgb(s, str);
>     //int wierd[4];
>     printf("(%d,%d,%d)\n",str.r, str.g, str.b);
>     //int wierd[4];
>     printf("%s\n", s);
>     //int wierd[4];
>     return 0;
> }
>
> The above produces as its output:
>
> (222,173,190)
> DEADBE
>
> but if I introduce an int array on any of the commented lines, it
> results in a runtime Segmentation fault: 11. Basically I'm just trying
> to port Nuklear[1] to D as a first project after reading Ali and Mike's
> awesome books. Moving one function at a time to an independent C file,
> compiling it to observe the result and then from the understanding
> gained, re-implementing it in D. The int array here is introduced to
> prepare for port of the next function and has nothing to do with the
> current content of main(), but I'm completely confused because I don't
> see anything wrong with the code that is causing the error.
>
> By the way, the program responds differently based on the type of the
> array:
>
>     double => Bus error: 10
>     char => no error
>     short => Segmentation fault: 11
>     long => Bus error: 10
>
> Obviously I'm missing something... please enlighten me.
>
> Thanks,
> Andrew
>
> [1] Yes, I know there is already a port and bindings available... this
> is purely for learning. My goal is actually to port imgui for a project
> I committed myself to at DConf2017 but in the process of doing so, I
> realized how woefully inadequate my knowledge of programming is. This is
> my attempt at rectifying the situation.

Just in case... here are the two functions being called in main():

https://github.com/vurtun/nuklear/blob/master/nuklear.h#L5695-L5722


More information about the Digitalmars-d-learn mailing list