Solved

Unusual Q. for C++ Gurus !

Posted on 1997-08-05
12
230 Views
Last Modified: 2010-04-10
Hi !
When I create several objects of the same type, with the new operator indeed, I figure out each occurence has its own data area for obvious reasons, but what about the code area ? Do they share the same code section ? (in order to save memory).
I don't see any reason to duplicate the code of the objects, or am I missing something ?

Any ideas, comments, disapprovals ? Just wondering !
CU, have fun

p.s. this weird question was born after I made/took a bet with a mate ;-)
0
Comment
Question by:ESI
12 Comments
 
LVL 3

Author Comment

by:ESI
Comment Utility
Edited text of question
0
 
LVL 4

Expert Comment

by:AVaulin
Comment Utility
You have to understand that code area is single for any objects number. First parameter of any class method in C++ is always pointer to object which need to call this method. That is why "this" pointer accessable in any class method.
0
 
LVL 3

Author Comment

by:ESI
Comment Utility
Hi AVaulin,
Ok, the address u point to with the 'this' ptr is in the data area of the object, different for each object indeed, but the code where the pointer is initialized is probably in the same code area, common to all object of a given class.
For example, if u have a sort rule in the code of a class, you think this code is duplicated with each instance of the class ?

Bye, and thanks 4 your time !
CU
0
 
LVL 1

Expert Comment

by:TKII
Comment Utility
ESI you got it completely right. The code is shared between all objects of the same class. It doesn't have to be this way but nothing else would make sense. Also it is said so in the Docs to VC++.
0
 
LVL 3

Author Comment

by:ESI
Comment Utility
Hi TKII !
Thanks 4 your reply.
Could u possibly point me to where u found some doc about this fact ? in the F1-Help ?

Bye, and thanks u 4 y'r precious time !
Cheers
0
 
LVL 3

Author Comment

by:ESI
Comment Utility
Is there any way to trace/show the PC main address counter of the CPU while debugging ? And to retrieve its value in C++ ?
(I'd like to write some self-modifying code)
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Expert Comment

by:mosfet
Comment Utility
do you mean cs:ip??
0
 
LVL 3

Author Comment

by:ESI
Comment Utility
Hi mosfet,
Yes, I'd like copy in a C variable & use the CS:IP pointer.
0
 
LVL 3

Author Comment

by:ESI
Comment Utility
Hi mosfet,
Yes, I'd like copy in a C variable the CS:IP pointer.
0
 
LVL 2

Expert Comment

by:alexxx
Comment Utility
I suggest you try to write self-modifying code in Visual Basic. It's far more interesting, beleive me.
0
 

Accepted Solution

by:
nigel5 earned 30 total points
Comment Utility
The code segment is compiled from your source, including templates. This makes up the code segment, and is static throughout the life of the program. The heap is where objects are created dynamically and this memory is only used to store the class attributes, not the code. Global objects are compiled in, but again they all use the same code segment and the same functions.
You can take a function pointer of a class and call it like a standard function as long as you pass the object's address (the 'this' pointer) as the first  argument when you call it. Static functions do not pass the this pointer in as thay are not object specific.

In sumary....
there is only 1 copy of every function you write, templates will generate into 1 function per templated argument combination, and this code is available to all threads, and objects.

heap and stack can be independant within threads and process'.
0
 
LVL 3

Author Comment

by:ESI
Comment Utility
10x for your good explanation !. I get it now ;-)

Well, have happy programming & take care all !
Bye
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now