
Looking at this piece of code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _point{
int x;
int y;
} point;
int main (void)
{
point *ptr;
point obj;
ptr = (point *)malloc(sizeof(char));
obj.x = 500;
obj.y = 5000;
memcpy((void *)ptr, (void *)&obj, sizeof(point));
printf("point.x = %d, point.y = %d\n", ptr->x, ptr->y);
return 0;
}
Why is it running correctly when I run it. Shouldn't I get a segfault given I incorrectly do malloc() on sizeof(char) instead of sizeof(point)?
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
