C腐败中的迷宫游戏。可能的内存泄漏。需要帮忙 :)

时间:2012-06-07 13:02:58

标签: c pointers malloc memory-leaks realloc

我已经开发了这个程序,这是我必须做的单一组合的迷宫。 我遇到了一些指向'path'结构的指针的问题。 我希望能够使用add_path动态更改数组的大小。 但是,运行代码r->paths = malloc(sizeof(path));时通常无法成功分配内存。以及我也称之为的realloc

我是动态分配内存的新手,并且不确定程序是否有内存泄漏。 我也不肯定它应该是malloc(sizeof(path));malloc(sizeof(path *));

void add_path(room *r, path newPath, int index)
{
    //delclarations
    int pathByteSize = sizeof(path);
    int fullSize = sizeof(r->paths);
    //check size
    if(r->paths == NULL)
    {
        printf("testa");
        //if its the first instant, 'malloc' new space
        r->paths = malloc(sizeof(path));
        if(r->paths == NULL)
        {   
            printf("Failed to save");
            return;
        }
        r->paths[0] = newPath;
        printf("testb");
    }
    else
    {
        //new size
        int newSize;
        //if its the second and onwards instant, 'realloc' new space
        printf("test1");
        path *tempPathArray = (path *)realloc(r->paths, sizeof(path) * index);
        printf("test2");
        if(tempPathArray == NULL)
        {
            printf("\nFailed Assigning Memory");
            return;
        }
        else
        {
            r->paths = tempPathArray;
        }

        r->paths[index] = newPath;
        free(tempPathArray);
    }
}

这是整个源代码

    #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <stdbool.h>

//TYPE DEFINITIONS
typedef enum
{
    NORTH,
    EAST,
    SOUTH,
    WEST,
    UNSET
} direction;

typedef char varTitle[50];

typedef struct 
{
    int indexSource;
    int indexDest;
    direction direct;
}path;

typedef struct
{
    int index;
    char desc[50];
    char title[50];
    path *paths;
}room;

typedef struct
{
    char direction[7];
}directionStruct;

//PROCEDURE/FUNCTION DEFINITIONS
directionStruct dir_to_str(direction direct);
void display_room(room *r, room toShow);
room create_room(int tempIndex, char tempTitle[50], char tempDesc[50]);
void create_rooms_and_paths(room *r);
int get_movement(room *r, room currentRoom);
void add_path(room *r, path newPath, int index);
//THE REST 

//Read String Function. Used to read in name
char read_char(char prompt[100])
{ 
    printf(prompt);
    char a = getchar();
    return a;
}

int main()
{
    //Delcarations
    room r[8];
    room currentRoom;
    room endOfMaze;
    //add rooms and paths
    create_rooms_and_paths(&r[0]);
    //variables
    currentRoom = r[0];
    endOfMaze = r[7];   

    do
    {
        display_room(r,currentRoom);
        int newRoom = get_movement(r,r[0]);
        currentRoom = r[newRoom];
    } while(currentRoom.index != endOfMaze.index);


    printf("\n-YOU FOUND THE EXIT!");
    printf("\n-Well done you have completed the game");
    printf("\n\n-Credits : ");
    printf("\n  Code : Gareth Somers");

    //set rooms


    //do
    //{
        //display first rooms
    //  display_room(currentRoom);

    //  int index = get_movement(currentRoom);
        //currentRoom = get_room(index,r);
    //}
    //while (currentRoom.index != endOfMaze.index);

    //you win



}

directionStruct dir_to_str(direction direct)
{
    directionStruct temp;

    if(direct == NORTH)
    {
        strcpy(temp.direction,"North"); 
        return temp;
    }
    else if (direct == EAST)
    {
        strcpy(temp.direction,"East");  
        return temp;
    }
    else if (direct == SOUTH)
    {
        strcpy(temp.direction,"South");     
        return temp;
    }
    else if(direct == WEST)
    {
        strcpy(temp.direction,"West");      
        return temp;
    }

}
room get_room(room *r, int toFind)
{
    //get_room searches for a room and returns it
    int i = 0;
    for(i = 0; i < sizeof(r); i++)
    {
        if(r[i].index == toFind)
        {
            return r[i];
        } 
    }
    return;
}

void display_room(room *r, room toShow)
{   
    //display_room disaplys a room and its paths
    //declarations
    int i;
    //show title
    printf("\n\n-%s",toShow.desc);
    printf("\n-You may go down the following directions : ");

    for (i = 0; i < 2; i++)
    {
        //this loops through each room and displays its details
        char tempString[100] = "  ";
        sprintf( tempString, "  %d. ", (i+1) );
        directionStruct tempDirectionStruct = dir_to_str(toShow.paths[i].direct);
        strcat(tempString, tempDirectionStruct.direction);
        strcat(tempString," to ");
        room tempRoom = get_room(r,toShow.paths[i].indexDest);
        strcat(tempString,tempRoom.title);
        printf("\n%s",tempString);
    }
}

room create_room(int tempIndex, char tempTitle[50], char tempDesc[50])
{
    //printf("\n Creating %s - %s",tempTitle, tempDesc);
    room temp;
    temp.index = tempIndex;
    strcpy(temp.title, tempTitle);
    strcpy(temp.desc, tempDesc);
    temp.paths = NULL;
    return temp;
}

path create_path(int source, int dest, direction dir)
{
    //create tempoary path
    path temp;
    //assign values
    temp.indexSource = source;
    temp.indexDest = dest;
    temp.direct = dir;
    //return path
    return temp;
}



void add_path(room *r, path newPath, int index)
{
    //delclarations
    int pathByteSize = sizeof(path);
    int fullSize = sizeof(r->paths);
    //check size
    if(r->paths == NULL)
    {
        printf("testa");
        //if its the first instant, 'malloc' new space
        r->paths = malloc(sizeof(path));
        if(r->paths == NULL)
        {   
            printf("Failed to save");
            return;
        }
        r->paths[0] = newPath;
        printf("testb");
    }
    else
    {
        //new size
        int newSize;
        //if its the second and onwards instant, 'realloc' new space
        printf("test1");
        path *tempPathArray = (path *)realloc(r->paths, sizeof(path) * index);
        printf("test2");
        if(tempPathArray == NULL)
        {
            printf("\nFailed Assigning Memory");
            return;
        }
        else
        {
            r->paths = tempPathArray;
        }

        r->paths[index] = newPath;
        free(tempPathArray);
    }
    //if empty create first spaze
    //if not empty realloc space
    //check if worked
    //copy over contents if did
}

void create_rooms_and_paths(room *r)
{
    //Create rooms (Title and Description)
    *r = create_room(0,"Room 1", "Welcome to Room 1");
    add_path(r,create_path(0,1,EAST),0);  add_path(r,create_path(0,2,SOUTH),1);
    r++;
    *r = create_room(1,"Room 2", "Welcome to Room 2");
    add_path(r,create_path(1,0,WEST),0);  add_path(r,create_path(1,4,SOUTH),1);
    r++;
    *r = create_room(2,"Room 3", "Welcome to Room 3");
    add_path(r,create_path(2,0,NORTH),0); add_path(r,create_path(2,3,EAST),1);  add_path(r,create_path(2,6,SOUTH),2);
    r++;
    *r = create_room(3,"Room 4", "Welcome to Room 4");
    //add_path(r,create_path(3,2,WEST),0); add_path(r,create_path(3,4,EAST),1);
    r++;
    *r = create_room(4,"Room 5", "Welcome to Room 5");
    //add_path(r,create_path(4,1,NORTH),0); add_path(r,create_path(4,3,WEST),1); add_path(r,create_path(4,5,EAST),2);
    r++;
    *r = create_room(5,"Room 6", "Welcome to Room 6");
    //add_path(r,create_path(5,4,WEST),0); add_path(r,create_path(5,7,SOUTH),1);
    r++;
    *r = create_room(6,"Room 7", "Welcome the end of the maze");
    //add_path(r,create_path(6,4,NORTH),0);
}

bool check_movement(room currentRoom, direction direct)
{
    //check_movement checks the current room and the directions avaliable to determine if the player can go that direction
    int i;
    for(i= 0; i < sizeof(currentRoom.paths); i++)
    {
        if(currentRoom.paths[i].direct == direct)
        {
            return true;
        }
    }
    return false;
}

int get_path_dest_index(room currentRoom, direction direct)
{
    //check_movement checks the current room and the directions avaliable to determine if the player can go that direction
    int i;
    int dest;
    for(i = 0; i < sizeof(currentRoom.paths); i++)
    {
        printf("\n tester : %i to %i",currentRoom.paths[i].indexSource,currentRoom.paths[i].indexDest);
        if(currentRoom.paths[i].direct == direct)
        {
            dest = currentRoom.paths[i].indexDest;
            break;
        }
    }
    return dest;
}

int get_movement(room *r, room currentRoom)
{
    //declarations
    char input;
    direction direct = UNSET;
    int i;
    bool foundDirection = false;

    //loop until direction found (correctly inputed)
    while(check_movement(currentRoom, direct) == false)
    {
        input = NULL;
        fflush(stdin);
        direct = UNSET;
        char prompt[100] = "\n-Please Enter the direction you wish to take (n,e,w,s) : ";
        input = read_char(prompt);

        //determites direction
        if(input == 'n')
        {
            direct = NORTH;
        }
        else if(input == 'e')
        {
            direct = EAST;
        }
        else if(input == 's')
        {
            direct = SOUTH;
        }
        else if(input == 'w') 
        {
            direct = WEST;
        }
    }

    return get_path_dest_index(currentRoom,direct);
}

1 个答案:

答案 0 :(得分:1)

重新分配数组时,立即释放它:

path *tempPathArray = (path *)realloc(r->paths, sizeof(path) * index);
/* Other things, including assinging to another pointer */
free(tempPathArray);

当您稍后尝试访问r->paths时,您可以访问已免费的内存。

相关问题