Click to See Complete Forum and Search --> : implementation of new and delete in different compilers.
indrajit_p1
April 27th, 2008, 11:28 PM
I recently attended a interview where I was asked something in the line of what the memory allocated by new or malloc looks like. My answer to it was that that would really depend on the compiler, which led us to the question that what if I am linking together two object codes compiled using different compilers and there is some object allocated in one of the .o (it could even be a library) s a reference/pointer to which is passed to some function in the other .o (or whatever it is), which in turn is resposible for deleting the memory allocated for the object.
now, if the implementation of new and delete varied from compiler to compiler that would not be possible.
Can someone throw some light on the reality of this. Was I right in what I said at the interview?
Regards
I
Lindley
April 27th, 2008, 11:38 PM
The reality is that C++ object code, including static libraries, isn't all that portable between compilers.
C code is. But not C++ code.
Oh, and the design you describe, passing a pointer out an expecting someone else to delete it.....is simply awful. The correct answer to the question is that no sane programmer would write something like that.
_uj
April 28th, 2008, 12:15 AM
Can someone throw some light on the reality of this.
You're right in that C++ offers no guarantee that code produced by different compilers can be linked.
On the other hand it's possible to replace new and malloc with your own implementations. You can also "plug in" your own memory allocators when you use containers from the STL library. This is what you do for example in the Intel TBB library to get memory allocation better suited for multi-threading.
So although compilers offer default memory allocation you can override it and replace it with your own. In a strict sense you don't depend on the compiler because you have the freedom of choise.
indrajit_p1
April 28th, 2008, 01:25 AM
Hi Folks,
First of all, thanks for your inputs.
_uj, I knew that it is possible to have user defined new and delete instead of the defaluts provided by the compiler (which didn't immediately cross my mind at the interview table because I have never done it myself). Have read it in Effective C++ by Meyers.
The reality is that C++ object code, including static libraries, isn't all that portable between compilers.
C code is. But not C++ code.
This is something I should take note of as a programmer myself ( I did have the hunch that it would be so, but this is the first time I'm getting it from [in all good possibilities] someone more experienced in this matter). Thanks!
Oh, and the design you describe, passing a pointer out an expecting someone else to delete it.....is simply awful. The correct answer to the question is that no sane programmer would write something like that.
As I said, this scheme of doing things was proposed by my interviewer asking me if it would work, and I sticked pretty strongly to my opinion that it won't, in all possibilities ( meaning at least it's not guaranteed by C++). The interviewer seemed to be of the opinion that it would, and was putting forward arguments like linking a VC++ project to a library compiled with Borland C++ where it would be possible to pass a pointer from the library and delete it inside the project.
I posted this question just to check that the rank I assigned the perticular interviewer (in my mind) in the moron/***hole scale is directly propotional to the one he should get in reality. His original line of questioning seemed to indicate he has aquired some exotic knowledge about memory allocation from some source like "Inside the C++ Object Model" by Lippman , and was trying to brandish it on me (since my resume shows that I am a graduate from a world famous Technical University in India) and drew all kinds of wrong inferences from that exotic knowledge (due to what, should be guess able by now).
I've spoken of the kind of people that abounds the Software Engineering business in the IT superpower, India (where I am from and where this interview took place), in this forum before. This should serve as another of the supporting examples.
I'm not at all sorry for the Indian programmers in this forum who are of contraty opinion (and who read this post). I agree there are brilliant of the brilliant people here as well, but they would be very rare exceptions. This is how the vast majority IS like.
chao!
I
_uj
April 28th, 2008, 02:20 AM
The interviewer seemed to be of the opinion that it would, and was putting forward arguments like linking a VC++ project to a library compiled with Borland C++ where it would be possible to pass a pointer from the library and delete it inside the project.
If this works then it's a feature of the compilers, not the C++ language.
To my knowledge the only language defining binary compatibility is Java bytecode (and maybe also CIL, the .NET counterpart).
Lindley
April 28th, 2008, 07:27 AM
The proper way to handle the scenario you describe----passing an allocated pointer out of a static library----is to also provide a function in the library interface to free the pointer. That way you can be sure that the new and delete calls come from code compiled in the same place.
indrajit_p1
April 28th, 2008, 07:59 AM
Thanks a lot for the knowledge points and the insights folks.
I
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.