0

I would like to have an array int candidates[9][] where the first dimension is known (9) and the second, depends on the execution.

I found that a method to allocate the array was the following:

int *candidates[9]; /* first allocation at declaration */
for(int i=0;i<9;i++) candidates[i] = new int[6]; /* allocation at execution */

but when I use it like that, and I try to access to candidates[i][j], it doesn't work. I initialize candidate[i] with a function fun() that return and int[] of the right size, but the content of candidate[i][j] is wrong.

candidates[0] = fun();

I don't understand where I am wrong... Thank you for your help :-)

1
  • I suggest a std::array<std::vector<int>, 9>. Commented Jan 14, 2013 at 10:05

3 Answers 3

1

Try int *candidates[9] instead of int candidates[9][] and it should work.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't understand your suggestion... where should I try it?
0

Why dont you try vector template class from STL...code is more neater and comprehensive...

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> arrayOfVecs[9];

    //use each array to put as many elements you want, each one different
    arrayOfVecs[0].push_back(1);
    arrayOfVecs[1].push_back(100);
    .
    .
    arrayOfVecs[1].push_back(22);
    arrayOfVecs[0].pop_back();
    arrayOfVecs[8].push_back(45);

    cout<<arrayOfVecs[1][0]<<endl;//prints 100

    return 0;
}

WITH ARRAY OF POINTERS

int main()
{
    int* arrayOfPtrs[9];

    for(int index = 0;index<9;index++)
    {
        int sizeOfArray = //determine the size of each array
        arrayOfPtrs[index] = new int[sizeOfArray];

        //initialize all to zero if you want or you can skip this loop
        for(int k=0;k<sizeOfArray;k++)
            arrayOfPtrs[index][k] = 0;

    }

    for(int index = 0;index<9;index++)
    {
        for(int k=0;k<6;k++)
            cout<<arrayOfPtrs[index][k]<<endl;
    }

    return 0;

}

3 Comments

I would like to use a solution that is the faster at execution as possible. This is really important. Isn't the std:vector solution slower than what I can have with the array of integer method?
it is slower in the aspect of deletion and insertion from between the list otherwise data access is same as an array of constant size...how ever from your code it seems you are creating an array of pointers...if ur app demands that each of the nine 1D array will be of the same length then you can go for array of pointers...vector template provides you flexibility more than array of pointers..
the second dimension (fixed at execution time, and in my example : 6) is constant for all the elements of the first dimension. The second dimension is fixed one and for all at execution time. It is not changed after... But really, the access time (writing and reading the elements) is very important for my application. Anyway, how can I make the array of pointers work?
0

Try int **candidates=0; followed by candidates = new int *[9] ;.

Code:

#include <iostream>
using namespace std;

int main(void)
{


    int **candidates=0;//[9]; /* first allocation at declaration */
    candidates = new int *[9] ;
    for(int i=0;i<9;i++) candidates[i] = new int ; /* allocation at execution */



    for(   i = 0 ; i < 9 ; i++ )
    {
        for( int  j = 0 ; j < 9 ; j++ )
        {

            candidates[i][j]=i*j;
            cout<<candidates[i][j]<<" ";
        }
        cout<<"\n";
    }



    cout<<" \nPress any key to continue\n";
    cin.ignore();
    cin.get();

   return 0;
}

3 Comments

Thank you. I forgot to mention that the variable is inside an object. Therefore, the int **candidates=0; : error: ISO C++ forbids initialization of member 'candidates' [-fpermissive]. How can I do ?
without the =0 assignation, it makes a segfault :-/
You just remove the '=0;' so it becomes just 'int **candidates;'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.