Solved

Very simple, but i donw know.

Posted on 1998-08-06
10
174 Views
Last Modified: 2010-05-18
Very simple, but i donw know.
How to do:
unsigned short asize = 999; //or use const
int i1[asize];
0
Comment
Question by:learn
10 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 60 total points
ID: 1169569
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169570
You can''t declare a local or global array (using []) with a variable size, their sizes must be constant.  That is

const int ConstInt = 5;
int VarInt = 5;

SomeClass Array1[ConstInt];  // okay.
SomeClass Array2[VarInt]; // Compiler error.

But that is okay, you don't need to.  continues.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169571
What you can do is declare a pointer to an array that is dynamically allocated using the "new [] operator". "new []" can take a variable length for the array.  

Before I show you an example, I should mention that in C and C++ pointers and arrays are closely related.  When you declare an local or global array, like

int IntAry[5];

You are creating an array of 5 ints.  But "IntAry" is not the array. IntAry is really a pointer.  It is a pointer to the first entry in the array.  You can access items in the array using either of these syntaxes

int i1 = IntAry[2];  // Get third item.
int i1 = IntAry+2;  // Get third item.

In the first case, with the [], it makes it seem like IntAry is an array.  And the second case, +2, it makes IntAry seem like a pointer.  but both cases are the same.  IntAry is really a pointer and the two cases generate the same code for the computer.

That means that you can use new to generate a dynamic array in memory and get back a pointer to that array.  And then use that pointer just like you are used to with arrays.  

Continues.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1169572
unsigned short asize = 999;

//  asize could be altered now.
int * i1 = new int[asize];

Now even though i1 is not an array, it can be used syntactically like one, like

int Int1 = i1[1]; // array like.
int Int2 = i1[2];
int Int3 = *(i+3);  // pointer like.

The difference between this approach and you original approach is that you must remember to destroy this array when you are done using delete [].

Now I have to go!!!  I'll get back to you about the other question in a few hours.
0
 
LVL 2

Expert Comment

by:warmcat
ID: 1169573
There's an uglier, older, but sometimes ass-saving method... use manifest constants which operate at the preprocessor level, ie

#define MAX_ARRAY_SIZE 999

int i1[MAX_ARRAY_SIZE];

This can sometimes bail you out of otherwise intractable problems because you can perform the #defines outside of the compiler, in the compiler command line.  This lets you compile the same source code in different ways depending on the values of defines.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Expert Comment

by:bequette
ID: 1169574
Depending on what your problem is CArray class might be an answer, but if your not familiar with C++ classes and templates it may be a bit confusing.  Try looking in the documentation for Carray.  Basically what it is, is a class that looks like a natural array, so once you declare it you can index like normal:
MyCArray[53]=255; and so on, but the differance is it allocates more space if you need it.  This means you can declare it with say 500 elements with a grow rate of 10 elements.  What happens is if you end up needing more than 500 elements, it will automatically make the array larger, 10 elements at a time.  This will help if what you are trying to do is allocate space but you just don't know how much you need until runtime.  Now in the case that you need to allocate an exact amount of space, but you don't know this exact amount until runtime, you need to use the Win32 Memory Allocation functions.  It's a bit more complicated than the CArray, but still not too hard to follow.  Let me know if you need more info.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169575
Warmcat there is absolutely no reason to use a #define like that.  You can use a C++ constant instead.  Like

const int MAX_ARRAY_SIZE = 999

int i1[MAX_ARRAY_SIZE];

This has the advantage of  type and scope safety that the #define does not have.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169576
bequette, the CArray class is an MFC class.  Learn does not appear to be using MFC (from other questions and the fact that this is the C++ topic area)  What he could be using is the vector container in STL.  But A he does not have the STL and B I believe he is trying to write a similar container himself (good learning experience).

>>Now in the case that you need to allocate an exact amount of space, but
>> you don't know this exact amount until runtime, you need to use the Win32
>> Memory Allocation functions
Why the Win32 memory allocation functions? C++ has new and delete.  
0
 

Author Comment

by:learn
ID: 1169577
Hi Nietod

>unsigned short asize = 999;
>    //  asize could be altered now.
>    int * i1 = new int[asize];

But how to resize the array latter? Using delete, new and copy? If the array is big and the elements in it is long also......

Cheers.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169578
Yes unfortuanely, new, copy, then delete are the only safe ways.  (There are some special case ways, but they are not general purpose and should be avoided).  This can get a little slow.  (Far less than you might think however, I'm ussually surprised at how fast this stuff can be done.)  
However, there are things that you can do to make it much faster.  First of all, remember what I suggested about allocating extra space in the string array that you use for expansion, that way you don't have to allocate a new array and copy every time a letter is added to the string?  well you can do that to the array as well.  Store two lengths.  One, the storage length, is the number of items stored in the actual array (the number of items you allocated)  This is not the items that the users of the array consider to be stored in the array.  This is the number of items that can be stored before it will be necessary to resize the array.  The other number is the number of items that are currently stored in the array class.  This is the number of items that the users of the classs will care about.  As far as they are concerned, this is the number of items storedl  For example, you create the array with 10 items and record that there is room for 10 items, but you indicate that there are no itmes stored.  (that is storage length is 10, but current lengthis 0.)  Then the user can add 10 items to the array, before the first time you need to reallocate and copy.  Make sense?

Another thing you can do is use reference counting.  Reference counting is a technique that can be used to make it possible to copy items very fast (Actually, it makes it seem like you copied the item, but doesn't really copy it.  If you add refernce counting to the string class it will be much faster to copy the strings from the old array to the new array.  Many things will actually become much faster.  I would make that a goal, but I would wait before doing it.  You don't have to do it right away.  you can add it later without changing the code that uses the string class.  That is, you will change the string class, but the code that uses the string class will stay the same.  The books suggested before deal with reference counting and other optimizations.  I would read them before starting this.  (I would be happy to help you, but you would do best to read the books first in any case.)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now