Solved

Unusual Q. for C++ Gurus !

Posted on 1997-08-05
12
232 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
ID: 1166728
Edited text of question
0
 
LVL 4

Expert Comment

by:AVaulin
ID: 1166729
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
ID: 1166730
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
ID: 1166731
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
ID: 1166732
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
ID: 1166733
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:mosfet
ID: 1166734
do you mean cs:ip??
0
 
LVL 3

Author Comment

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

Author Comment

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

Expert Comment

by:alexxx
ID: 1166737
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
ID: 1166738
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
ID: 1166739
10x for your good explanation !. I get it now ;-)

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Connecting to MS SQL db from Win32 application written in C 3 79
Socket Programming (Unix) 8 118
Dialogbox API leak? 18 81
Error creating a new C++ project in ,net 20 17
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

912 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

20 Experts available now in Live!

Get 1:1 Help Now