understanding GUID

L

Lamefif

// The GUID is represented as a structure defined in <winnt.h>.
typedef struct_GUID{ unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];} GUID;

GUID is a 128-bit number ? but looking at the structure, i have some
questions.

Data4 is an array of char of length 8. meaning Data4 is a pointer to
an array longer then 32 bits(8 char * 8 bits = 64), which would make
a
GUID bigger then 128 bits?

is Data4 a pointer, are they usually not 4 bits?
 
V

Victor Bazarov

Lamefif said:
// The GUID is represented as a structure defined in <winnt.h>.
typedef struct_GUID{ unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];} GUID;

GUID is a 128-bit number ?

On a system with specific sizes of 'short' and 'char', probably.
but looking at the structure, i have some
questions.

Data4 is an array of char of length 8. meaning Data4 is a pointer to
an array longer then 32 bits(8 char * 8 bits = 64),

Uh... How could it be both? In the first sentence you said "Data4
is an *array* " (emphasis mine), and in the second you said "Data4 is
a pointer". AFAICS, it's an array.
which would make
a
GUID bigger then 128 bits?

Why? Suppose 'long' is 32 bits, 'short' is 16 bits, and 'char' is 8
bits. Suppose there is no padding. 32 + 16 + 16 + 8*8 => 128.
is Data4 a pointer, are they usually not 4 bits?

Data4 is NOT a pointer. It's an array.

V
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

// The GUID is represented as a structure defined in <winnt.h>.
typedef struct_GUID{ unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];} GUID;

GUID is a 128-bit number ? but looking at the structure, i have some
questions.

No, GUID is a data-structure which might take 128 bits of memory, it is
not a 128 bit number.
 
L

Lamefif

Lamefif said:
// The GUID is represented as a structure defined in <winnt.h>.
typedef struct_GUID{ unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];} GUID;
GUID is a 128-bit number ?

On a system with specific sizes of 'short' and 'char', probably.
but looking at the structure, i have some
questions.
Data4 is an array of char of length 8. meaning Data4 is a pointer to
an array longer then 32 bits(8 char * 8 bits = 64),

Uh... How could it be both? In the first sentence you said "Data4
is an *array* " (emphasis mine), and in the second you said "Data4 is
a pointer". AFAICS, it's an array.
which would make
a
GUID bigger then 128 bits?

Why? Suppose 'long' is 32 bits, 'short' is 16 bits, and 'char' is 8
bits. Suppose there is no padding. 32 + 16 + 16 + 8*8 => 128.
is Data4 a pointer, are they usually not 4 bits?

Data4 is NOT a pointer. It's an array.

Thanks Victor.

Data4 is a variable that holds the address of the first char, No?.

In other words we would need space for Data4 as well as 8 chars?
 
V

Victor Bazarov

Lamefif said:
Lamefif said:
// The GUID is represented as a structure defined in <winnt.h>.
typedef struct_GUID{ unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];} GUID;
[..]

Data4 is a variable that holds the address of the first char, No?.
No.

In other words we would need space for Data4 as well as 8 chars?

No.

V
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Lamefif said:
// The GUID is represented as a structure defined in <winnt.h>.
typedef struct_GUID{ unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];} GUID;
GUID is a 128-bit number ?

On a system with specific sizes of 'short' and 'char', probably.
but looking at the structure, i have some
questions.
Data4 is an array of char of length 8. meaning Data4 is a pointer to
an array longer then 32 bits(8 char * 8 bits = 64),

Uh... How could it be both? In the first sentence you said "Data4
is an *array* " (emphasis mine), and in the second you said "Data4 is
a pointer". AFAICS, it's an array.
which would make
a
GUID bigger then 128 bits?

Why? Suppose 'long' is 32 bits, 'short' is 16 bits, and 'char' is 8
bits. Suppose there is no padding. 32 + 16 + 16 + 8*8 => 128.
is Data4 a pointer, are they usually not 4 bits?

Data4 is NOT a pointer. It's an array.

Thanks Victor.

Data4 is a variable that holds the address of the first char, No?.

No, that would be a pointer. Data4 is an array of 8 chars, in other
words 8 chars placed one after another in memory.
 
L

Lamefif

Lamefif wrote:
// The GUID is represented as a structure defined in <winnt.h>.
typedef struct_GUID{ unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];} GUID;
GUID is a 128-bit number ?
On a system with specific sizes of 'short' and 'char', probably.
but looking at the structure, i have some
questions.
Data4 is an array of char of length 8. meaning Data4 is a pointer to
an array longer then 32 bits(8 char * 8 bits = 64),
Uh... How could it be both? In the first sentence you said "Data4
is an *array* " (emphasis mine), and in the second you said "Data4 is
a pointer". AFAICS, it's an array.
which would make
a
GUID bigger then 128 bits?
Why? Suppose 'long' is 32 bits, 'short' is 16 bits, and 'char' is 8
bits. Suppose there is no padding. 32 + 16 + 16 + 8*8 => 128.
is Data4 a pointer, are they usually not 4 bits?
Data4 is NOT a pointer. It's an array.
Thanks Victor.
Data4 is a variable that holds the address of the first char, No?.

No, that would be a pointer. Data4 is an array of 8 chars, in other
words 8 chars placed one after another in memory.

^ thanks i think i got it :) Data4 holds the first char.
 
V

Victor Bazarov

Lamefif said:
[..]
^ thanks i think i got it :) Data4 holds the first char.

No, it does not hold the first char. It holds *all eight* of them!

int main() {
char Data4[8] = { 'a', 'b', 'c', 'd' };
}

The memory allocated for Data4 is 8 bytes. The contents of the memory
are the characters with which it's initialised (plust more null chars
after the 'd').

sizeof(Data4) == 8

typeid(Data4).name() == "array of 8 char" (or something like that)

Now, if you see 'Data4' (the name of the array) used anywhere in
an *expression*, like

puts(Data4) ;
^^^^^^^^^^^ Expression

or

char blah = Data4[2] ;
^^^^^^^^ Expression

*only then* the compiler substitutes the name with _the address_ of
the first character. That address becomes the pointer used in the
expression. The concept of array names decaying to pointers to the
first element is not as trivial as some authors would like. It needs
a careful consideration to be understood.

V
 
L

Lamefif

Lamefif said:
[..]
^ thanks i think i got it :) Data4 holds the first char.

No, it does not hold the first char. It holds *all eight* of them!

int main() {
char Data4[8] = { 'a', 'b', 'c', 'd' };
}

The memory allocated for Data4 is 8 bytes. The contents of the memory
are the characters with which it's initialised (plust more null chars
after the 'd').

sizeof(Data4) == 8

typeid(Data4).name() == "array of 8 char" (or something like that)

Now, if you see 'Data4' (the name of the array) used anywhere in
an *expression*, like

puts(Data4) ;
^^^^^^^^^^^ Expression

or

char blah = Data4[2] ;
^^^^^^^^ Expression

*only then* the compiler substitutes the name with _the address_ of
the first character. That address becomes the pointer used in the
expression. The concept of array names decaying to pointers to the
first element is not as trivial as some authors would like. It needs
a careful consideration to be understood.

V


pardon my ignorance.

a char memory cell would be the address and 8 bit char value, and we
have 8 of these.

you are saying that Data4 holds all 8 cells.

how is this done?
is the computer putting aside a chunk made up of 8 cells and assigning
to this collective a single address (Data4)?

i mean these are 8 separate bytes each with they're own address right,
you cannot bug em up

i would appreciate a bit more then "It needs a careful consideration
to be understood"

thank you
 
V

Victor Bazarov

Lamefif said:
[..]
pardon my ignorance.

No problem at all (although I suspect that you're being sarcastic).
a char memory cell would be the address and 8 bit char value, and we
have 8 of these.

A memory cell is not the address. It _has_ an address.
you are saying that Data4 holds all 8 cells.

Yes. The type of 'Data4' is "an array of 8 chars". That means that
'Data4' designates the area in memory that consists of 8 memory blocks,
each contains 1 char (byte). 'Data4' is a variable name, no less but
no more.
how is this done?

How what is done? How 8 consecutive memory blocks are allocated? How
one variable can be used to identify a whole bunch of memory blocks
stringed together? That's just the C++ magic, man.
is the computer putting aside a chunk made up of 8 cells and assigning
to this collective a single address (Data4)?

Here you go, using the "a" word again... 'Data4' is an item in your
source code. If you ask for its address, the address of the first
element in the array (the type of 'Data4') is given (and, yes, all
other 7 elements follow the first one closely).
i mean these are 8 separate bytes each with they're own address right,
Right.

you cannot bug em up

I don't know what that means.
i would appreciate a bit more then "It needs a careful consideration
to be understood"

I meant the fact that 'Data4' _name_ used in an expression is converted
to a pointer requires some understanding. I did not mean the allocation
of the characters in the memory or the addressing methods used by the
compiler.

V
 
L

Lamefif

Lamefif said:
[..]
pardon my ignorance.

No problem at all (although I suspect that you're being sarcastic). Not at all
a char memory cell would be the address and 8 bit char value, and we
have 8 of these.

A memory cell is not the address. It _has_ an address.
you are saying that Data4 holds all 8 cells.

Yes. The type of 'Data4' is "an array of 8 chars". That means that
'Data4' designates the area in memory that consists of 8 memory blocks,
each contains 1 char (byte). 'Data4' is a variable name, no less but
no more.
how is this done?

How what is done? How 8 consecutive memory blocks are allocated? How
one variable can be used to identify a whole bunch of memory blocks
stringed together? That's just the C++ magic, man.

yes that the magic, how one variable can be used to identify a bunch
of memory blocks?
Here you go, using the "a" word again... 'Data4' is an item in your
source code. If you ask for its address, the address of the first
element in the array (the type of 'Data4') is given (and, yes, all
other 7 elements follow the first one closely).
exactly it cant possibly know where the next char is, apart form the
fact that they are next to each other and we can increment.
is only aware of the first element, the one is pointing to ?
 
V

Victor Bazarov

Lamefif said:
Lamefif said:
[..]
you are saying that Data4 holds all 8 cells.

Yes. The type of 'Data4' is "an array of 8 chars". That means that
'Data4' designates the area in memory that consists of 8 memory
blocks, each contains 1 char (byte). 'Data4' is a variable name, no
less but no more.
how is this done?

How what is done? How 8 consecutive memory blocks are allocated?
How one variable can be used to identify a whole bunch of memory
blocks stringed together? That's just the C++ magic, man.

yes that the magic, how one variable can be used to identify a bunch
of memory blocks?

I am not sure what it is you want to know. How does a function identify
a whole lot of operations performed by the CPU on the data passed to it
or otherwise available for manipulation? Forget that, how can *one*
variable be used to identify a _single_ memory block? If you can answer
(if you understand) the latter, what is the problem of understanding
the arrays? (BTW, what book are you reading that doesn't explain arrays?)

An array is an object that consists of other objects, called "elements",
and in which those elements are placed contiguously in the memory given
to the array itself. In a system where 'sizeof(int) != 1', how does
an object of type 'int' identify a bunch of bytes? From the language
point of view it really does not matter. Magic. Electronics. Logic.
The compiler/system/CPU all work do make it transparent to the user or
programmer.

If you want to know the object model of C++, find and read "Inside the
C++ Object Model" by Stanley Lippman. But you don't have to actually
understand all this to be a successful C++ programmer.

V
 
L

Lamefif

Lamefif said:
Lamefif wrote:
[..]
you are saying that Data4 holds all 8 cells.
Yes. The type of 'Data4' is "an array of 8 chars". That means that
'Data4' designates the area in memory that consists of 8 memory
blocks, each contains 1 char (byte). 'Data4' is a variable name, no
less but no more.
how is this done?
How what is done? How 8 consecutive memory blocks are allocated?
How one variable can be used to identify a whole bunch of memory
blocks stringed together? That's just the C++ magic, man.
yes that the magic, how one variable can be used to identify a bunch
of memory blocks?

I am not sure what it is you want to know. How does a function identify
a whole lot of operations performed by the CPU on the data passed to it
or otherwise available for manipulation? Forget that, how can *one*
variable be used to identify a _single_ memory block? If you can answer
(if you understand) the latter, what is the problem of understanding
the arrays? (BTW, what book are you reading that doesn't explain arrays?)

An array is an object that consists of other objects, called "elements",
and in which those elements are placed contiguously in the memory given
to the array itself. In a system where 'sizeof(int) != 1', how does
an object of type 'int' identify a bunch of bytes? From the language
point of view it really does not matter. Magic. Electronics. Logic.
The compiler/system/CPU all work do make it transparent to the user or
programmer.

If you want to know the object model of C++, find and read "Inside the
C++ Object Model" by Stanley Lippman. But you don't have to actually
understand all this to be a successful C++ programmer.

V

Thanks for the book suggestion i will have a look at it ...downloading
now :)
 
P

Puppet_Sock

you are saying that Data4 holds all 8 cells.

how is this done?

Heh heh. How does your house hold both you and your siblings?

Data4 is just the name for that chunk of data. The type
tells the compiler what sort of arithmetic to do in order
to supply you with what you asked for. How it does it is
the compiler's problem, and you shouldn't worry about it.
(Unless you are doing assembly coding for some reason.)
Socks
 
J

Juha Nieminen

Lamefif said:
you are saying that Data4 holds all 8 cells.

how is this done?
is the computer putting aside a chunk made up of 8 cells and assigning
to this collective a single address (Data4)?

You seem to think that Data4 is a variable holding something (eg. an
address to the array). It isn't. Data4 is just a C++ symbol which can be
used to refer to the array it symbolizes. For example, if you perform a
sizeof(Data4) what C++ will do is to look the size of that array and
return it. It won't return the size of "Data4" because it's not a
variable, it's just a name symbolizing that array.

You might be confused by the fact that C++ defines some implicit
"conversions" from an array symbol to a pointer. For example, if a
pointer is expected and you give it the array symbol, an implicit
"conversion" will be performed, and a pointer to the first item in the
array will be the result. This doesn't mean that Data4 is a pointer
(because it isn't, eg. sizeof(Data4) is a proof of that). It just means
that a pointer pointing to the first item of the array symbolized by
Data4 will be created as necessary.

This is a C++ source code construct. It has basically nothing to do
with how the CPU and its memory works. 'Data4' is an abstract symbol.
It doesn't contain anything. It just can be used, in C++, to refer to
the array (and eg. create pointers pointing to it).
 
J

James Kanze

[...]
int main() {
char Data4[8] = { 'a', 'b', 'c', 'd' };
}

[...]
Now, if you see 'Data4' (the name of the array) used anywhere in
an *expression*,

Not anywhere. Only in specific cases (admittedly frequent).
puts(Data4) ;
^^^^^^^^^^^ Expression

Because puts is declared as taking a char const* argument. If
it were declared:

void puts( char (&array)[8] ) ;

the conversion wouldn't take place. Nor would it in something
like "sizeof( Data4 )". (I know you know this, but I'm worried
that someone might misunderstand your statement, and apply it
too generally.)
char blah = Data4[2] ;
^^^^^^^^ Expression
*only then* the compiler substitutes the name with _the
address_ of the first character. That address becomes the
pointer used in the expression. The concept of array names
decaying to pointers to the first element is not as trivial as
some authors would like.

The concept itself is not that complex. The problem is all the
rest which uses it. The fact, for example, the C++, like C
before it, doesn't actually have array indexing, or the fact
that arrays are second class citizens, and that you cannot
manipulate their value. The whole thing is a mess, and is one
of the reasons why beginners should eschew C style arrays in
favor of std::vector.
 
J

James Kanze

Lamefif said:
[..]
you are saying that Data4 holds all 8 cells.
Yes. The type of 'Data4' is "an array of 8 chars". That
means that 'Data4' designates the area in memory that
consists of 8 memory blocks, each contains 1 char (byte).
'Data4' is a variable name, no less but no more.
how is this done?
How what is done? How 8 consecutive memory blocks are
allocated? How one variable can be used to identify a whole
bunch of memory blocks stringed together? That's just the
C++ magic, man.
yes that the magic, how one variable can be used to identify a
bunch of memory blocks?

What's so magical about it. If I write "double d;", d is used
to identify a block of 8 bytes. If I write "char a[8];", a is
used to identify a block of 8 bytes. The only real difference
is that in the second case, each of those bytes is individually
accessible, via its own address. (This is actually the case as
well with double, but getting at the individual bytes requires
some advanced programming, using reinterpret_cast, etc.)
exactly it cant possibly know where the next char is, apart
form the fact that they are next to each other and we can
increment. is only aware of the first element, the one is
pointing to ?

What Victor is saying is that:

1. Data4 is the name of a variable with the type char[8]. It's
not the name of any one element of the array, but refers to
the entire array (which is a compound object, like a
structure).

2. In certain contexts (a lot of them, actually), the name
Data4 will convert implicitly to the address of the first
element of the array. And because of the way address
arithmetic works in C++, Data4+1 will then be the address of
the second element, Data4+2 that address of the third, etc.

Note that in 2, there is a conversion. Data4 isn't an address;
it is the name of a variable which contains all 8 elements. It
is only the result of the conversion which is an address, and
whose type is that of a single element.
 
J

Jerry Coffin

// The GUID is represented as a structure defined in <winnt.h>.
typedef struct_GUID{ unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];} GUID;

GUID is a 128-bit number ? but looking at the structure, i have some
questions.

No, GUID is a data-structure which might take 128 bits of memory, it is
not a 128 bit number.

Actually, you have things sort of backwards: a GUID _is_ a 128-bit
number, and this structure is only a valid representation of a GUID on
an implementation that assures the struct occupies exactly 128 bits.

In case that wasn't clear, a GUID is a concept that exists entirely
separately from C++ or this struct. This struct is simply one possible
way of storing a GUID.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top