Welcome to MSDN Blogs Sign in | Join | Help

Aakash Kambuj's blog

Windows Live Core
How operator new works

I've always been confused about how operator new works. Finally, I found this article which clarifies how the compiler generates code when it encounters new.

C++ Language Reference  

How new Works

The allocation-expression — the expression containing the new operator — does three things:

  • Locates and reserves storage for the object or objects to be allocated. When this stage is complete, the correct amount of storage is allocated, but it is not yet an object.
  • Initializes the object(s). Once initialization is complete, enough information is present for the allocated storage to be an object.
  • Returns a pointer to the object(s) of a pointer type derived from new-type-name or type-name. The program uses this pointer to access the newly allocated object.

The new operator invokes the function operator new. For arrays of any type, and for objects that are not of class, struct, or union types, a global function, ::operator new, is called to allocate storage. Class-type objects can define their own operator new static member function on a per-class basis.

When the compiler encounters the new operator to allocate an object of type type, it issues a call to type::operator new( sizeof( type ) ) or, if no user-defined operator new is defined, ::operator new( sizeof( type ) ). Therefore, the new operator can allocate the correct amount of memory for the object.

Note   The argument to operator new is of type size_t. This type is defined in DIRECT.H, MALLOC.H, MEMORY.H, SEARCH.H, STDDEF.H, STDIO.H, STDLIB.H, STRING.H, and TIME.H.

An option in the grammar allows specification of placement (see the Grammar for new Operator). The placement parameter can be used only for user-defined implementations of operator new; it allows extra information to be passed to operator new. An expression with a placement field such as

T *TObject = new ( 0x0040 ) T;

is translated to

T *TObject = T::operator new( sizeof( T ), 0x0040 );

The original intention of the placement field was to allow hardware-dependent objects to be allocated at user-specified addresses.

Note   Although the preceding example shows only one argument in the placement field, there is no restriction on how many extra arguments can be passed to operator new this way.

Even when operator new has been defined for a class type, the global operator can be used by using the form of this example:

T *TObject =::new TObject;

The scope-resolution operator (::) forces use of the global new operator.

 

The original text can be found at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_how_new_works.asp

SQL Server support

I have gotten a number of emails from people through my blog, asking various questions about SQL Server 2000 and SQL Server 2005. While I appreciate getting these questions, please note that the best place for SQL Server support is the SQL Server newsgroups, where the presence of hundreds of extremely knowledgeable people will ensure you get a rapid response to your queries.

The SQL Server 2000 newsgroups are located on the NNTP server msnews.microsoft.com. You will need a newsreader like Outlook Express to browse and post on the newsgroups. The SQL Server folders are in the microsoft.public.sqlserver.* hierarchy.

For SQL Server 2005 Beta support issues, visit the SQL Server 2005 newsgroups at http://communities.microsoft.com/newsgroups/default.asp?icp=sqlserver2005&slcid=us  [thanks j0ey]

 

datetime quirks

Datetime and smalldatetime data types can be confusing at times.
For example, consider the following select statement:

create table t1(c1 datetime)
go
insert t1 values('20010101')
go

select c1 + '1/1/3' from t1
go

 If you expected '1/1/3' to represent january 1, 3AD, you're in for a little surprise.
This addition of a string to a datetime produces the following result:

2104-01-02 00:00:00.000

This happens because SQL Server treats '1/1/3' as '1/1/2003'. Then it calculates the difference in days between '1/1/2003' and '1/1/1900', and adds the numbers of days to the value stored in c1. This happens because January 1, 1900 is treated as the base date in SQL Server 2000 and SQL Server 2005.

Tell me about your experience with Browse mode
I'd like to get a feel of how many of you out there are using Browse mode (appending FOR BROWSE to your SELECT statements, and then using the additional key column metadata returned in order to update the table) directly in your applications in SQL 2000. What kind of applications are you using them in - scripted stuff like websites/ecommerce, or in the middle tier/for backend updates?
 
I would appreciate your feedback/comments about Browse mode posted as comments to this blog - what you think is good about it, what you would like improved, and whether the currently available information on the subject is sufficient/non-existent.
 
If you would like any questions about Browse mode answered, feel free to post them to either the SQL 2000 or SQL 2005 newsgroups, or contact me.
 
Meanwhile, that promised Browse mode article is coming :)
Talking about SQL server topics

Hi all,

My name is Aakash Kambuj, and I'm a developer on the SQL Server team, working on SQL Server 2005. Over the next few weeks, I'm going to be writing about some of the more esoteric features of SQL Server and Transact-SQL, like FOR BROWSE and Bulk Insert, and how their behaviour is improved in SQL Server 2005.

So stay tuned!

-Aakash

 

Page view tracker