Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Last Compiler Warning

Status
Not open for further replies.

WayneRyan

Programmer
May 23, 2002
202
US
Hi again,

I recently posted a couple requests to help
with compile errors. Well, I have one last
one to get rid of.

The following is the start of a function
and it gets the warning on the last line.

/* ***************************************** */
void parse_how_parm(struct load_how *how)
{ unsigned short tlp;
union howdef_type *howdef;
unsigned short temp;
Boolean hdw_flag = FALSE;

switch (asts_reg9)
{ case 0:
if (!how00_rcvd_flag) how00_rcvd_flag = 1;
howdef->how_ld = *how;
/* ***************************************** */

Warning: C4700
Message: local variable 'howdef' used without
having been initialized

The code works, but I must get rid of this error.

The variable howdef is a structure of type
howdef_type which is comprised of several
other structures.

howdef->how_ld is a structure of 3 unsigned shorts.
*how is a pointer to three unsigned shorts.

How do I make the compiler happy?

Thanks,
Wayne
 
Wayne,

I suppose asts_reg9 will not be zero (the case 0 is not executed), otherwise there would almost certainly be an access violation error.

What is going on is this:

- you declare a pointer (howdef), but you do not initialize it, nor do you assign a value to it. Its contents is undefined and it may or may not (this is most likely) point to valid memory.
- You assign a value to the memory, pointed to by a pointer containing random data.

This is what the compiler warns you for. You must assign a legal value to howdef (howdef = ...;) before the statement causing the warning.
This can also be done in the declaration:
union howdef_type *howdef = ...; Marcel
 
union howdef_type *howdef = NULL;

That'll do it.

However, you dont assign it but you use it. That is what the compiler has issues with. If you know what you are going to assign it to, replace null with that. With the current suggestion of NULL, you code will crash unless you left some code out.

In either case, you must have it assigned to a valid address before you start using it. Yes it will "theoretically" work, but you never know what you have.
 
Thanks Marcel and Zyrenthian,

So this line:

union howdef_type *howdef;

Creates a pointer to a structure of howdef_type.
It does not create the structure.
It does not have an initial value.
What does the union do? It appears to be "superimposing" a
random pointer to a structure with the definition of
a structure.

Do I have this right.

Thanks again,
Wayne
 
union is a type that can be though of somewhat like a struct but it is
1. Only as large as its largest member
2. Can only have one of its n members set

union u{
char c;
short s;
int i;
float f;
double d;
};

sizeof(u) will return either 4 or 8 depending on platform but most likely 8 which is equivalent of sizeof(double)

If you want to inspect it... declare this union and set "c". then output each member. They will all have values eqivalent to "c" in some way. You may need to print em out in hex to see it due to padding issues and endianism.

unions were mainly used to save space in memory in the old days when Bill Gates said something along the lines of "256k... No computer will ever need more then that". Almost as good as his BSD durring the presentation of Win98.

Anyhoo, that is the gist of the union. An application of it is usually a union within a struct where the struct contains some member to let you know how to access the union.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top