January 17, 2021
Hot Topics:

Hottest Forum Q&A on CodeGuru

  • By Sonu Kapoor
  • Send Email »
  • More Articles »


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:

How do I cast from std:string to char *? (top)


avi123 wants to know how to cast from std::string to char *. Although there might be a simple solution for that question, I think it is interesting for everybody to know the solution.

How do I cast from std:string to char *?

Actually, this is not possible. The only thing that you can do is to use string::c_str(), which will return a const char *, which means that you can't modify it.

But what if I need to modify the char? Can I do something like this?

const char* str1;
char* str2;
str2 = (char*)str1

No, you can't do this. The (char *) is a C-style cast that removes the constness of str1. The solution provided by YvesM is to make a non-const copy of the string. Here is how it might look.

string s = "Hello";
char *sz;

sz = new char[s.length() + 1];
strcpy(sz, s.c_str());
// now work with sz, which is a copy of s

// clean up
delete [] sz;

const is there for a reason. The reason is that you should not modify a const object. If you try to modify it, it could lead to all sorts of strange problems and undefined behaviour.

How do I read a GIF file? (top)


vidhu a junior member is trying to read data from a GIF file, but, unfortunately, he does not know how!

I want to read some data from a GIF file. I have no idea how to
go about it. Can anyone help me...

Well, to read a GIF file, you should read the article CXImage. Surely you are thinking now why I selected this question in my column—since it does not sound very interesting.

The interesting part is provided by Paul McKenzie. Here is the interesting piece of information:

The only problem is that the LZW algorithm used to read (and write) GIF files is patented by Unisys. This means that if you want to distribute your program to others (even if it's freeware) you must get a license from Unisys (which isn't free).

The Unisys patent expired in the United States in June of this year. It will expire in Europe, Canada (and I believe in Asia) next year, June or July 2004.

How to change the default IME programatically? (top)


cherian_2002 wants to change the default IME to japanese.

Hi All,
I need to change the default IME to Japanese.
So that when a user presses keys in a text box, japanese
letters will be coming automatically.

By default english letters will be coming. After we set to MS IME
Japanese, then only japanese letters will be coming.

I am having Japanese m/c.

The solution is to set different conversion status flags for Hiragana and katakana in the ImmSetConversionStatus() function.

And that's how it looks like:

For Hiragana set flag as

For katakana set flag value as

How to execute a 16-bit or DOS application in a 32-bit program? (top)


Kurosan is trying to run a 16-bit, third-party DOS application in his 32-bit application under Win98 with the function system or ShellExecute(), but unfortunately, he gets the following error:

This program has performed an illegal operation and will be
shut down.
I have a third party 16-bit/DOS application which runs fine in
the DOS console under Win98. When using the system() or
ShellExecute() to execute the third party app in my 32-bit program,
an error "This program has performed an illegal operation and will
be shut down" occurred.

How should I make such a call for a 16-bit or DOS app?

Also can Visual Studio create a real DOS project?

The problem is that you need version 1.52 to create a "real DOS application," which ships with Visual Studio 5 and 6.

How to find out if the object is dynamic and was created on the heap? (top)


olstar asked a very interesting question. He has created a pointer to an object and wants to delete it only when it was created on the heap. How do you think this can be done?

I have a pointer to an object and I need to call delete operator
only if the object was created on the heap. Is there any way to
programmatically define this?

I'm using MFC in my app.

The first suggestion comes from MikeAThon. He suggests including a member variable in the class—something like m_bCreatedOnHeap, then set the preferred default in the ctor and override it in your code when the object is created (if it's created inconsistently with the default). In the desctructor, test the value of the variable and delete when neccesary.

This can look like this:

class CMyClass{
BOOL m_bCreatedOnHeap;

CMyClass::CMyClass() : m_bCreatedOnHeap(TRUE)

if (m_bCreatedOnHeap) delete this;

// in code
CMyClass cmy;
cmy.m_bCreatedOnHeap=FALSE;       // must overrride since it's
                                  // on the stack

CMyClass* pcmy = new CMyClass;    // default is ok as it is

But, oktronic sees a problem with this code. If you dynamically allocate the class, how will the destructor get called so it can delete itself? If you call delete on the ptr, there will be a stack overflow because delete calls the destructor inside the destructor. So, if it's dynamically allocated, it will call the destructor over and over...

The secnd and better solution by oktronic for this is to overload the delete operator for the class. Do as Mike suggested and put a boolean in the class. Also, in the class, do this:

class ...{
void operator delete(void* obj);

void CMyClass::operator delete(void* obj){
    CMyClass* delObj = (CMyClass*)obj;


void main(){
    CMyClass* ptr = new CMyClass;

    delete ptr;

    CMyClass static;
    ptr = &static;

    delete ptr;    // calls the same delete function, won't
                   // free memory

What is a namespace? (top)


Although this is a simple question asked by avi123, I think the answer provided by Andreas Masur is very very nice and explains the topic in details with some nice examples.

1. what is a namespace?
2. why when I use std:string, I also need to use namespace std
I mean:

#include <string>
using namespace std;

1.) What is a namespace?

Namespaces allow you to group a set of global classes, objects, or functions under one name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces.

The form of a namespace is:

namespace identifier

where identifier is any valid identifier and namespace-body is the set of classes, objects, and functions that are included within the namespace. For example:

namespace general
  int a, b;

In this case, a and b are normal variables integrated within the general namespace. To access these variables from outside the namespace, you have to use the scope operator ::.

For example, to access the previous variables, you would have to put:


As you can see, the answer is going very deep in the topic and I would like ask you to read the whole answer by Andreas in the thread.

Why should I use the STL? (top)


darwen started a very interesting topic. He does not really like the STL because he does not like the std namespace. Instead, he likes the MFC, because you can do much more with that!

I hate the standard template library.
I hate the fact you can't use DEBUG_NEW with it to track down
memory leaks.
I hate all the std::blah blah stuff when there's classes in MFC
which do virtually the same job but much, much nicer.

I don't even like CArray.
However CPtrArray, CObArray and CStringArray are very, very,
very useful.

You can't inherit from any of the STL classes because they don't
have virtual destructors so you can't for instance do nice things
that you can in MFC like:
class CServerNames : public CStringArray
    virtual ~CServerNames();
where this class would load in all server names from wherever.
In one line.
Wherever you wanted to put it in the code.
In an MFC environment I don't see any reason to use it. Ever !

Paul McKenzie, a regular contributor and an experienced developer, started at first with his comment. In opposition to Darwen, he likes STL and here is why.

  1. There's a whole lot of code that is not STL where DEBUG_NEW cannot be used.
  2. If you want all that overhead of CObject added to your classes, go right ahead. Most C++ programmers do not want their classes hamstrung being derived from CObject.
  3. Sorting in STL is much easier than ever. Try to sort a CPtrArray, or CStringArray, or CList. You can't unless you write your own code. In STL, this can be achieved in just with a function such as std::sort.

These are just few comments about why STL is better than MFC, but there is much more to say than that. Please take a look at the whole thread, which now contains two pages.

Which STL is better? (top)


cvogt61457 wants to know which STL is better.

There are several different providers for STL for use with MSVC.

STL Port
MS STL (comes with MSVC)

Is any of these better than the others?

I know with the MS STL, you have to set the complier to ignore
the complier warning C4786. Is this required with the other

Since MSVC already has STL, how would I access another
provider's header files? By putting the path to the other
provider's headers first in the include path?? (I certainly don't
want to try to delete the headers from the include directory.)

It always depends on what you need. The STL that comes originally with VC6 is not very good. The provider (Dinkumware) provides an upgrade for that STL, though, which makes it pretty good. If you are using VC.NET, I think there is no real need to change the STL implementation anymore. But remember, the upgrade of Dinkumware STL for MSVC 6.0 is a paid product. No free upgrade here!!!

This article was originally published on October 17, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

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