0

I am trying to code RGB values into a struct and it is working fine but I get an exception saying my struct was nullptr when I try to run a function that accesses that struct. Here is the code:

struct Color {
    unsigned char R;
    unsigned char G;
    unsigned char B;
}*blue, *red, *green, *yellow, *purple, *pink, *brown;

void CreateColors()
{
    blue->R = 0;
    blue->G = 0;
    blue->B = 255;

    red->R = 255;
    red->G = 0;
    red->B = 0;

    green->R = 0;
    green->G = 255;
    green->B = 0;

    yellow->R = 255;
    yellow->G = 255;
    yellow->B = 0;

    purple->R = 133;
    purple->G = 87;
    purple->B = 168;

    pink->R = 255;
    pink->G = 0;
    pink->B = 191;

    brown->R = 168;
    brown->G = 130;
    brown->B = 87;

}

If you guys could tell me what I am doing wrong or a better data structure to use that would be great. I am TA'ing a cpp class and am trying to subtly start using pointers to warm them up. Thanks again.

Also when I try to use these values like so:

if (deg >= 0 && deg < boundaries[COAL_B])
{
    pixels[i][j][0] = blue->R;
    pixels[i][j][1] = blue->G;
    pixels[i][j][2] = blue->B;
}

It throws the same error.

7
  • 3
    You are not allocating any space for the structures. Commented Oct 18, 2018 at 16:27
  • 2
    " I am TA'ing a cpp class and am trying to subtly start using pointers to warm them up" - this looks like an awful way of doing that, and it seems you don't have the minimal knowledge needed to teach C++ (or C). Commented Oct 18, 2018 at 16:28
  • 1
    Not to be rude but if you are going to TA a C++ class you really should understand C++. Commented Oct 18, 2018 at 16:29
  • I'm sorry I thought when you declare a pointer of a certain data type it allocates enough memory to store that pointer. Are you saying I should use new and manually allocate memory? @EugeneSh. Commented Oct 18, 2018 at 16:30
  • 2
    We are saying you might want to read a book on C++ (or C) before attempting to teach it. Commented Oct 18, 2018 at 16:31

2 Answers 2

5

You're creating a series of pointers to struct Color, but you're not initializing any of them, so because they're defined at file scope they are all set to NULL. You then attempt to dereference those NULL pointers. This invokes undefined behavior.

You should instead define instances of these structs instead of pointers:

struct Color {
    unsigned char R;
    unsigned char G;
    unsigned char B;
} blue, red, green, yellow, purple, pink, brown;

Then access them as instances:

void CreateColors()
{
    blue.R = 0;
    blue.G = 0;
    blue.B = 255;

    red.R = 255;
    red.G = 0;
    red.B = 0;

    ...

Better yet, get rid of the initialization function and initialize them at the point they're defined:

struct Color {
    unsigned char R;
    unsigned char G;
    unsigned char B;
};

struct Color blue = { 0, 0, 255}, red = { 255, 0, 0 }, green = { 0, 255, 0}, ...
Sign up to request clarification or add additional context in comments.

4 Comments

"You should instead define instances of these structs i" - really don't think he should be declaring global instances of these structs either.
nah global instance of this struct is exactly what i need in this case. Thank you for answering my question.
Also, when using structs the default behavior when initializing them is to fill up the variables in the order they are in the struct? I kinda jumped to classes so I don't really know the details of struct behavior.
@MaxAttax Yes, fields are initialized in the order they are defined in. Also, feel free to accept this answer if you found it useful.
0

You declare a pointer to a structure but never allocate memory for it. When you access it's members, you're dereferencing a null pointer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.