0

I have a structure which includes a string field. I create an array of those structures and then I want to pass them to a function (by reference). Everything works perfectly fine when I comment out the string field, but if I don't the program crashes. I can't find an answer to this anywhere..

Here's the code (I reduced it to only show the issue):

struct student {
    int a;
    int b;
    string name[20];
    char status;
};

void operation(student the_arr[1],int number_of_students) {
    delete[] the_arr;
    the_arr = new student[3];
    for(int i = 0; i<3; i++) {
        the_arr[i].a = i+5;
        the_arr[i].b = i+4;
    }   
}

int main() {    
    student *abc;
    abc = new student[0];
    operation(abc, 0);  
    system("pause");
    return 0;
}

I need the array to be dynamic so I can change its' size when I need to.

14
  • 6
    You should use a vector. Commented Mar 22, 2013 at 16:30
  • 1
    new student[0], void operation(student the_arr[1],... : WAT? Commented Mar 22, 2013 at 16:31
  • 1
    Also, why does your student have 20 names? Commented Mar 22, 2013 at 16:32
  • 2
    Of course you can do it without vectors, but why? Commented Mar 22, 2013 at 16:32
  • 1
    " Compiling with GCC warns that abc is used uninitialized until you change new student[0] to new student[naturalNumber]" thanks, silly me but when i commented out the string it worked even with 'new student[0]".. Commented Mar 22, 2013 at 16:34

2 Answers 2

1

Assuming you can't use std::vector instead of dynamically allocated arrays follow the answer below. In any other case you should use the containers provided by the standard library.

Note: Your program doesn't crash. The only things the compiler will complain about it the allocating zero elements part, but will let you compile and run this program.

Your function is completely wrong. When using dynamic allocation you can simply pass a pointer like this:

void operation(student* the_arr, int number_of_students) {

Then inside your function you are dynamically allocating memory which is stored inside the the_arr pointer which is not passed by reference therefore leading to the creation of a local pointer variable that will lose the pointer after its execution:

void operation(student*& the_arr [...]

I suggest you to avoid the below solution though and return the new pointer instead:

student* operation(student* the_arr, int number_of_students) {
    delete[] the_arr;
    the_arr = new student[3];
    [...] 
    return the_arr; // <----
}

Allocating abc = new student[0]; doesn't make any sense. You are trying to allocate an array of 0 elements. Maybe you meant abc = new student[1];?

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

11 Comments

While problems, they shouldn't make a difference with the posted code as is. I get the feeling the posted code as is doesn't crash, though, despite the memory leak.
@Jueecy: Are you sure that's invalid?
@OliCharlesworth, no, not sure anymore.
I just want to remind that, everything is alright without string in structure
@DamianWoroszył, Most likely a red herring because of some other undefined behaviour.
|
0

You should just use the vector or other sequence objects. Though I'm not sure what you are trying to do with your code. Here's a quick example:

// Vector represent a sequence which can change in size
vector<Student*> students;

// Create your student, I just filled in a bunch of crap for the
// sake of creating an example
Student * newStudent = new Student;
newStudent->a = 1;
newStudent->b = 2;
newStudent->name = "Guy McWhoever";
newStudent->status = 'A';

// and I pushed the student onto the vector
students.push_back( newStudent );
students.push_back( newStudent );
students.push_back( newStudent );
students.push_back( newStudent );

2 Comments

He said he can't use vectors. Probably because this is homework.
Anyway, use a vector of Student, not a vector of Student *.

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.