Solved

Global Arrays  in Borland C++ Builder (Variable array)

Posted on 2002-06-16
11
1,136 Views
Last Modified: 2011-04-14
I want to create a variable 3 dimensional array in c++ that i can change and access any where in the program

this is what I want to do, I want to enter a string and then from there should the 3 dimenional array be be build.
If I enter a new string a new array should be build.

At the moment I have to specify the lenght of the array
this should be done automatically

the code i've got I think is static

My code sofar

{
   char charset[] =  "groen";  //I must be able to change this
   int str_length = strlen(charset);
   char cubearr[5][5][5];
   // I must be able to use this array through the program
   // edit, re-initialize the array

   int i=0;

   int x=0;
   int y=0;
   int z=0;

   for ( x = 0; x < str_length; x++)
   {
     for ( y = 0; y < str_length; y++)
     {
       for ( z = 0 ; z < str_length ; z++)
       {
         if (i==5)
           i=0;
         cubearr[x][y][z] =  charset[i];
       }
     }
   }
}  

I want to make this dynamic and accessable all over in the program

thanks
0
Comment
Question by:morees
  • 4
  • 2
  • 2
  • +3
11 Comments
 

Accepted Solution

by:
Boots111 earned 65 total points
Comment Utility
Morees~

To make the object visible throughout the program you will have to make it a global.  As for dynamic, you have two options.

The easier and cleaner option:

#include <vector.h>
//#include <vector> on some systems

vector<vector<vector<char>>> cubearr;

that line will declare a vector of vectors of vectors.  Thus giving you a 3-d array.  Each vector can be resized via the resize function.  Remember that you have to resize each sub vector independantly.  (If I were you, I would wrap it in a class to do most of that stuff for you)

The uglier way:

char ***cubearr;

that will give you a pointer to a pointer to a pointer of char arrays.  Each pointer must be allocated and deallocated independantly as above.

Best of luck,
Boots
0
 

Author Comment

by:morees
Comment Utility
This is the error I get when I use this decleration

#include <vector.h>

vector<vector<vector<char>>> cubearr;


E2299 Cannot generate template specialization from 'specifier'     Compiler error
0
 
LVL 3

Expert Comment

by:Crius
Comment Utility
Ok, well, Boots111 has it pretty much correct. The only thing I can do along his answer is provide for you the allocation code:

char ***cubearr;
char charset[] =  "groen";
int str_length = strlen(charset);
int i,j;

cubearr = (char ***)malloc(str_length*sizeof(char **));
if(!cubearr)
   //output that you are out of memory/allocation failed
for(i=0;i<str_length;i++)
{
   cubearr[i]=(char **)malloc(str_length*sizeof(char *));
   if(!cubearr[i])
      //output that you are out of memory/allocation failed
   for(j=0;j<str_length;j++)
   {
      cubearr[i][j]=(char *)malloc(str_length*sizeof(char));
      if(!cubearr[i][j])
         //output that you are out of memory/allocation failed
   }
}

Note: You will need to fill in the //output that you are out of memory/allocation failed with whatever error reporting mechanism you have (I don't know if you're in a windows app or not), and likely exit/deallocate the memory. I can provide the deallocation routines if you want.

The only other option as far as simulating a 3 dimensional array is to make a one dimensional array, like so:

cubearr = (char *)malloc(str_length*str_length*str_length*sizeof(char));

Once you have this one dimensional array made, you can access it by using multiples of the str_length, that is:

cubearr[x*str_length*str_length+y*str_length+z];

Let me know if you still have questions.
0
 

Expert Comment

by:Boots111
Comment Utility
Morees~

Sorry apparently the compiler you are using does not fully support the language specification (I cannot really blame it, very few C++ compilers do)

The following program runs and works in VC++ 6.0:

#include "stdafx.h"
#include <iostream>
#include <vector>

using namespace std;

typedef vector<char> D1;
typedef vector<D1> D2;
typedef vector<D2> D3;

int main(int argc, char* argv[])
{

     cout << "Hello World!" << endl;

     D3 v3;
     
     v3.resize(3);
     for(int i = 0; i < 3; i++)
     {
          v3[i].resize(3);
          for(int j = 0; j < 3; j++)
          {
               v3[i][j].resize(3);
               for(int k = 0; k < 3; k++)
                    v3[i][j][k] = 't';
          }
     }

     for(i = 0; i < 3; i++)
     {
          for(int j = 0; j < 3; j++)
          {
               for(int k = 0; k < 3; k++)
                    cout << v3[i][j][k] << ' ';
               cout << "1";
          }
          cout << endl;
     }

     char z;
     cin >> z;
     return 0;
}

Notice the typedefs to get around the problem of nesting the templates...  Same concept though.

Boots
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Some corrections:
>>#include <vector.h>
>>//#include <vector> on some systems

On ALL standard C++ compilers, it should be <vector>, and not <vector.h>.
You should never use <vector.h> in your code.


The following code is also incorrect:
vector<vector<vector<char>>> cubearr;

Instead, use the following syntax:
vector<vector<vector<char> > > cubearr;

It might be hard to notice with the font type on this web site, but there is a space between >
In other words:
vector<vector<vector<char>[space]>[space]> cubearr;

This is needed with template types within template types.

morees,
This is also probably the cause of your compile error.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 30

Expert Comment

by:Axter
Comment Utility
You also need to use the resize function for each array item.
Example code:
//Needed for VC++
#pragma warning(disable: 4786)

#include <vector>


typedef std::vector<std::vector<std::vector<char> > > My3DimArray;


int main(int argc, char* argv[])
{
     char charset[] =  "groen";
     int str_length = strlen(charset);
     My3DimArray cubearr;
     int i=0;
     int x=0;
     int y=0;
     int z=0;
     
     for ( x = 0; x < str_length; x++)
     {
          if (!x) cubearr.resize(str_length);
          for ( y = 0; y < str_length; y++)
          {
               if (!y) cubearr[x].resize(str_length);
               for ( z = 0 ; z < str_length ; z++)
               {
                    if (!z) cubearr[x][y].resize(str_length);
                    if (i==str_length)
                         i=0;
                    cubearr[x][y][z] =  charset[i++];
               }
          }
     }

     system("pause");
     return 0;
}
0
 

Author Comment

by:morees
Comment Utility
This might help:

I'm using Borland C++ Builder 5
I want to read in a charset (my example groen) in a text box, and then from there create a global 3 dimesional array
that I can use in the program.

I only start creating the cube on a buttonclick event.

If I change the charset and re-create the cube, the cube must be re-created with the new charset of any lenght.

I' hope this helps

0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
morees,
Did you try the example I posted?

If you're not satisfied with the answer posted, you should reject it so that you have a better chance of getting a good answer from another expert.
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
Dear morees

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "boots111"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>Unless there is objection or further activity,  I will suggest to accept

IMHO, the points should be split.
The answer posted by boots111, was rejected.
0
 
LVL 6

Expert Comment

by:Mindphaser
Comment Utility
Force accepted

** Mindphaser - Community Support Moderator **

Axter,Crius there will be a separate question with points for your help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

772 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

11 Experts available now in Live!

Get 1:1 Help Now