fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. float* a_predefined_2d_array = nullptr;
  5.  
  6. class Level {
  7. private:
  8. int size;
  9. float** a_predefined_2d_array;
  10. public:
  11. int GetSize();
  12. int SetSize(int);
  13. int DoSomething();
  14. int Load(string, int);
  15.  
  16. };
  17.  
  18. int Level::GetSize() {
  19. return size;
  20. }
  21.  
  22. int Level::SetSize(int _size) {
  23. size = _size;
  24. }
  25.  
  26. int Level::Load(string str, int _size) {
  27. SetSize(_size); //works fine!
  28. cout<<GetSize()<<endl; //works fine!
  29. //basically loads everything to initiliaze the program. The point is that the above calls work fine!
  30. }
  31.  
  32. //The problem is here!!!
  33. int Level::DoSomething() {
  34. a_predefined_2d_array = new float*[GetSize()]; //SEG FAULTS!!!
  35. cout<<"size = "<<GetSize()<<endl; //SEG FAULTS!!!
  36. }
  37.  
  38. int main() {
  39. Level l{};
  40.  
  41. l.DoSomething();
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
size = 0