Jump to content

Whenever I try to run my code I get error C4700, uninitialized local variable "role" used. The error points to the line (role->title = newValue;). My struct has to be setup to use pointers although this is the first time I have had to do it (or use structs). I thought I had everything coded correctly. It doesn't produce any errors until I try to built it in Visual Studio. Just sitting in the IDE it thinks it's just fine. I don't understand what I have to do, I guess it wants me to initialize *role to a known value? But I really don't know how to do that, and I didn't think you had to. Anyways I'm really lost and could use some help, thank you.

struct structureType //declared globally
{
	string title;
	int year;
	string task;
};

////rest of code
structureType *role;

//code that will produce title (aka newValue)
role->title = newValue;
//code that will produce year (aka newValue)
role->year = newValue;
//code that will produce task (aka newValue)
role->task = newValue;

//continuing code

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
https://linustechtips.com/topic/603885-help-initializing-a-pointer-structure/
Share on other sites

Link to post
Share on other sites

don't you have to go something like role = new structureType or something like that after you create it but before you assign values?

 

Or, you could just change that line to "structureType role;" without the star... i think that would do it, but you'd be doing things a little differently

Solve your own audio issues  |  First Steps with RPi 3  |  Humidity & Condensation  |  Sleep & Hibernation  |  Overclocking RAM  |  Making Backups  |  Displays  |  4K / 8K / 16K / etc.  |  Do I need 80+ Platinum?

If you can read this you're using the wrong theme.  You can change it at the bottom.

Link to post
Share on other sites

2 minutes ago, Ryan_Vickers said:

don't you have to go something like role = new structureType or something like that after you create it but before you assign values?

Yeah...so that worked. What the hell does that do? I don't get the purpose behind it. I thought after this line:

 

structureType *role;

 

You can start assigning values to the elements in the struct. 

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to post
Share on other sites

1 minute ago, Spev said:

Yeah...so that worked. What the hell does that do? I don't get the purpose behind it. I thought after this line:

 


structureType *role;

 

You can start assigning values to the elements in the struct. 

So that line there ^^ just creates basically a place holder - a handle if you will, like the name of a person who doesn't exist, or the address to a house that hasn't been build.  You then need to actually create the object which is what my line did.

Solve your own audio issues  |  First Steps with RPi 3  |  Humidity & Condensation  |  Sleep & Hibernation  |  Overclocking RAM  |  Making Backups  |  Displays  |  4K / 8K / 16K / etc.  |  Do I need 80+ Platinum?

If you can read this you're using the wrong theme.  You can change it at the bottom.

Link to post
Share on other sites

13 minutes ago, Spev said:

Whenever I try to run my code I get error C4700, uninitialized local variable "role" used. The error points to the line (role->title = newValue;). My struct has to be setup to use pointers although this is the first time I have had to do it (or use structs). I thought I had everything coded correctly. It doesn't produce any errors until I try to built it in Visual Studio. Just sitting in the IDE it thinks it's just fine. I don't understand what I have to do, I guess it wants me to initialize *role to a known value? But I really don't know how to do that, and I didn't think you had to. Anyways I'm really lost and could use some help, thank you.

nvm seems it was solved

Spoiler


You'll have to allocate memory for the struct, either with calloc or malloc:

 


struct structureType *role = malloc(sizeof(struct structureType));


 

 

Link to post
Share on other sites

structureType* role; 

Creates a variable named "role" of type "structureType*". Before you use that variable you have to give it a value.

To create a new instance of your struct you use the "new" operator

structureType* role = new structureType();

This will allocate memory on the heap and call the constructor of structureType (you didn't define one so the compiler made one for you.) 

Later in the code when done with the variable you must use the "delete" operator to free the memory you allocated with "new". 

delete role;

This calls the destructor (again you didn't define one so the compiler made one for you.)  then deallocates the memory. After calling delete on an object, you can't use it anymore. If you don't call delete the memory is never freed and that part of memory won't be usable anymore (called a memory leak.) If you allocate memory you must free it.

1474412270.2748842

Link to post
Share on other sites

11 minutes ago, Spev said:

Yeah...so that worked. What the hell does that do? I don't get the purpose behind it. I thought after this line:

 


structureType *role;

 

You can start assigning values to the elements in the struct. 

Nope. The data type followed by an asterisk creates a pointer. A pointer is a 32 or 64bit (depending on target platform) integer that holds the location of a memory address. You have to assign a valid memory address to the pointer in order for it to be useful. To do this you can a couple ways: 

 

a) Allocate memory on the heap through the key word 'new'

DataType* a = new DataType;

 

b) Take the address of an object allocated on the stack with the the following syntax.

DataType b;
DataType* a = &b;

 

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×