Assign value to member of struct using double pointer

tzippy

I have a function that takes a double pointer to a struct and assigns a value. However I get an "access violation writing location ..." when trying to access the member member1. This is my code:

struct mystruct{
  unsigned int member1;
  void * data;
};

int main(){

  mystruct **foo = new mystruct *;
  bar(foo);

}

void bar(unsigned int val, mystruct ** foo)
{
    (*foo)->member1 = val;
}
dhein

You just created a new mystruct pointer. That means:

You get allocated a memory block, large enough to hold a address, and assign it to the pointer which is pointing to a pointer that points to a mystruct member. That doesn't mean, that there is a valid address hold in the pointer which you expect to be pointing to a mystruct element. Even more currently there isn't even a valid address, where the pointer to the pointer is pointing on, as you just assigned a valid memory area to it, what doesn't mean there is a usefull address stored in.

So at all, what you want is:

You want a pointer which has a valid memory block to store a address in of another pointer, which is pointing to a valid memory area, where is a (probably valid) mystruct stored in.

What you are doing is: you are requesting a memory area where you COULD (what you aren't even doing) store a poitner to another pointer... and so on.

so what you should do is:

mystruct **foo = new mystruct *;
*foo = new mystruct;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unable to assign the value to structure member using double pointer

Assign a Struct Pointer to Double Pointer Struct

Assign temporary struct pointer as struct member

reflect, assign a pointer struct value

Initialize matrix (double pointer) member of struct

Setting value to a double struct pointer

c++ how to assign NULL value to a member pointer when creating new struct

Using free with double pointer to a struct

Pass callable object by value, assign it to pointer member

Assigning value to the address of member of a struct pointer

Golang: Assigning a value to struct member that is a pointer

Assign string value to property in pointer to struct

How to assign a value to a golang struct pointer?

Can't assign char value to pointer in struct

Assign a pointer to a struct that is a member of a struct of the same type to another pointer to a struct of the same type

In C, I'm having trouble getting the pointed value of a member struct(a double pointer). The value is lost outside the function assigning the value

Pointer to pointer member Struct

copy with memcpy structs double pointer member content to another struct

Why did coder assign value of struct pointer to a static struct?

How to free a tree struct using double pointer?

Pointer and Struct member function

How to correctly reference a pointer within a struct using a double pointer

Assign return value to struct member with shorthand assignment/declaration

Pybind11: How to assign default value for a struct member variable?

double pointer to struct inside struct

Double pointer to struct

how to assign value to nested struct using reflect

Is it possible to assign a entire struct using a pointer and one statement

Returning the value of a class member pointer variable using 'this' pointer