Solved

dynamic array declration

Posted on 2004-03-26
10
425 Views
Last Modified: 2013-12-14
i want to know if it is possible to declare an array of the type

int array[ length ]; where length is a variable.

i don't want to use malloc and calloc as they complicate the program.

i get an error message "Constant Expression Required"

is there any way of making the length constant so that the above statement is valid ??

i need this for a class called Timer

i want to declare it as

Timer T[5][length];
Where length is  a variable.

is it possible ??
( without using malloc and calloc as far as possible )

0
Comment
Question by:radarsh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 64 total points
ID: 10690847
>>i want to know if it is possible to declare an array of the type
>>int array[ length ]; where length is a variable.

Not if you are using VC++. You might want to use std::vector, e.g.

#include <vector>


std::vector<int> array(lenght);
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10690855
Your compiler tells you that it's not possible...

You are asking this in the C++ area, so why not just use a C++ feature? The STL vector is like an array, but it allocates it's memory dynamically in the background. This means that you don't have to take care of this yourself.

#include <vector>
using namespace std;

vector<Timer> T;

... then just use T like an array:
T[0] = ...
T[1] = ...
T[2] = ...

0
 
LVL 1

Assisted Solution

by:GJanusz
GJanusz earned 62 total points
ID: 10691205
I, too, would use vector.  How you use it is another matter, of course.  There are probably many ways.  Personally, working with a variable array of a constant array of class instances confuses me sometimes (hopefully not for the duration of this post).

I might consider coding it this way:

typedef struct {
      Timer      y[5];
} Timer5;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:GJanusz
ID: 10691232
And the rest of my post ... (note that the typedef struct could be a class instead if you like)

vector<Timer5> T;
T.resize(nLength);

Then you can reference Timer instances with references that look like:
T[29].y[4]

In real life, I tend to run into situations more where I want to have a vector of vectors, in which case, I make my life simpler by defining something like this:

typedef vector<something> T_vSomething;

Then I can allocate a vector of a vector of somethings with:
vector<T_vSomething> myvv;
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 62 total points
ID: 10692553
The C++ equivalent of malloc is the new operator. You can do something like this:

class T
{
private:
    char* ps;
    int  i;
public:
    T() : ps(new char[10]), i(0) {}
    ~T() { delete [] ps; }
};

int main(int nArgs, char* szArgs[])
{

    typedef T T5[5];
    long len = 2;
    T5* pT5 = new T5[len];
   
    ...
    delete [] pT5;

    return 0;
}

Then you can have two dimensions - the first must be constant.

A dynamic container like vector is better, but vector<T> is an one-dimensional array.

Regards, Alex

0
 

Expert Comment

by:fsugolf06
ID: 10693172
heres a pretty simple solution here:

//first declare a pointer

int * array;

//then dynamically allocate the array like this:

array = new int[10][someVariable];

that should work
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 62 total points
ID: 10693542
>>array = new int[10][someVariable];

That works, but it leaves a potential for memory leaks just like malloc.

Here's another method:
http://axter.com/code/dynamic_2d_array.h

With the simple class in the above link, you can easily declare a 2-D array like this:

dynamic_2d_array<Timer> T[5][length];

For more information, and to see other methods for dynamically creating a 2D array, see the following link:
http://www.axter.com/faq/topic.asp?TOPIC_ID=60&FORUM_ID=4&CAT_ID=9
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c++ syntax question 9 63
FMX TCameraComponent Problem 2 104
How do I get Window Title of all opened process? 4 57
why doesn't assert terminate here? 2 16
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
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 viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

733 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