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

Tuesday Feb 3rd 2004 by Sonu Kapoor

This week's topics include learning the difference between 'C'-like type conversion and VC++ cast operators, whether it is necessary to delete a created pen, whether it is possible to re-sort a map using an another key, whether the STL contains a matrix class, and whether it isnecessary to delete an object from std::list.

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.

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved