c++ - 为什么我不能将新对象添加到我的对象指针数组中?

标签 c++ arrays pointers

我有一个带有 Movie 对象数组的 MovieController 对象。我有一个在 Controller 上调用“doAddMovie”的 MovieView 对象。

当我在 MovieController 的构造函数中调用“doAddMovie”时,一部电影被添加到数组中。但是,当我从 MovieView 调用“doAddMovie”时,出现了段错误。

谁能告诉我我做错了什么?

电影 Controller .h

#ifndef MOVIECONTROLLER_H
#define MOVIECONTROLLER_H

#include "MovieView.h"
#include "Movie.h"

class MovieView;

class MovieController
{
  public:
    MovieController();
    void doAddMovie(string title, int year, string genre);

  private:
    MovieView* movieView;
    Movie** movies;
};

电影 Controller .cc:

const int MAX_MOVIES = 10;

MovieController::MovieController() {
  movies = new Movie*[MAX_MOVIES];
  doAddMovie("Star Wars", 1977, "SciFi"); // <<-- this works
}

/* when I call this method from my MovieView object, I get a segmentation fault, pretty
 * sure the fault happened on line:  movies[i] = new Movie(title, year, genre);
 */
void MovieController::doAddMovie(string title, int year, string genre) {
  cout << "doAddMovie; title=" << title << "; year=" << year << "; genre =" << genre;
  int i = 0;
  while (i < MAX_MOVIES) {
    if (movies[i] == NULL) {
      movies[i] = new Movie(title, year, genre); <<-- segmentation fault here
      break;
    }
    i = i+1;
  }
  return;
}

最佳答案

doAddMovie() 期望在可用槽上找到一个 NULL 指针,但是在分配数组后您没有将数组槽初始化为 NULL,因此数组的内容将是随机垃圾。 doAddMovies() 在构造函数中调用时起作用的事实是侥幸。

试试这个:

class MovieController
{
public:
    MovieController();
    ~MovieController();

    void doAddMovie(string title, int year, string genre);

private:
    MovieView* movieView;
    Movie** movies;
};

MovieController.cc:
const int MAX_MOVIES = 10;

MovieController::MovieController()
{
    movies = new Movie*[MAX_MOVIES];
    memset(movies, 0, sizeof(Movie*)*MAX_MOVIES); // <-- add this
    doAddMovie("Star Wars", 1977, "SciFi");
}

MovieController::~MovieController()
{
    for (int i = 0; i < MAX_MOVIES; ++i)
        delete movies[i];
    delete[] movies;
}

void MovieController::doAddMovie(string title, int year, string genre)
{
    for (int i = 0; i < MAX_MOVIES; ++i)
    {
        if (movies[i] == NULL)
        {
            movies[i] = new Movie(title, year, genre);
            cout << "Movie added: title=" << title << "; year=" << year << "; genre =" << genre;
            return;
        }
    }

    cout << "Movie ignored: title=" << title << "; year=" << year << "; genre =" << genre;
}

话虽如此,您应该拥抱 C++ 内存管理并且根本不使用原始数组:

#include <vector>

class MovieController
{
public:
    MovieController();
    void doAddMovie(string title, int year, string genre);

private:
    MovieView* movieView;
    std::vector<Movie> movies;
};

MovieController.cc:

MovieController::MovieController()
{
    doAddMovie("Star Wars", 1977, "SciFi");
}

void MovieController::doAddMovie(string title, int year, string genre)
{
    movies.push_back(Movie(title, year, genre));
    cout << "Movie added: title=" << title << "; year=" << year << "; genre =" << genre;
}

最后,如果在那之后您仍然遇到 SEGFAULT,请确保您的 MovieView 对象正在使用有效的 MovieController 调用 doAddMovie() > 开头的对象指针。

关于c++ - 为什么我不能将新对象添加到我的对象指针数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21471810/

相关文章:

c++ - 无法在初始化列表中捕获

dllimport静态数据成员的C++定义

javascript - 从动态创建的输入字段加载数组

c++ - 使用除法技术求数组的最大公约数

c++ - 为什么 reinterpret_cast 不能将 int 转换为 int?

c - 返回 int array[] 或指向 int array[] 的指针返回到 C 中调用例程的函数

c++ - 字符串类赋值运算符 (+=) 行为异常

c++ - 为 C++ 初学者从 OpenCV 流式传输的优雅方式?

mysql - 如何构建像 FourSquare 一样的排行榜(高于和低于你的用户)

c++ - 从功能改变值(value)