User forums > Help

Heap size

<< < (2/3) > >>

mdelfede:

--- Quote from: lev9596 on September 18, 2006, 08:31:49 pm ---Still having problems getting this to work.  I am a relatively new user to this program and programming in C++.  I feel really dumb...has anyone else had a problem with this b4 or is it just me??

--- End quote ---

I think that's more a general programming question than a C::B one.
BTW, I think you're allocating a very (too ?) big static array. Maybe looking at your code snippet would help....
Anyway, that's not a good programming practice to allocate a similar array; much better work with dynamic data, allocating just what you need.

Ciao

Max

thomas:
Right, but since he is addressing a 2D array, it is not quite so simple, he'll have to use an array of pointers to dynamically allocated memory or something (or calculate the row offset by hand).

I am not sure if that's the easiest thing for a beginner to do...

rcoll:

--- Quote from: thomas on September 18, 2006, 09:08:19 pm ---Right, but since he is addressing a 2D array, it is not quite so simple, he'll have to use an array of pointers to dynamically allocated memory or something (or calculate the row offset by hand).

I am not sure if that's the easiest thing for a beginner to do...

--- End quote ---

It is not the easiest thing for a beginner, but it is still standard practice.  Like the rest of us that have learned C, he'll just have to jump in and take the time to figure it out.

Jedon -- how big is this array we're talking about?  If it is really huge, and the code is complicated, we'll move this to a regular programming forum and help you through it.

-- ron --

mdelfede:

--- Quote from: thomas on September 18, 2006, 09:08:19 pm ---Right, but since he is addressing a 2D array, it is not quite so simple, he'll have to use an array of pointers to dynamically allocated memory or something (or calculate the row offset by hand).

I am not sure if that's the easiest thing for a beginner to do...

--- End quote ---

Not the easiest, but the only 'good' way. On my 'first' programming days I made some structural analysis software, beginning with staticalli allocated matrices, and it was a nightmare :-)
Then, having only C and not C++, I resorted to an 1-dimension dynamic array, simulating a 2 dimension array; the index calculation is quite simple.
The simplest (and more reliable way) now is to write some C++ array class, or to get a ready to use one somewhere.
It would also be a good programming practice for a 'not-absolute-beginner'.

Ciao

Max

lev9596:
Actually the array i need to create needs to store the number of permutations 1 through 9 contains.  so there are a 9 factorial possibilities i need to store.  Maybe im just going about it wrong. but this is my code so far.

#include <algorithm>
#include <iostream>



using namespace std;

int main() {
    int numArray[] = {1,2,3,4,5,6,7,8,9};
    int *mainArray = new int[362880*9];
    int a = 1;

    for(int i = 0; i < 9; i++){
        mainArray[0*i] = numArray;
    }
    while(next_permutation(numArray, numArray+9)) {
        for(int i = 0; i < 9; i++) {
            mainArray[a*i] = numArray;
            a++;
        }
        }
    for(int i = 0; i < 9; i++){
    cout << "First " << mainArray[0*i] << endl;
    cout << "Ten Thousandth: " << mainArray[10000*i] << endl;
    cout << "Last " << mainArray[362880*i] << endl;
    }
    delete mainArray;
}

This code is what is giving me crashing problems... and i have no clue why.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version