0

So I have tried to create a generator like:

#include <iostream>
#include <iomanip>  
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
char* data;
void genRandomFilledChar(char *s, int i, int j, int k) { 
    const char alphanum[] =
        "0123456789"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";
    for (int q = 0; q < i; ++q) {
        for (int w = 0; w < j; ++w) {
            for (int e = 0; e < k; ++e) {
                s[e] = alphanum[rand() % (sizeof(alphanum) - 1)];
            }
        } }}

int main()
{
    data =  new char[10000][10000][10000];
    genRandomFilledChar(data, 10000, 10000, 10000);
    cin.get();
    return 0;
}

But it fails to compile. What am I doing wrong?

cpx
  • 17,009
  • 20
  • 87
  • 142
Rella
  • 65,003
  • 109
  • 363
  • 636

5 Answers5

2

You can't allocate new array like you do. You should declare one dimensional array of proper size or create array of pointers to pointer to array.

char *data = new char [1000*1000*1000]; 

or

char ***data = new char**[1000];
for (int i=0; i< 1000; i++) {
    data[i] = new char*[1000];
    for (int j=0; j< 1000; j++) {
        data[i][j] = new char[1000];
    }
} 

Also you have error in genRandomFilledChar. You fill only first 10000 chars with random values every time.

And, the last. For rand() you need to initialize random generator with srand.

DReJ
  • 1,966
  • 15
  • 14
1
char* data;
// ....
data =  new char[10000][10000][10000];

Are wrong. First, a three dimensional array of chars is char***. Next, you need to initialize that memory in 3 steps.

data = new char**[10000];
for(int i=0; i < 10000; ++i){
  data[i] = new char*[10000];
  for(int j=0; j < 10000; ++j){
    data[i][j] = new char[10000];
    for(int k=0; k < 10000; ++k){
      // and for best performance, initialize directly
      data[i][j][k] = /*HERE*/;
    }
  }
}
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • Your code will not work like it should. You create new `data` pointer 10000 times and reassign it every time. Also you didn't fill `*data` and `**data` correctly, you have lost `i` and `j`. Please, fix your code. – DReJ May 13 '11 at 06:22
  • @DReJ: Oh my god, now that was embarassing, must be the lack of sleep. Thanks for pointing the errors out. >_>" – Xeo May 13 '11 at 06:25
  • Multidimensional arrays and nested pointers are not the same thing at all, see our [FAQ on arrays](http://stackoverflow.com/questions/4810664) for details. – fredoverflow May 13 '11 at 08:31
1
s[e] = alphanum[rand % (sizeof(alphanum) - 1)];

should be

s[q][w][e] = alphanum[rand % (sizeof(alphanum) - 1)];

because it is a 3 dimensional array

Jordonias
  • 5,778
  • 2
  • 21
  • 32
1

Because new char[10000][10000][10000] has the type char (*)[10000][10000], not char*, the type of data. And I think what you want is

void genRandomFilledChar(char *s, int i, int j, int k) { 
    const char alphanum[] =
        "0123456789"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";
    for (int q = 0; q < i; ++q) {
        for (int w = 0; w < j; ++w) {
            for (int e = 0; e < k; ++e) {
                s[e + w * k + q * j * k] = alphanum[rand() % (sizeof(alphanum) - 1)];
            }
        }
    }
}

int main()
{
    data =  new char[10000 * 10000 * 10000];
    genRandomFilledChar(data, 10000, 10000, 10000);
    cin.get();
    return 0;
}
neuront
  • 9,312
  • 5
  • 42
  • 71
1

C multidimensional arrays are just a way to calculate index automatically. Why not declare char data[i*j*k] (or get from heap with new) and then fill it as if it is single dimension? You can later use [][][] indexes with data.

Andrey Sidorov
  • 24,905
  • 4
  • 62
  • 75