0

i have a problem, i think it has to do with heap memory. would appreciate help.

this is in tribe header:

Survivor* SurvivorsArray = new Survivor[MaxSurvivorsInTribe]; 

it's my problem. when i write

Survivor* SurvivorsArray = new Survivor[MaxSurvivorsInTribe]; 

i get this problem:

EXE3.exe has triggered a breakpoint.

this is take my in code to:

    if (_crtheap == 0) {
#if !defined (_CRT_APP) || defined (_DEBUG)
    _FF_MSGBANNER();    /* write run-time error banner */
    _NMSG_WRITE(_RT_CRT_NOTINIT);  /* write message */
#endif  /* !defined (_CRT_APP) || defined (_DEBUG) */
    __crtExitProcess(255);  /* normally _exit(255) */
}

return HeapAlloc(_crtheap, 0, size ? size : 1);
}

but if i write:

Survivor* SurvivorsArray = new Survivor[100]; 

so it's work without error.

Currently it only works if I set up a preset array but if I want to get the array size from the user I get an error.

main:

#include <iostream>
#include <string>
using namespace std;

#include "survivor.h"
#include "tribe.h"
int main(){
Tribe t1;
Tribe t2;
char nameTribe1[20]; 
int maxTribe1;
cout << "Enter name of first tribe: " << endl;
cin >> nameTribe1;
cout << "Enter max of survivors to first tribe(max 100): " << endl;
cin >> maxTribe1;
t1.tribe(nameTribe1, maxTribe1);

char nameTribe2[20];
int maxTribe2;

cout << "Enter name of second tribe: " << endl;
cin >> nameTribe2;
cout << "Enter max of survivors to second tribe(max 100): " << endl;
cin >> maxTribe2;
t2.tribe(nameTribe2, maxTribe2);
return 0;
}

survivor headr:

class Survivor{

public: 

char NameOfSurvivor[20];
int Age;
double StartWidgth;
double FinalWidgth;

void survivor(char name[20], int age, double sWidgth);
};

#endif

survivor cpp:

#include "survivor.h"
#include <iostream>

using namespace std;
void Survivor::survivor(char name[20], int age, double sWidgth){
for (int i = 0; i < 20; i++)
    NameOfSurvivor[i] = name[i];

Age = age;
StartWidgth = sWidgth;
FinalWidgth = -1;
}//end survivor

tribe header:

class Tribe{

public: 

char NameOfTribe[20];
int MaxSurvivorsInTribe;
**Survivor* SurvivorsArray = new Survivor[MaxSurvivorsInTribe];** 
int NumbersOfSurvivorsInTribe;   
void tribe(char name[20], int maxSurvivor);

};

#endif

tribe cpp:

#include "tribe.h"
#include <string>
#include <iostream>

using namespace std;

void Tribe::tribe(char name[20], int maxSurvivor){
for (int i = 0; i < 20; i++)
    NameOfTribe[i] = name[i];


for (int i = 0; i < maxSurvivor; i++){
    for (int j = 0; j < 20; j++){
        SurvivorsArray[i].NameOfSurvivor[j] = ' ';
    }//end for
    SurvivorsArray[i].Age = 0;
    SurvivorsArray[i].StartWidgth = 0;
    SurvivorsArray[i].FinalWidgth = 0;

    MaxSurvivorsInTribe = maxSurvivor;
    NumbersOfSurvivorsInTribe = 0;
}//end for
}//end tribe

thank's.

2
  • Why are your #include statements inside of the main function? If you put them above it (and the namespace too,) does it work? Commented Apr 13, 2017 at 22:31
  • No Sorry I wrote it down badly here on the site. i fix that. Commented Apr 13, 2017 at 22:33

4 Answers 4

1

You can't perform dynamic allocations inside a class declaration. Move array allocation to to the Tribe constructor.

By the way, unless you're forbidden from using the STL for some reason, it will be much better to use a vector instead of an array.

Sign up to request clarification or add additional context in comments.

3 Comments

I did a quick test on VS2013, and it compiled cleanly when using a constant size. I did not expect that.
Looking at your code I'm seeing many other issues. Consider posting it in the code review stack exchange site.
MaxSurvivorsInTribe isn't even defined when you attempt to pass it as a parameter to new operator. It is only assigned to after you've populated the array! The compiler can't guess its value.
0

MaxSurvivorsInTribe seems to be not initialized, when you allocate memory for array. It can have any integer value by default. You can use pointer as a field and initialize it in constructor:

class Tribe{
...
int MaxSurvivorsInTribe;
Survivor* SurvivorsArray;
Tribe(char* name, int maxSurvivor){
   MaxSurvivorsInTribe = maxSurvivor;
   Survivor* SurvivorsArray = new Survivor[MaxSurvivorsInTribe];
   ....      
}

And don't forget to free memory in destructor:

~Tribe(){
   delete Survivor;
}

You need to reed more about classes in c++ and dynamic arrays.

Comments

0

Your constructors aren't formatted correctly, you put:

void tribe(char name[20], int maxSurvivor);

but it should be

Tribe(char name[20], int maxSurvivor);

Since it couldn't be initialized correctly, MaxSurvivorsInTribe wasn't set and couldn't be used.

Then in main, you'll initialize it like this:

char nameTribe1[20]; 
int maxTribe1;
cout << "Enter name of first tribe: " << endl;
cin >> nameTribe1;
cout << "Enter max of survivors to first tribe(max 100): " << endl;
cin >> maxTribe1;
Tribe t1(nameTribe1, maxTribe1);

1 Comment

I don't think he was trying to use a constructor.
0

Your code is failing because it is trying to call the new function to create the buffer with an undefined size. When you first declare your tribe variables like this:

Tribe t1;
Tribe t2;

You are triggering the implicit default constructor, which is going to attempt to create an object. The value you are using for the array size, MaxSurvivorsInTribe is a local field of the Tribe class that is not then initialized to any value.

You should probably do some code restructuring where you collect the size and name of each tribe before you instantiate your tribe objects, and then provide a constructor that accepts the max size and name values that also allocates the space needed for your array. Something like:

int main(){
char nameTribe1[20]; 
int maxTribe1;
cout << "Enter name of first tribe: " << endl;
cin >> nameTribe1;
cout << "Enter max of survivors to first tribe(max 100): " << endl;
cin >> maxTribe1;

char nameTribe2[20];
int maxTribe2;

cout << "Enter name of second tribe: " << endl;
cin >> nameTribe2;
cout << "Enter max of survivors to second tribe(max 100): " << endl;
cin >> maxTribe2;

Tribe t1(nameTribe1,maxTribe1);
Tribe t2(nameTribe2,maxTribe2);
}

And for tribe:

class Tribe{

public: 

char NameOfTribe[20];
int MaxSurvivorsInTribe = 0;
Survivor* SurvivorsArray = nullptr;
int NumbersOfSurvivorsInTribe = 0;   
Tribe(char name[20], int maxSurvivor) : MaxSurvivorsInTribe(maxSurvivor) 
{
  SurvivorArray = new Survivor[MaxSurvivorsInTribe ];
}
};

As others have noted, you would be even better served if you used a std::vector as your container and even a std::string to hold your name to avoid potential errors.

class Tribe{

public: 

std::string NameOfTribe;
int MaxSurvivorsInTribe = 0;
std::vector<Survivor> SurvivorsArray;
int NumbersOfSurvivorsInTribe = 0;   

Tribe(std::string name, int maxSurvivor) : MaxSurvivorsInTribe(maxSurvivor), NameOfTribe(name)
{
}
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.