Stack Machine Implementation in C++

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
 
K

Kevin Goodsell

Chris said:
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?

The standard does not define anything like that. "stack" is a class
template implementing a common data structure. "heap" is a data
structure supported indirectly through functions like make_heap(), and
used by the class template priority_queue.

If by "stack" you mean "the place where things with automatic storage
duration live" and by "heap" you mean "the free store", then no, that is
not accurate. Only local objects explicitly declared 'auto' or
'register' or NOT explicitly declared 'static' or 'extern' have
automatic storage duration. See 3.7 (basic.stc) for details.

http://std.dkuug.dk/jtc1/sc22/wg21/docs/wp/html/nov97-2/
http://std.dkuug.dk/jtc1/sc22/wg21/docs/wp/html/nov97-2/basic.html#basic.stc
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];

Maybe on your machine, but this is not a requirement according to the
C++ standard. 'long' must be at least 32 bits, 'int' at least 16, and
'char' at least 8. It's not required that an implementation allocate
exactly the space required for the objects and no more.
+-----------------+
| 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?

Aren't you the one that's defining the system?
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.

I don't know what you're saying here, but I don't think it has anything
to do with standard C++, the topic here. Are you sure you got the right
group?

-Kevin
 

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
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top