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

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -