basic class memory use question

V

voidtwerp

Hi,

I hope this is not too OT but I would like clarification on how classes
are held in memory.
each object obviously has an individual copy of its data.
I guess a class would only have one copy of its functions held in
memory (not sure what part of memory this would be refered to as).
Then whenever an object has a function called on it this function would
be copied onto the stack - am I right?

But then this should cause problems if the returned pointer is used
after the function has been replaced on the stack but it works
perfectly well - so what exactly is stored where?

char * MyClass::MyFunc(int selection)
{
switch(selection)
{
case 1:
return "one";
case 2:
return "two";
default:
return "no selection";
}
}

thanks in advance for your time.
 
U

Ulrich Hobelmann

voidtwerp said:
Hi,

I hope this is not too OT but I would like clarification on how classes
are held in memory.
each object obviously has an individual copy of its data.
I guess a class would only have one copy of its functions held in
memory (not sure what part of memory this would be refered to as).
Then whenever an object has a function called on it this function would
be copied onto the stack - am I right?

Objects are held in memory (heap or stack), function activation records
(local variables) are held on the stack, and the function's code is
static (in the read-only code segment usually).
But then this should cause problems if the returned pointer is used
after the function has been replaced on the stack but it works
perfectly well - so what exactly is stored where?

char * MyClass::MyFunc(int selection)
{

selection: on stack / in registers
switch(selection)
{
case 1:
return "one";
case 2:
return "two";
default:
return "no selection";

These three strings: in some static data segment (read only). What you
return is a pointer to them, which should be returned in a register.
 
H

Heinz Ozwirk

voidtwerp said:
Hi,

I hope this is not too OT but I would like clarification on how classes
are held in memory.
each object obviously has an individual copy of its data.
I guess a class would only have one copy of its functions held in
memory (not sure what part of memory this would be refered to as).

So far your guess is close enough. And as C++ itself does not give that
memory a name (that I can remember) I won't give it a name either. Functions
are just a part of the program and the program can access them.
Then whenever an object has a function called on it this function would
be copied onto the stack - am I right?

No. As far as C++ is concerned, functions are not copied anywhere. They are
just there. The OS may load the code from disk if it thinks that to be
usefull, but usually you don't have to worry about that. And even if the
code would be copied, no decent OS would copy them onto a stack. Most OS
even take special care that whatever is on the stack or in any other piece
of memory, that is used for data storage, will never be executed. But that's
a matter of system secuitity and not of C++.

What usually is allocated on a stack (even though C++ does not require it to
be a stack) are local variables. These variables are allocated when
execution reaches the point where they have been defined in the code and
they will be destroyed when execution leaves the smallest enclosing scope.
But then this should cause problems if the returned pointer is used
after the function has been replaced on the stack but it works
perfectly well - so what exactly is stored where?

This is indeed a problem for local variables. You must never return a
pointer or reference to a local variable.
char * MyClass::MyFunc(int selection)
{
switch(selection)
{
case 1:
return "one";
case 2:
return "two";
default:
return "no selection";
}
}

String literals are no local variables. You can think of them as global
constants which are available as long as the program runs. So it is ok to
have a function

char const* Foo()
{
return "foo";
}

But you should never try

char const* Bar()
{
char bar[] = "bar";
return bar;
}

And to add even more confusion, you can do

char const* Baz()
{
char const* baz = "baz";
return baz;
}

Foo is ok because it simply returns the address of something global. Baz is
ok too. It takes the address of something global, stores it in a local
variable, and finally returns a copy of this address, which still points to
something global. Bar, however, creates a local variable, initializes it
with a copy of a global string, and than returns the address of the local
variable.

HTH
Heinz
 
M

Murali Krishna

voidtwerp said:
But then this should cause problems if the returned pointer is used
after the function has been replaced on the stack but it works
perfectly well - so what exactly is stored where?

returning a value from a function doesn't replace the function code
with that returned value.

Upon returning from a Win32 function, the function's return value, if
any, can be found in eax (a register) and not in the address of the
function that is returning.
char * MyClass::MyFunc(int selection)
{
switch(selection)
{
case 1:
return "one";

All functions are dumped into memory.. Stack and they will be cleaned
up when the process is being killed.

Each function will have it's own memory space like variables.

After completion of function execution it is the responsiblity of the
calling function to make sure that the position of the stack is
restored to exactly where it originally was, the position before
function MyClass::Myfunc was called. It also depends on number of and
type of arguments of that function. It involves few jump sequences.

and we dont have to worry about that.

if you see the function's address and the return value's address, you
can find that they are different.

to learn more about that, learn asm.

-- Murali Krishna
 
J

Jim Langston

voidtwerp said:
Hi,

I hope this is not too OT but I would like clarification on how classes
are held in memory.
each object obviously has an individual copy of its data.
I guess a class would only have one copy of its functions held in
memory (not sure what part of memory this would be refered to as).
Then whenever an object has a function called on it this function would
be copied onto the stack - am I right?

But then this should cause problems if the returned pointer is used
after the function has been replaced on the stack but it works
perfectly well - so what exactly is stored where?

I will give you my experience spefically for this code for OSes I've worked
with. Others have given you information about storage.
char * MyClass::MyFunc(int selection)

When this method is called an interger is pushed onto the stack along with
housekeeping (registers etc) and the entry point to the function is called.
{
switch(selection)
{
case 1:
return "one";

"one" is a static character array that is stored in the executable file
somewhere. It is loaded into program memory, whereever that may by and the
address of it remains constant for the life of the program (or the OS makes
it appear it is constant anyway).

When this return statement is called the address of where the memory
location where the static char string "one" is stored is pushed onto the
stack. Since "one" is static, it doesn't change, this address will be valid
after the function returns.
case 2:
return "two";

Same as Case 1.
default:
return "no selection";

Same as case 2.

When the method is returned the return varaible (in this case a character
pointer) is popped from the stack and used however you called the method (or
thrown away if you didn't do anything with it). Then the information pushed
onto the stack when the function was called is popped off (the integer you
passed as a parameter and the housekeeping registers etc...)
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top