473,385 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

basic class memory use question

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.

Jul 4 '06 #1
4 2020
voidtwerp wrote:
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.
}
}

thanks in advance for your time.
Jul 4 '06 #2
"voidtwerp" <vo*******@gmail.comschrieb im Newsbeitrag
news:11*********************@v61g2000cwv.googlegro ups.com...
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

Jul 4 '06 #3
voidtwerp wrote:
>
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

Jul 4 '06 #4
"voidtwerp" <vo*******@gmail.comwrote in message
news:11*********************@v61g2000cwv.googlegro ups.com...
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...)
>
thanks in advance for your time.

Jul 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: soni29 | last post by:
hi, i'm reading a c++ book and noticed that the author seems to allocate memory differently when using classes, he writes: (assuming a class called CBox exists, with member function size()): //...
11
by: fremenusul | last post by:
First let me say I am familar with programming, but not familiar with VB.net. I am loading an existing XML document with this procedure Dim xmlFile As String = "..\data\Products.xml" Dim...
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
14
by: luis | last post by:
Are basic types (int, long, ...) objetcs or not? I read that in C# all are objects including basic types, derived from Object class. Then in msdn documentation says that boxing converts basic...
5
by: Paul Bromley | last post by:
I have written a similar enquiry to this newsgroup, but had no responses - hence I will rephrase it with the hope that someone will answer. I am new to using Classes, but trying hard to get the...
3
by: Sally Sally | last post by:
I have a very basic question on the two parameters shared buffers and effective cache size. I have read articles on what each is about etc. But I still think I don't quite grasp what these settings...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
6
by: Jeff Bender | last post by:
I am trying to remember how to code in C++ after many years of using Java exclusively. I have this setup: class Base { public: virtual void printA(){} };
9
by: Peskov Dmitry | last post by:
It is a very basic question.Surely i got something wrong in my basic understanding. //Contents of file1.cpp using namespace std; #include <iostream> template <typename T> class my_stack;
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.