Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need some Help with Dynamic Arrays

Status
Not open for further replies.

OveRride

Programmer
Nov 2, 2000
1
IT
Well, I've been programming in most basic environments, VBasic 4/5/6... Delphi 5... and now I'm approaching MSVC++ and I'm facing some troubles understanding arrays...

well, let's get to the point, in most languages, for example in vb, I could declare an array without specifying the exact size, and then use Redim ArrayName(newsizes) to redim it. Optionally I could use the Preserve keyword to avoid clearing the array after redim.

How do I make a redim-able array in MSVC++ ???

this code

int myint[];

produces a error C2133: 'mod' : unknown size

also, If I use it in a class interface definition, it does not produce errors, but just a warning

warning C4200: nonstandard extension used : zero-sized array in struct/union

So I've been looking in the help and some other guides, and I have the feeling that a possible solution would be using malloc, or the "new" operator... but since I got my mind messed up with that stuff, I'll wait for you Gurus to tell me your opinion.

Thanx in Advance.
 
Hi,
I once majored in C/C++, but have not programmed in it for ages - so I am rusty. I'll take my chances at being wrong here... the only real dynamic array that can be created in C/C++ is a character string. To overcome this you must use a linked list, this is where you would use the "new" operator.

Rob Marriott
rob@career-connections.net
 
Dear OveRride,

Yes, you are one the right track...

int* pint = NULL;
pint = new int[20];

or..

int nCount = 20;
pint = new int[nCount];

don't forget to clean up

delete pint;

> Optionally I could use the Preserve keyword to avoid clearing the array after redim.

Now that is a little different. That's where classes come into play. A C++ class can be designed to encapsulate the concept of a 'redim-able' array and hide all the details of memory allocation and copying values to grow the size of the array.

Hope this helps
-pete
 
Hi,

I've not done any c++ programing for awhile but I seem to remember that the line

delete pint;

should be

delete []pint;

I KNOW from past blunders that these little things can cause a lot of problems.

Dick
 
Dear Pappy1942,

That would be true if the elements of the array were objects so that their destructors would be called.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top