GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Apr-2008, 05:22
vicky_brsh vicky_brsh is offline
New Member
 
Join Date: Dec 2007
Posts: 25
vicky_brsh has a little shameless behaviour in the past

Concept of new operator


Hi All,

I have some queries regarding the new operator. Can any one clearify them to me ?

1) As far as i know new is an operator which calls the constructor and allocates the memory using sizeof() operator.
But, sizeof operator is an operator that is executed during preprocessing time and constructor is a function that should be called during execution time.
This means that new is able to handle both pre processing execution and run time execution ?

2) We use new operator to call the constructor. but, we can also allocate memory as below

char *c = new char;

In this case there is no concept of constructor being called. Then how the new operator works?

3) If we declare function as a static function that function can be called and defined only in that particular file itself. But, how the compiler can achieve this ?



Thanks and Regards
Vikram
  #2  
Old 17-Apr-2008, 07:22
mamntc02 mamntc02 is offline
Junior Member
 
Join Date: Mar 2008
Location: Barcelona - Catalonia
Posts: 57
mamntc02 has a spectacular aura about

Re: Concept of new operator


Quote:
Originally Posted by vicky_brsh
But, sizeof operator is an operator that is executed during preprocessing time and constructor is a function that should be called during execution time.sizeof operator works in compiling time
This means that new is able to handle both pre processing execution and run time execution ?
new is an operator used to reserve memory, as well as, instance objects. However the type of data to be allocated must be known by the compiler. In other words, when you instance an object of a class, this class must be defined before. So, sizeof can calculate the amount of memory it needs in compiling time.
Of course, the allocation is done in run time.

Quote:
Originally Posted by vicky_brsh
2) We use new operator to call the constructor. but, we can also allocate memory as below

char *c = new char;

In this case there is no concept of constructor being called. Then how the new operator works?
new operator allocates a new chunk of memory to store a variable of type required. The type may be a basic type (long, char, etc.. ), a struct, class,.... or any other data type.
How it works depends on compiler and OS, and its implementation. Anyway, I don't know how it's implemented

Quote:
Originally Posted by vicky_brsh
3) If we declare function as a static function that function can be called and defined only in that particular file itself. But, how the compiler can achieve this ?
When you compile a program, each .c or .cpp file is compiled separately, and later they are linked together, so the compiler knows where is any function. If it is declared as static, it will only can be called from the same file.
Do not confuse them with static methods defined in a class. They has different meaning: They're class methods and not object methods.

Regards
__________________
Please, correct me if I'm wrong, and sorry for my english ;)
  #3  
Old 18-Apr-2008, 04:24
Golmal Golmal is offline
Junior Member
 
Join Date: May 2006
Posts: 43
Golmal has a little shameless behaviour in the past

Re: Concept of new operator


Hi,
I had some other thought...
Seems like the explaination provided by mamntc02 is correct but i like to add more to it... any corrections are accepted.

For 1 and 2 point

On trying to instantiate an object both in the case of user defined or default one.. it first checks if the operator new is been overloaded by it's class or not, if not it will go to the global scope and fetches the size of the object to be created.Allocation of the memory takes place later, in the case of the char may be in the global scope the size of char is be defined just as in C complier.


For point 3
If we declare function as a static function that function can be called and defined only in that particular file itself. But, how the compiler can achieve this ?

the techinques may be of name mangalining(prefix the function name with the file name along with the parameters to the function, during parsing inside the complier) along with the location where this static method or the variable are been stored.. usually in bss.. Thereby the can be easily distinguisable .
  #4  
Old 20-Apr-2008, 21:27
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Concept of new operator


A billion thnaks for your help.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Assignment Operator and Pointer Peter_APIIT C++ Forum 14 25-Sep-2007 06:36
Write a C++ program involving operator overloading jjrsbigo C++ Forum 2 13-Dec-2006 07:47
C++ PhoneBook marita C++ Forum 46 12-Jun-2005 13:10
Operator overloading c++ clander C++ Forum 4 25-Apr-2005 10:21
Operator overloading problem grscot C++ Forum 2 21-Oct-2004 00:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:33.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.