I write a piece of code in c++ where I need to create Arrays.
Let us assume the length of array depends on some external factors(eg: numberOfStudents) which can vary .
my code follows
typedef struct Student{
//some attributes
}Student;
if(numberOfStudents < 50)
{
Student students[50];
//some more code manipulating the array
}
else if(numberOfStudents > 50 && numberOfStudents < 200)
{
Student students[200];
//some more code manipulating the array
}
else
{
Student students[1000];//assuming there cannot be more than 1000 students
//some more code manipulating the array
}
clearly I created array at three places depending on some criteria.
I want to know how much memory will be allocated. if numberOfStudents
is very small, say 10, then will the other arrays in else if
and else blocks also consume memory?
in such a case will 50 blocks of memory be used for smallest array or will it result in 1000+200+50=1250 block of memory usage.
My compiler is NOT c99 compliant.So, whenever i try creating dynamic length arrays,which I always preferred, I get compilation errors.
Also, I cannot use Vectors. For any suggestion, that why don't you use vector, I will say thanks. But unfortunately I cannot use it as MISRA C does not allow the same.
To be noted, I am relatively new to C++. Was a Java developer!