473,387 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to "define" a struct

PLS
I have a struct containing a few private members and a bunch of
properties. When I try to use the structure like this

ABC item;
item.property1 = 1;
item.property2 = 2;
...

In total, the properties set will set every private variable.

Yet the compiler gives me "Use of unassigned local variable" in the
first property set.

I'm confused. Structs have a default constructor that sets the memory to
binary zeros, so the struct is defined.

Why do I get this error and what can I do about it?

Thanks,
++PLS

Oct 31 '07 #1
5 1837
On 2007-10-31 16:43:16 -0700, PLS <no****@nowhere.comsaid:
[...]
I'm confused. Structs have a default constructor that sets the memory to
binary zeros, so the struct is defined.
That's a false conclusion. All types have a default constructor. But
local variables don't automatically use the default constructor. The
default constructor for your struct would only be automatic if the
struct appeared, for example, in a class instance.
Why do I get this error and what can I do about it?
You can initialize your variable:

ABC item = new ABC();

Pete

Nov 1 '07 #2
PLS
Doesn't "new" create on the GC heap? I thought structs were value types
and appeared on the stack. In this case, I WANT the struct on the stack
if possible.

++PLS

In article <200710311706138930-NpOeStPeAdM@NnOwSlPiAnMkcom>,
Np*********@NnOwSlPiAnMk.com says...
On 2007-10-31 16:43:16 -0700, PLS <no****@nowhere.comsaid:
[...]
I'm confused. Structs have a default constructor that sets the memory to
binary zeros, so the struct is defined.

That's a false conclusion. All types have a default constructor. But
local variables don't automatically use the default constructor. The
default constructor for your struct would only be automatic if the
struct appeared, for example, in a class instance.
Why do I get this error and what can I do about it?

You can initialize your variable:

ABC item = new ABC();

Pete

Nov 1 '07 #3
On 2007-10-31 17:17:24 -0700, PLS <no****@nowhere.comsaid:
Doesn't "new" create on the GC heap?
No, though I share your concern. :) I've complained about this syntax
before, but no one seems to agree with me.
I thought structs were value types
and appeared on the stack. In this case, I WANT the struct on the stack
if possible.
Your struct is a value type, and like all value types it will be on the
stack when declared as a local variable.

I'm not really sure what exactly happens when you use "new" with a
value type. For sure, it runs the specified constructor; what I don't
know is whether a temporary instance is created and then copied to the
declared instance being initialized, or if the compiler is smart enough
to just initialize the declared instance.

Either way, you wind up with just your declared instance, stored
wherever is appropriate according to the declaration. In this case,
that's going to be on the stack.

Yes, it's kind of confusing. :)

Pete

Nov 1 '07 #4
Peter Duniho wrote:
On 2007-10-31 16:43:16 -0700, PLS <no****@nowhere.comsaid:
>I'm confused. Structs have a default constructor that sets the memory to
binary zeros, so the struct is defined.

That's a false conclusion. All types have a default constructor.
No. Maybe value types have.

Arne
Nov 1 '07 #5
On 2007-10-31 18:27:03 -0700, Arne Vajhøj <ar**@vajhoej.dksaid:
Peter Duniho wrote:
>On 2007-10-31 16:43:16 -0700, PLS <no****@nowhere.comsaid:
>>I'm confused. Structs have a default constructor that sets the memory to
binary zeros, so the struct is defined.

That's a false conclusion. All types have a default constructor.

No. Maybe value types have.
You're right...I didn't express myself very well there. My point is
that having a default constructor is not a sufficient condition for a
local variable being considered to be "definitely assigned" (which is
what the OP) is talking about, as even non-struct value types (int,
bool, etc.) have default constructors but aren't automatically
"definitely assigned" as a result.

Pete

Nov 1 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Hodapp | last post by:
I have seen messages posted about this before, and there is a clear reference to it in the manual, but I have been unable to find a solution. I'm on Slackware 9.1, kernel 2.6.0-test11, using...
1
by: Herve MAILLARD | last post by:
Hi, Hi have the following array public struct Type_Var { public int ID; public string Mnemonique; public DateTime Date; public float Valeur;
3
by: xudeutsch | last post by:
I have such a block in C++ #pragma pack(push,4) #define STATE_NULL 0x0000 #pragma pack(pop) and I need to convert it to C#. I want to use the "MarshalAs" attribute, but i dont know which...
4
by: xudeutsch | last post by:
thanks, Nicholas and cody. but as far as i know, the following directive " #pragma pack(push,4)" will ensure that: when the data are transferred through networks to other platforms, the const...
1
by: James | last post by:
Hi all, How do you #define (as in C++) in C#? Many thanks, James
14
by: Chen Shusheng | last post by:
CSS white here: Simply strange, I found "define" can not work with "malloc". Together my complier will say "parse error". Could anyone tell me why? ------------------------- #define MAX 10000...
17
by: Chen Shusheng | last post by:
Hi all, In fact, I want to let my memory run out. And see what will happen. My system is windowsXp. Memory is 256M.I think my cdes will apply more memory than I have. Codes are below: ...
2
by: slrj | last post by:
Can someone explain me what's going on in the code below. As per my knowledge "#define <macro name> <Macro replacement>" doesn't have a semicolon and it's terminated by a new line. But the code line...
6
by: Luna Moon | last post by:
Here is a quote from the book: C++ Annotations, I don't understand these sentences, could anybody help me? thanks! As a side effect to this implementation it must be stressed that it is not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.