Advertisement

delete [ ] array

Started by July 23, 2000 12:26 AM
11 comments, last by 40 Thieves 24 years, 6 months ago
jumble:

Must be my mistake...

VC displays every element of your pointer array as having the same memory address, but that is probably just a bug in the variables window. Actually, your solution does work fine, and it''s also quite a bit better than mine for arrays bigger than 2 dimensions, but it only works when the size of all dimensions but the first is constant.


Mr. Cucumber:

Actually, only the first dimension in an array can be variable. All other dimensions must be constant. Don''t ask me why...


The_[PI]_ehT:

Yes, my solution is not totally allocated on the heap (actually, nothing seems to be...you always need a stack-based pointer to it, or a stack based pointer to a heap-based pointer to it, or...etc.). However, the array dimensions do not have to be constant in my example - that was merely to give the 8''s a name so the reader could tell the first dimension from the second dimension. You could just as easily have used variables for the dimensions.

BTW, you can also use new to allocate an array of pointers:


gameboard = new TEAM*[many_times];



And delete them:


delete[] gameboard;



There doesn''t seem to be any reason to use malloc in C++ outside of the new operator.


Wilka:

Always the STL person, huh?

...just kidding! I like vectors better, too, but I think that one has to learn how arrays work before learning vector.



- null_pointer
Sabre Multimedia
quote:
Wilka:

Always the STL person, huh?


Yep

I''ve my fair share of bugs cased by dynamic arrays, and since I started using STL I''ve found a reason to use new[] over vector. So I''m probably trying to get as many people as I can to start using STL as well

quote:
...just kidding! I like vectors better, too, but I think that one has to learn how arrays work before learning vector.


I agree with you here, but I''m not sure if we''re right .

Would it really make much of difference if you''ve done it manually first before you start using vector? It''ll give you a better understand of what vector actually does, but you really need to know to be able to use vector properly?

If things like std::vector and std::string where using instead of new[] and char* when teaching people C++, it would probably make it a lot easier for them to learn - they wouldn''t need to worry about memory management etc. (which is probably one of the hardest things for people new to C++). Obviously they should be taught about new[] and char* (maybe have them make their own vector class), just not during the first lesson
Advertisement
quote:
VC displays every element of your pointer array as having the same memory address, but that is probably just a bug in the variables window.


It''s not a bug, it''s a feature

In a debug build, MSVC sets all pointers to "0xcccccccc" by default. It''s to help with debugging, if you see a pointer with that value you know it hasn''t been properly initialised.

I''ve also seen the value "0xcdcdcdcd" in a few places, but I''m not sure what it means (maybe after a delete?).

I''ve just had a quick check, and it looks like "0xcdcdcdcd" is the default for variables allocated on the heap, and "0xcccccccc" is for variables on the stack of any type (not just pointers).

This topic is closed to new replies.

Advertisement