Problems Expanding an Array in C++

前端 未结 3 540
耶瑟儿~
耶瑟儿~ 2021-01-22 01:14

I\'m writing a simulation for class, and part of it involves the reproduction of organisms. My organisms are kept in an array, and I need to increase the size of the array when

相关标签:
3条回答
  • 2021-01-22 01:56

    Looks like you are modifying the pointer oldarray to point to the new array, but remember in the function that's just a copy and won't affect the variable you passed in. You probably need to pass a reference to a pointer if you want to do it this way.

    And indeed, std::vector does this for you anyway

    0 讨论(0)
  • 2021-01-22 01:59

    The C++ standard library already has functionality written to do this.

    Use the std::vector container.

    0 讨论(0)
  • 2021-01-22 02:04

    You need to pass oldarray as a reference: orgType *& oldarray. The way it's currently written, the function will delete the caller's array but will not give it the newly allocated one, causing the crash.

    Better yet, use std::vector instead of reimplementing it.

    0 讨论(0)
提交回复
热议问题