Friday, February 23, 2007

C++ : Free-store versus Heap

What's the difference between the heap and the free-store? The C++ Programming Language keeps on referring them interchangeably. There was as huge cry over this issue in C/C++ programmer's community in Orkut. I had to shoot a mail to Dr. Bjarne Stroustrup. Here's our conversation:

My Mail:

Dear Mr Stroustrup,

Sorry to disturb you again. You have mentioned several times in the TC++PL that 'new' allocates memory from the 'free store (or heap)'. There has been a huge cry on the C++ community at Orkut (that I am moderating) as to whether free-store is the same as heap. The argument given against is that Mr Herb Sutter has mentioned that the free-store is different from the heap:

http://www.gotw.ca/gotw/009.htm

and that global 'new' has nothing to do with the heap.

So, if so, why has TC++PL used 'free store (or heap)' instead of mentioning the use of 'heap' separately.

Waiting anxiously for the response.

Regards,
Zaman Bakshi

His Reply:

Note that Herb says: "Note about Heap vs. Free Store: We distinguish between "heap" and "free store" because the draft deliberately leaves unspecified the question of whether these two areas are related. For example, when memory is deallocated via operator delete, 18.4.1.1 states:"

In other word, the "free store" vs "heap" distinction is Herb's attempt to distinguish malloc() allocation from new allocation.

>
> So, if so, why has TC++PL used 'free store (or heap)' instead of
> mentioning the use of 'heap' separately.

Because even though it is undefined from where new and malloc() get their memory, they typically get them from exactly the same place. It is common for new and malloc() to allocate and free storage from the same part of the computer's memory. In that case, "free store" and "heap" are synonyms. I consistently use "free store" and "heap" is not a defined term in the C++ standard (outside the heap standard library algorithms, which are unrelated to new and malloc()). In relation to new, "heap" is simply a word someone uses (typically as a synonym to "free store") - usually because they come from a different language background.


My Reply:

Thank you Mr. Stroustrup, I had inferred the same thing (about using free store as general -- or better, synonym -- term) and had explained the community. But, I had been requested to reconfirm.

With warm regards,
Zaman Bakshi

Sunday, February 18, 2007

C++ : Indeterminate Value

The C language standard clearly 'defines' what an indeterminate value is in C. But the C++ standard is missing this definition. Naturally, we can't adopt the definition in C standard to C++. I wanted to know, and who could have been more reliable than Mr. Bjarne Stroustrup to clear this cloud of uncertainty. So, I dashed an email to him. Here's the conversation that followed:

----------------------------------

Dear Mr Stroustrup,

I am reading D&E, and let me congratulate you for writing such a great book. It has been of a lot of help. Mr. Stroustrup, I am moderating a C++ community on Orkut and there has been a very big issue over what 'indeterminate value' means for the C++ standard. The C standard clearly states what 'indeterminate value' means, but the C++ standard though using (indeterminate value) many times doesn't specify its definition. Should we regard 'indeterminate value' in C++ as being undefined, or should we stick to the C standard's definition (for 'indeterminate value')?

Anxiously waiting for the response.

Regards,
Zaman Bakshi.

----------------------------------

Thanks.

I never used "indeterminate value" and hadn't noticed that it had "snug
into" the C++ standard. I have raised an issue and "indeterminate value"
will be defined in C++0x. You can't "stick to C's definition" because
that definition has never been approved for C++ (was introduced into C
relatively lately, I believe). "indeterminate" simple means that you
don't know what that value is (if could be absolutely any bit pattern
that fits in the object). I believe the C++ standard is specific about
which operations requires a properly initialized object.

Does this address the issues raised in your discussion? If not, please
ask again.

----------------------------------

Dear Mr Stroustrup,

Thank you for replying to my e-mail. Yes, it does answer my issue. This is exactly what I had inferred, but as you know, developers like me can't argue over the standard, so had to clear the doubt. I, like others, are anxiously waiting for C++0x to be out with the standard. Good luck with it. And I thank you again for your response.

Regards,
Zaman Bakshi

----------------------------------

Thanks. I'm working hard for C++0x to become C++09. Doing that requires
a complete feature freeze and complete WP by the end of 2007.