2013-10-06 58 views
0

所以我試圖創建一個指向Piece類型對象的2維數組。問題是當我嘗試分配一個指向數組的指針時,我得到了分段錯誤。我意識到在我開始分配之前我需要初始化到數組,但我無法正確地進行分配。不能爲2維指針數組分配內存C++

這是包含一個二維數組指針的Map的頭文件。

#ifndef MAP_H 
#define MAP_H 

#include <iostream> 
#include <vector> 
#include <fstream> 
#include <stdio.h> 
#include <stdlib.h> 
#include <sstream> 
#include <string> 
#include <cstring> 
#include "Player.h" 
#include "Sprite.h" 
#include "Piece.h" 
#include "Messages.h" 
#include "PieceType.h" 

using namespace std; 

class Map 
{ 
    private: 

     Piece*** pieces; 
     int startingX; 
     int startingY; 
     int width; 
     int height; 
     string mapName; 

    public: 

     Map(string); 
     ~Map(); 

     void printMap() const; 
     Piece* pieceType(char); 
     void setSprite(Piece*); 
     void firstMove(); 
     void resetMap(string); 

     bool moveUp(int, int); 
     bool moveDown(int, int); 
     bool moveLeft(int, int); 
     bool moveRight(int, int); 

     int getHeight(); 
     int getWidth(); 


}; 

#endif 

我說的數組是片段。

我嘗試在Map的構造函數中分配它。

Map::Map(string name) 
{ 
    ifstream map; 
    string line; 
    string dimention; 
    mapName = name; 

    map.open(name.c_str()); 

    if (map.good()) 
    { 
    getline (map, line); 

    int i = 0; 

    while(line[i] != 'X') 
    { 
     dimention[i] = line[i]; 
     i++; 
    } 

    stringstream convert(dimention); 

    convert >> width; 

    int temp = i; 
    dimention = ""; 
    i = 1; 

    while(line[(i + temp)] != '\0') 
    { 
     dimention[i] = line[(i + temp)]; 
     i++; 
    } 

    stringstream convertTwo(dimention); 

    convertTwo >> height; 

    for (int i = 0; i < height; i++) 
    { 
     if (!(map.eof())) 
     { 
    getline (map, line); 
     } 
     else 
     { 
    cout << "Error with file" << endl; 
    break; 
     } 

     for (int j = 0; j < width; j++) 
     { 
    pieces[i][j] = pieceType(line[j]); //This is where I'm getting the segmentation fault 

    cout << "assigned" << endl; 

    if ((pieces[i][j])->getType() == WAYPOINT) 
    { 

     if (pieces[i][j]->getWaypointType() == 0) 
     { 
     startingX = j; 
     startingY = i; 
     } 
    } 

    else 
    {  
    (pieces[i][j])->setXCordinate(j); 
    (pieces[i][j])->setYCordinate(i); 
    } 

     } 
    } 
    } 
} 

其中name是一個字符串,其中保存具有用於加載特定映射的信息的文件的名稱。

另外的功能pieceType如下:

Piece* Map::pieceType(char type) 
{ 
    Piece* temp; 

    if (type == '.') 
    { 
    return NULL; 
    } 
    if (type == 'S') 
    { 
    temp = new Waypoint(0); 
    return temp; 
    } 
    if (type == 'E') 
    { 
    temp = new Waypoint(1); 
    return temp; 
    } 
} 

航點是派生類件構成。

+0

我想在編譯時數組的大小是未知的?數組大小是否有限制? – rdans

+1

在分配它之前,您正在取消「裁片」,即使它是您需要分配指針的指針。更好的是,使用std:vector來跟蹤什麼以及何時刪除指針,例如'vector > pieces;' –

+0

' pieces [i] [j] = pieceType(line [j]);'這不是內存分配,這是賦值。 – ipinak

回答

2

問題的確是你必須初始化該數組。就像這樣:

,只是以後你 widthheight,在開始使用前 pieces
pieces=new Piece**[height]; 
for(int i=0;i<height;i++){ 
    pieces[i]=new Piece*[width]; 
} 

寫。 但是你應該知道的是:對於每個new,應該有一個對應的delete,否則那個內存永遠不會被釋放,並且你會得到一個內存泄漏。要釋放內存,在析構函數的添加此:

for(int i=0;i<height;i++){ 
    for (int j = 0; j < width; j++){ 
     delete pieces[i][j]; 
    } 
    delete[] pieces[i]; 
} 
delete[] pieces; 

這是假定每個pieces[i][j]包含要麼new或NULL分配的對象,並與這兩個工作。看着你的代碼,這似乎是你的情況。但是,如果其中一個未分配(不是你的情況),它將不起作用。

0

使用std::vector<std::vector<Pieces>>而不是(嘗試,因爲它不工作)重新發明車輪。它安全,簡單,避免了手動內存管理的麻煩。