-1

I do not understand what is causing the memory access error here.

I made this simple example which shows my 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 to be changed to get it to run? This is gcc on Linux.

Peter Varo
  • 11,726
  • 7
  • 55
  • 77
Georg
  • 119
  • 1
  • 10
  • 2
    What is `new_canvas` pointing at? – Oliver Charlesworth May 17 '14 at 10:57
  • 3
    This won't compile as C++. Are you sure you're not coding in C instead? – Mat May 17 '14 at 10:57
  • I highly recommend you to use `main` as a function which returns an integer, like: `int main(void)`... – Peter Varo May 17 '14 at 11:02
  • @PeterVaro In C, leaving off the return type actually gives you a function that returns an int. Poor general practice, but that's the way the spec rolls, so making the change you suggest wouldn't change anything about the code. – mah May 17 '14 at 11:14
  • @mah yepp 1) my suggestion has nothing to do with question, yes, but 2) afaik it is the standard to do it that way. Only very old C codes didn't use integer as a return value of main, expected it would be anyway. – Peter Varo May 17 '14 at 11:16
  • @PeterVaro: If nothing is specified as a function's return-type `int` is assumed. – alk May 17 '14 at 11:27
  • 2
    Not anymore, implicit int is not in the current language spec, C99. @alk – Cody Gray - on strike May 17 '14 at 11:29

3 Answers3

3

You have never initialized new_canvas. Most likely, you want to do this:

struct mycanvas new_canvas;
new_canvas.pixels=testchunk;
mrks
  • 8,033
  • 1
  • 33
  • 62
  • I thought I could initialize it with this statement: "struct mycanvas* new_canvas;" This makes new_canvas a pointer and therefore the next statement is: "new_canvas->pixels=testchunk;" – Georg May 17 '14 at 11:05
  • 3
    @Georg making a pointer isn't enough. You have to make it point somewhere valid before you use what it points at. – mah May 17 '14 at 11:12
0
#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 everything is ok

return 0;
}
user3616181
  • 975
  • 2
  • 8
  • 27
-1

I guess I figured it out now:

#include <stdlib.h>

typedef struct mycanvas {
    void *pixels;
} mycanvas;

main(){
    void* testchunk;  
    testchunk = (void*) calloc (1024 * 768 * 4,sizeof(char));

    struct mycanvas* new_canvas=(mycanvas*)malloc(sizeof(mycanvas));

    new_canvas->pixels=testchunk; 
}

Thank you for your help! By the way, since I use cpp I could use "new" instead of malloc.

Georg
  • 119
  • 1
  • 10
  • This is worse than the version with `struct mycanvas new_canvas; struct mycanvas *new_canvas2 = &new_canvas;`. In this version you have to `free` afterwards but in that version you didn't. – M.M May 17 '14 at 12:36
  • This code is illegal in C++ because you did not declare a return type for `main`. Also you said in the initial post that you use gcc (not g++). – M.M May 17 '14 at 12:37
  • You are clearly *not* using C+. If you were, you'd include ``, you wouldn't typedef your structs, you wouldn't have to use struct in the declaration of variables, you wouldn't have to cast the result of `malloc` and `calloc`, you'd need a return type for `main`, and probably more stuff I'm missing. You are clearly writing C code. If you're *hoping* to write C++, you're doing it all wrong. Definitely pick up [a good book on the language](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Cody Gray - on strike May 18 '14 at 00:16