C
Chris Cranford
I'm trying to write a virtual machine and relies on a stack and I have a
question about how I would work with UDTs such as:
typedef struct tagTData {
unsigned long ulData;
char szData[28];
} TData;
The above structure is seen as a 32-byte block of memory space. If a variable
is defined in a function like so:
void myFunc(void) { TData data; /* do stuff here */ return; }
Essentially 32-bytes (8 4-byte/32-bit blocks) are reserved on the stack. If
the struct was used as a pointer, only 1 32-bit block that contains the memory
pointer to the structure is allocated on the stack.
Is it then up to the compiler to calculate the byte offsets for the data
structure when assembly is written when code accessing the UDT fields?
For example:
TData *pData = new TData();
pData->ulData = 1; // accessed via memptr+0 byte offset
pData->szData = NULL; // accessed via memptr+4 byte offset
TData data;
data.ulData = 1; // SP to variable data + 0 byte offset
data.szData = NULL; // SP to variable data + 4 byte offset
Thanks
Chris
question about how I would work with UDTs such as:
typedef struct tagTData {
unsigned long ulData;
char szData[28];
} TData;
The above structure is seen as a 32-byte block of memory space. If a variable
is defined in a function like so:
void myFunc(void) { TData data; /* do stuff here */ return; }
Essentially 32-bytes (8 4-byte/32-bit blocks) are reserved on the stack. If
the struct was used as a pointer, only 1 32-bit block that contains the memory
pointer to the structure is allocated on the stack.
Is it then up to the compiler to calculate the byte offsets for the data
structure when assembly is written when code accessing the UDT fields?
For example:
TData *pData = new TData();
pData->ulData = 1; // accessed via memptr+0 byte offset
pData->szData = NULL; // accessed via memptr+4 byte offset
TData data;
data.ulData = 1; // SP to variable data + 0 byte offset
data.szData = NULL; // SP to variable data + 4 byte offset
Thanks
Chris