Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Initializing a Global Array

Tags:

c++

arrays

Hey everyone. I am an experienced java programmer and am just learning C++.

Now I have a bit of a beginner's problem. I have an array variable x of type int.

The user will input the size of x in method B. I want to use x in method A.

void method A()
{
 using int x [] blah blah blah
}

void method B()
{
int n;
cin >>n;
int x [n]; // How can I use this int x in method A without getting error: storage size x is unknown.
// Or the error 'x' was not declared in this scope.
}

EDIT: Parameter passing isn't a solution I am looking for.

DOUBLE EDIT: I do know about the vector option, but my program is cramming on time. I am creating an algorithm where every millisecond counts.

BTW I found out a way of doing it.

int x [] = {}

method B();
method A () { blah blah use x}
method B () {/*int*/ x [n]}
like image 624
Jimmy Huch Avatar asked May 06 '11 17:05

Jimmy Huch


5 Answers

If you actually want an array and not a vector, and you want that array dynamically sized at runtime, you would need to create it on the heap (storing it in a pointer), and free it when you're done.

Coming from Java you need to understand that there's no garbage collection in C++ - anything you new (create on the heap) in an object you will want to clean up in the destructor with delete.

class foo
{
    private:
    int *array;

    public:
    foo() { array = NULL; };
    ~foo()
    {
        if (array != NULL)
            delete [] array;
    }

    void createArray()
    {
        array = new int[5];
    }

};

More info at: http://www.cplusplus.com/doc/tutorial/dynamic/

like image 98
Brian Roach Avatar answered Oct 27 '22 02:10

Brian Roach


This is a version of your example that works in c++.

#include <iostream>

int *my_array;

void methodA(a,b){
     my_array[a] = b;
}

int methodB(){
     int n;
     std::cin >> n;
     my_array = new int[n];
}

int main(){
     int x;
     x = methodB();
     methodA(x-1, 20);
     delete [] my_array;
     return 0;
}
like image 22
Null Set Avatar answered Oct 27 '22 03:10

Null Set


Use a vector:

std::vector<int> x(n);

then pass that to method A as an argument of type std::vector<int> const &.

Edit: Or make the vector a data member of your class and set it with:

size_t n;
std::cin >> n;
x.resize(n);
like image 28
Fred Foo Avatar answered Oct 27 '22 02:10

Fred Foo


In C++ you can't directly size an array with a runtime value, only with constants.

You almost certainly want vector instead:

std::vector<int> x(n);

like image 22
Mark B Avatar answered Oct 27 '22 04:10

Mark B


EDIT: flesh out answer.

I can't quite tell if you are trying to learn about arrays, or if you are trying to solve some practical problem. I'll assume the latter.

The only way for method A to have access to any variable is if it is in scope. Specifically, x must either be:

  • a local, including a parameter (but you said no to parameter passing)
  • a class member, or
  • a global

Here is a solution in which x is a class member:

class C {
public:
  std::vector<int> x;
  void A() {
    std::cout << x[2] << "\n"; // using x[], for example.
  }
  void B() {
    int n;
    cin >> n;
    x = std::vector<int>(n); // or, as others have pointed out, x.resize(n)
  }
};
like image 20
Robᵩ Avatar answered Oct 27 '22 02:10

Robᵩ