C
Chris Cranford
I am in the process of implementing a stack machine virtual environment where
each entry on the stack is a 32-bit value which then can reference to any
memory pointer, numeric value, or alike.
My understanding of "stack" versus "heap" is that unless I use new/delete in
order to specifically allocate memory for a variable's storage space that it
will be allocated upon the stack, correct?
If so, the following three declarations would yield a total allocation of 4
stack slots assuming that strings are stored in ASCII 8-bit format and not
UNICODE 16-bit format.
unsigned long ulVal1;
int nVal2;
char szString[8];
+-----------------+
| ulVal1 | Slot 0
+-----------------+
| nVal2 | Slot 1
+-----------------+
| Low 4-byte Str. | Slot 2
+-----------------+
| High 4-byte Str | Slot 3
+-----------------+
Therefore, any operation on ulVal1 and nVal2 simply will have to copy 1 slot
from the local stack variable space to the top-of-stack slot, but when working
with the string, it will require working in 4-byte chunks, right?
Again, if that is the case, to store the word "test" in the string will
require that the high 4 byte string (slot 3) be copied to TOS, then the first
4-bytes of "test" would be copied to TOS, then a stack operation is called to
copy TOS to SOS. Right?
Then if the string was 5 characters long, I would do the same process as
before followed by copying slot 2 to TOS and then the 5th string value + 3
byte padding to TOS and perform a copy from TOS to SOS and store it back.
Thanks,
Chris
each entry on the stack is a 32-bit value which then can reference to any
memory pointer, numeric value, or alike.
My understanding of "stack" versus "heap" is that unless I use new/delete in
order to specifically allocate memory for a variable's storage space that it
will be allocated upon the stack, correct?
If so, the following three declarations would yield a total allocation of 4
stack slots assuming that strings are stored in ASCII 8-bit format and not
UNICODE 16-bit format.
unsigned long ulVal1;
int nVal2;
char szString[8];
+-----------------+
| ulVal1 | Slot 0
+-----------------+
| nVal2 | Slot 1
+-----------------+
| Low 4-byte Str. | Slot 2
+-----------------+
| High 4-byte Str | Slot 3
+-----------------+
Therefore, any operation on ulVal1 and nVal2 simply will have to copy 1 slot
from the local stack variable space to the top-of-stack slot, but when working
with the string, it will require working in 4-byte chunks, right?
Again, if that is the case, to store the word "test" in the string will
require that the high 4 byte string (slot 3) be copied to TOS, then the first
4-bytes of "test" would be copied to TOS, then a stack operation is called to
copy TOS to SOS. Right?
Then if the string was 5 characters long, I would do the same process as
before followed by copying slot 2 to TOS and then the 5th string value + 3
byte padding to TOS and perform a copy from TOS to SOS and store it back.
Thanks,
Chris