Hello guys,

I've a class with a cpp and .h files. It's a part of a large program. I would like to create an array of structs and use it wherever in the same class, i.e. I want to initialize it in a specific function and use it in other functions within the same class.

Example:

//aFile.h
// mySimpleClass
private:
  typedef struct myStruct {
      int age;
  } MyStruct;
  void myFct1 (void) {...};
  int myFct2 (void) {...};
//aFile.cpp
// intialize an array of MyStruct in myFct1 
// use the array in the other functions

How can I do that?

Thank you!

You just want to dynamically allocate a new array of your struct right? Like this:

myStruct* mySimpleClass::myFunc1 (int arraySize) {
   myStruct struct1 = new myStruct[arraySize];
   return struct1;
}

You just want to dynamically allocate a new array of your struct right? Like this:

myStruct* mySimpleClass::myFunc1 (int arraySize) {
   myStruct struct1 = new myStruct[arraySize];
   return struct1;
}

Yeah that's right. But there's no return in my case. So what about the following code?

void mySimpleClass::myFunc1 (void) {
  int arraySize;
  myStruct* struct1;
  // do something
  struct1 = new myStruct[arraySize];
   // do something
}

int mySimpleClass::myFunc2 (void) {
   // use the array? struct1
}

Is it correct? Because this is what I have in my case.

Thank you!

If you wanted to use your array of struct outside of that method's scope (myFunc1) you need to return to something, then pass it to another method (like myFunc2)

So according to you, there's no global/external variable like C#? I need something that can be used by all functions of the same class and only one of those functions is able to create/initialize the array of struct.

Well, looking at the code you produced. the pointer struct1 is a local variable to myFunc1( ). So by the time the function call ends. the scope of struct1 ends. creating 2 problems

1) the dynamic array created remains unused.
2) The dynamic array is not deallocated with the delete operator.. Thus creating a memory leak.

There are solutions to this problem.

1. As you have mentioned. arrange a global variable .on which manipulations can be done by the functions.

2. Make your function return a pointer .. that pointer will make the allocated array usable.

You can define a class wide variable within your class.

class myClass {
     private:
          myStruct mS*;

}

Which will allow you to access mS in both functions, so you could allocate the memory to it in one function. Use it in a second function and then deallocate it in another function, such as the destructor

Chris

You can define a class wide variable within your class.

class myClass {
     private:
          myStruct mS*;

}

Thank you all. Thank you Chris. That's what I'm looking for.

Can I allocate memory for my array simply like this:

void mySimpleClass::myFunc1 (void) {
      int arraySize;
      // do something
      mS = new myStruct[arraySize];
      // do something
      mS[0]->age = 10;
      ...
}

Cheers!

as long as arraysieze contains a value when you try to allocate memory for your array yes. Here is an exmaple,

#include <iostream>

class myLameArray {
    private:
        int *x;
        int sizee;

    public:
        ~myLameArray(){
            delete[] x;
        }

        void setUpArray(int size){
            x = new int[size];
            sizee = size;
        }

        void initArray(){
            for(int i = 0; i < sizee; i++){
                x[i] = i;
            }
        }

        void printArray(){
            for(int i = 0; i < sizee; i++){
                std::cout << x[i];
            }
        }
};
#include "myLameArray.cpp"

using namespace std;

int main(void){
    myLameArray* l = new myLameArray();
    l->setUpArray(10);
    l->initArray();
    l->printArray();

    delete l;

    return 0;
}

Chris

Thank you!

Guys, I have another question. I'm having a warning related to the fact that my variable array of structs (myStruct) is not in initialized. How can I initialize it? Is the following code the best solution?

void mySimpleClass::myFunc1 (void) {
      int arraySize;
      mS = new myStruct[1];
      // do something
      if (aCondition) {
         mS = new myStruct[arraySize];
         // do something
         mS[0]->age = 10;
      }
      ...
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.