c - What is causing memory access error here? -
i not understand causing memory access error here.
i made simple example shows problem:
#include <stdlib.h> typedef struct mycanvas { void *pixels; } mycanvas; main() { void* testchunk; testchunk = (void*) calloc (1024 * 768 * 4,sizeof(char)); struct mycanvas* new_canvas; new_canvas->pixels=testchunk; //causes memory access error }
what needs changed run? gcc on linux.
#include <stdlib.h> typedef struct mycanvas { void *pixels; } mycanvas; int main() { void* testchunk; testchunk = (void*) calloc (1024 * 768 * 4,sizeof(char)); mycanvas *new_canvas = malloc(sizeof(struct mycanvas)); new_canvas->pixels=testchunk; //now ok return 0; }
Comments
Post a Comment