473,324 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Making lokal defined Multidimensional Array Global

Hi

I have a problem in my C++ Funktion. I needed an 2 dimensional Array. For
this i took a template script in order to create the array in the funktion.
The Size of the Array I have to calculate first before I create it.So I have
to init it inside my Funktion. Now how do I get this Array so that it is
globally accessible.

Thanx
Patrick
Jul 22 '05 #1
2 1696

"Patrick" <th**********@spamgourmet.com> wrote in message
news:ck**********@home.itg.ti.com...
Hi

I have a problem in my C++ Funktion. I needed an 2 dimensional Array. For
this i took a template script in order to create the array in the funktion. The Size of the Array I have to calculate first before I create it.So I have to init it inside my Funktion. Now how do I get this Array so that it is
globally accessible.

Thanx
Patrick


Impossible, arrays cannot be copied in C++. Either create the array outside
the function and pass a pointer to it inside the function. Or, better still,
switch to using vectors instead of arrays, unlike arrays vectors can be
copied, so you can just write

#include <vector>
using namespace std;

vector<vector<int> > create2dVector()
{
vector<vector<int> > vec;
// init the vector here
...
return vec;
}

john
Jul 22 '05 #2

"Patrick" <th**********@spamgourmet.com> wrote in message
news:ck**********@home.itg.ti.com...
Hi

I have a problem in my C++ Funktion. I needed an 2 dimensional Array. For
this i took a template script in order to create the array in the
funktion.
The Size of the Array I have to calculate first before I create it.So I
have
to init it inside my Funktion. Now how do I get this Array so that it is
globally accessible.

Thanx
Patrick


Globals are evil. :-) I'd reconsider that idea, if possible. Do you really
need it accessible by all classes and functions? Perhaps you might consider
having a class that contains both the array object, and any other objects
and code that need access to the array.

Many new programmers start out by writing their programs as one big main()
function, which calls other functions, in a precedural fashion. Using
object-oriented approaches often leads to better code, for many reasons.
Instead, you might want to declare a local instance of this new "container"
class in your main() function, and let it do all the work you now have in
main(). (Assuming that's the way you've structured it, of course...you
haven't shown any code, so I'm guessing.)

At the very least, I'd put the array into a class. Then you can use it
however you please. You can create it in one place, and pass a pointer to
it to other functions that need to work on it. Or you can declare it
global, but wait until your program is ready to "initialize" it, calling a
member function to do the actual creating of the internally stored array.

In any case, wrapping it in a class would be my first task.

-Howard
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rob Tweed | last post by:
Probably a simple question but I can't find the answer anyway. Specifically, is it possible to copy a multidimensional array into the $_SESSION array - ie a deep clone of all keys and data? I...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
2
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array,...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
3
by: Tyrone Slothrop | last post by:
I have created a script which recurses a display of directories like so: <? $dir = "/path/to/base/directory"; function scan_dir_recurse ($dir,$tab) { global $fileArr; $tab++; if ($tab 4) {...
1
by: Chuy08 | last post by:
If I have a multidimensional array like the following: Array $records =Array 0 = 30 year, 6.0; 1 = 30 year, 6.0; 2 = Pay Option, 1.0; 3 = Pay Option, 1.0; How could I flatten this to...
14
by: Michel Rouzic | last post by:
Hi, I've recently met issues with my program which can only be explained by heap corruption, so I've tried debugging my program with Valgrind, and here's what I get with the following...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.