Hottest Forum Q&A on CodeGuru - January 26th - 2004

Comment from Author:

In the column from last week, we covered tiff's problem. Here, he was looking for the _mbstowcsz() function in VC7. But, unfortunately, he could not find any solution. So, I asked you this question, but unfortunately I got no replies. I searched, too on, the net, but could not find the function in VC7.


Lots of hot topics are covered in the Discussion Forums on CodeGuru. If you missed the forums this week, you missed some interesting ways to solve a problem. Some of the hot topics this week include:

What is the difference between "C"-like type conversion and VC++ cast operators? (top)


reptile asked an interesting question. He wants to know the difference between a C-type conversion and a VC++ comparision. Do you know the difference?

Is there any difference between "C"-like conversion and VC++ cast
conversion operators (reinterpret_cast, dynamic_cast, static_cast)?

I don't mean a logical difference, I mean low-level code generation
differences. Maybe cast conversion operators are used just in help
for programmers?
  • In C, the conversion will be done assuming that the casted object or variable is of a type in which it is casted. If it is not, your program can crash.
  • The C++ conversion includes additional information to each variable or object. It tests whether the cast is possible. If not, it returns NULL. You can test the result against NULL and assert.

Is it necessary to delete a created pen? (top)


Gyannea needs to know whether is it necessary to delete a Pen?

If I create a Pen with
hpenl = CreatePen(PS_SOLID, 0, color1);
and select that pen and then
hpenl = CreatePen(PS_SOLID, 0, color2);
and select that pen, can I delete a single pen or will
there be a second undeleted pen out in never-never land?
It is ALWAYS neccesary to delete a Pen created with DeleteObject because the Windows GDI objects are limited resources. If you don't free an object with a call to DeleteObject every time you create a new object, you will at some time run out of resources. This will cause problems with programs other than your own.

Always remember that the allocated GDI objects are not freed when the program exits. These remain allocated until a reboot.

Is it possible to re-sort a map using an another key? (top)


ilamparithi is working with an STL Map. He has already inserted all data in the map and now he wants to re-sort the map using an another key. Is that possible?

Hi All,
After all the datas are inserted into the MAP, is it is possible to
re-sort the MAP using some other key?

No, this is not possible. An std::map is always sorted by the key type that is given to it at compile-time. The only way you can sort by something else is to copy the contents to a new map or vector, which then is sorted on a different key or you have to delete the old key and reinsert the data with a new key.

Does the STL contain a matrix class? (top)


LudaLuda needs a matrix class. Now he wonders whether he can find it in the STL.

Does anyone know what header file needs to be included to use the
matrix class? ex: Im trying to use
matrix x(2,3)
Is there an STL class? Does VC++ 6 include a class?
It's supposed to create a 2D int-array with 2 Rows and 3 Columns.

Well, the STL does not contain such a class and VC++6 also does not contain a class. You can use a vector to create a 2D array. Take a look at the following faq. And, if you really need a seperate class for that, you can download one here.

Is it necessary to delete an object from std::list? (top)


Miran created an STL list of objects. Now, he wants to know whether remove will only remove the object from the list or it will also free the memory dynamically allocated for a MYSTRUCT object in AddObject function? Because the code is very long, I only show a small part of it. But, to understand the whole scenario, I suggest you read the whole thread. Paul McKenzie and Philip Nicoletti explained the problem in detail.

// MYSTRUCT = A simple structure.
// g_MyStructList = ..std::list<MYSTRUCT*> g_MyStructList;

void AddObject(LPSTR lpstrFile, DWORD dwSize, BOOL bFlag)
    MYSTRUCT* pMySrtuct = new MYSTRUCT;
    pMySrtuct->strFile = lpstrFile;
    pMySrtuct->dwSize = dwSize;
    pMySrtuct->bFlag = bFlag;

Well, the displayed code above does not store objects; instead, it stores pointers. There is no need for delete if you store objects. You do not "delete" objects from the list unless the "object" is a pointer to dynamically allocated memory. The simple rule is this: If you call "new", you are responsible for calling "delete". If you didn't call "new", you don't call "delete".

Take a look at the whole thread to read more about this interesting question.

About the Author

Sonu Kapoor

Sonu Kapoor is an ASP.NET MVP and MCAD. He is the owner of the popular .net website DotNetSlackers publishs the latest .net news and articles - it contains forums and blogs as well. His blog can be seen at:


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Salesforce has been recognized by Gartner as a leader in this report for three years in a row. This graphic was published by Gartner, Inc. as part of a larger research document and should be evaluated in the context of the entire document. The Gartner document is available upon request from Gartner does not endorse any vendor, product or service depicted in its research publications, and does not advise technology users to select only those vendors with the highest ratings. Gartner research …

  • You've managed to piece together the "DREAM TEAM." Your tech staff works like a well-oiled machine, keeping your company humming and thriving. And then it happens: dissension. For whatever reason, your employees have grown unhappy and you find out they're searching for new employment or losing productivity. What did you do wrong? Did you hire the wrong people? Did your company push them away? Or is it a combination of numerous factors? Read this white paper to learn how to build an environment that fosters …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date