Creating List Like Array with Dynamic Memory Management

There are a lecture class and student class. We are trying to save student information on lecture class with arrays.

For example:

Student * studentList = new Student[numberOfStudent];
studentAdd("Mary");
studentDelete("Mary");

Problem: User is not giving number of student and number of student is increasing when user add a new one with a one of lecture method. So I think that i need a list like structer to save them but it is forbidden for this work. Do you have any efficient ideas than my temporary solution?

My temporary solution: Saving number of student and array size and when number of student is more than size copy array to a new one which is bigger than old one.

This question is related to my assigment which we are forcing to this by

*using dynamically allocated memory using pointers

*without using any static arrays with fixed sizes or other data structures such as vector from the standard library

Answers


you could just use functions if you don't want to use objects. Note that I haven't compiled this code.

#include <iostream>
#include <sstream>
#include <string>
#include "Student.h"

void add(Student **list, Student *rhs);
void destroy(Student **list,int const &idx);
void destroy(Student **list,const char* student_name); //find the student name and delete
void resize(Student **list,int const &idx);
Student** begin(Student **list,); //get the first Student
Student** end(Student **list,); //the end of the array
void clear(Student **list,); //drop all Students

int main(int argc, char **argv)
{
  if(argc < 2)
  {
    stc::cerr << "not enough parameters" << std::endl;
  } 
  istringstream buffer(argv[1]);
  int value;
  buffer >> value; 
  Student ** studentList = new *Student[value];
  int s
  add(studentList, "Mary");
  destroy(studentList, "Mary");
  return(0);
}

void add(Student **list, Student *rhs) { /*definition*/ }
void destroy(Student **list, int const &idx) { /*definition*/ }
void destroy(Student **list, const char* student_name) { /*definition*/ }
void resize(Student **list, int const &idx) { /*definition*/ }
Student** begin(Student **list) { /*definition*/ }
Student** end(Student **list) { /*definition*/ }
void clear(Student **list) { /*definition*/ }

Now just define all that and you are all set

I do not recommend this approach

I would use objects... and taking into account what PaulMcKenzie said about capacity

class StudentList
{
private:
  Student **list;
  std::size_t size;
  std::size_t capacity;
public:
  StudentList(){}
  ~StudentList(){ this->clear(); }
  inline void add(Student *rhs) { //definition }
  inline void destroy(int const &idx) { //definition }
  inline void destroy(const char* student_name) { //definition }
  inline void resize(int const &idx) { //definition }
  inline Student** begin() { //definition }
  inline Student** end() { //definition }
  inline void clear() { //definition }
};

And if possible

Iterators

Underneath vector you will find a similar implementation, it uses Iterators to encapsulate the Student**.


Need Your Help

Setting an background image over an image set for a UIImageView - Objective C

ios objective-c uiimageview uiimage

I am setting images to a UIImage programmatically. But I want to also add a play button over that view. Think of Facebook in how there is a play button over the image. That is exactly what I am ...