shallow copy struct got weird result on Xcode

the code here is a typical example for shallow copy, it ought to got the warning when running, but on my Xcode, its running well and result is

hi,hi
hi,hi

why?

The code:

struct pc
{
    char *p;
};

int main()
{
    struct pc pc1;
    pc1.p = malloc(100);
    strcpy(pc1.p, "hi");
    struct pc pc2 = pc1; // shallow copy
    printf("%s,%sn", pc1.p, pc2.p);
    free(pc1.p);
    printf("%s,%s", pc1.p, pc2.p);
    return 0;
}


Source: c#

Leave a Reply