Advertisement

destroying pointers in classes...

Started by February 22, 2001 11:34 AM
7 comments, last by treadwell99 23 years, 11 months ago
Need some help...two questions about destroying pointers and classes in C++: For example, I''ve got char * strarray[] = { "zero", "one", "two" } (an array of char strings) in one header file and a char ** ptrstr (pointer to a pointer of char) in a class in another file that will point to this array. After I''m done using these variables: Question 1) How do I delete the char ** in the destructor of my class? Question 2) If I do put the string array in a class, how do I delete this string array? Thanks, in advance. -treadwell99
Hmm, I''m not really sure if I understand exactly what you want to do.
Is the char *str[] allocated with new?
Do you really want the class to destroy the global array?

If you, by any chance, want to destroy each element in the array (each one allocated with new) :

MyClass::~MyClass() {

for (int C=0; C < numberOfStrings; C++) {
delete[] myGlobalArray[C];
}
numberOfStrings = 0;
}

If you have (as you wrote in your question) :

char *strarray[] = {"zero","one","two"}

then you cant really deallocate the array,
just NULL your class member pointer like :
ptrstr = NULL;
But that is not needed anyway since the object destructed never will be used again.

Hope I understood your question, otherwise please correct me

/ Tooon
Advertisement
Neat, thanks, Tooon. Didn''t know it was that easy. I haven''t used pointers to pointers much--let alone inside a class; got confused, I guessed.

Just to clarify my problem and answer your questions:
quote:
Is the char *str[] allocated with new?

No, it''s not newed (should I''ve new it???). It''s just declared and defined (it won''t be updated) in a header file that will be global to other files to use--probably make it static.
quote:
Do you really want the class to destroy the global array?

No, right now, I don''t want the class to destroy the array. I just needed a class member pointer to point to the array. Either that, or pass a copy of the array into my class, but I figured a pointer to this global array would be better.

My goal is to make the char** class member to point to multiple string arrays.

-treadwell99
If you don''t do a new then you can''t do a delete. I believe putting it in the header will cause an error when you link it. Instead you should put it in a source file then put extern **strarray in the header so that other compile units can access it.
Keys to success: Ability, ambition and opportunity.
WARNING!

Pointers and arrays are not the same thing. Don''t declare an array somewhere and then an extern pointer reference in another file, or you will be tearing your hair out chasing down obscure bugs.

Here''s why:

Suppose you declare your array of string pointers as above. The somewhere in memroy you will have an array like this (the actual text strings might be anywhere, it''s not important):

ptr to "zero\0"
ptr to "one\0"
ptr to "two\0"

Now when the linker attempts to resolve the external reference, as far as it knows it is a pointer, so off it goes to the array and retrieves ''ptr to "zero\0"'' as the value of the pointer.

When you try to dereference what you believe is a pointer to a pointer, you will get the memory location of the string "zero\0" - dereference this and instead of getting the string you wanted, you will get what ever is at location 0x7a65720f or 0x0f72657a (depending on the endian-ness of your processor). Probably not what you wanted.

In short, if you have a variable declared as char * [], make sure your external declaration is a char * [], not a char **. Nuff said.


--
Get a stripper on your desktop!
An array identifier is a pointer to its first element. The array indexing operator [] merely uses pointer addition to find the other elements based on the size of the type.
Advertisement
Global const char strings are owned by the system. If you are interested, the data of the string is most likely compiled into your executable and that's what the pointer points to. The memory consumed by the string is in all cases owned by the system and you should never attempt to manipulate it or delete it.

Defining it as a const pointer to a const char will eliminate linker errors caused by placing it in a header file:


// in some header included in multiple translation units (.cpp files):
const char* const somestring = "Hello, world!";



However, defining it this way will cause linker errors because the pointer itself is not constant (although the memory it points to IS):


// Causes linker errors if included in multiple translation units
const char* someotherstring = "Hello, world!";



Do you understand why this is so? If you defined something as non-const in a header file that was included in multiple translation units, one of those units could modify its value while another unit depends on its value. Since the compilation order of translation units is defined by the compiler, you would get undefined behavior. Your code would compile on some compilers, or even only sometimes on certain compilers and only ever by "luck."

However, if you define it as const then it cannot be modified, so the compiler happily accepts const variables defined in header files.

Edited by - null_pointer on February 23, 2001 1:42:12 PM
quote:
Original post by null_pointer

An array identifier is a pointer to its first element. The array indexing operator [] merely uses pointer addition to find the other elements based on the size of the type.


Yes, and that makes it very nice for iterating through with ptr++. But this statement doesn''t hold true for external linkage. This is discussed in "Deep C Secrets" and also came up in either C/C++ User''s Journal, or Windows Developers Journal (I think it was the former). I have also made this mistake myself, even after reading "Deep C Secrets." I declared

int featureArray[50];

and then in another file

extern int *featureArray;

and my program wouldn''t run. My problems went away when I realised this and changed it to be

extern int featureArray[];

Bottom line is it''s better to learn that they are not the same thing early on, even though implicitly the identifier is treated as a pointer (e.g. featureArray is the same as &featureArray[0]).

--
Get a stripper on your desktop!
Good point. You are quite right, that wouldn''t compile and if it did it wouldn''t produce the correct results.
Keys to success: Ability, ambition and opportunity.

This topic is closed to new replies.

Advertisement