字符串正确的内存分配 [英] proper memory allocation for strings

查看:82
本文介绍了字符串正确的内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个问题我一直在试图解决现在约8小时......我已经放弃了我的帮助,没有答案的搜索。我已经尝试过使用的realloc()的malloc()分别,所以任何输入将是巨大的!

So I've had this problem I've been trying to solve for about 8 hours now... I've given up my search for an answer without help. I've tried using realloc() and malloc() respectively, so any input would be great!

在C中,它存在的目的是允许创建的地图,稍后我会使用ncurses的创建地图。

The purpose of this being in C is to allow for the creation of a 'map', I will later be using ncurses to create the map.

从文件输入如下:

10X16 de4 dw9 ds8 g8,7 m3,4 h6,5 p2,2 
6X20 dn5 ds4 W4,3 e2,12 M1,1
10X13 ds3 dw9
10X12
5X4
6x12

下面是code:

char *importLevel()
{
    FILE *fPointer; 
    fPointer = fopen("Level", "r"); //Opens text file to read
    char* rooms[150];// set up for memory allocation
    char commands[150];// set up for pulling data from read file

    while (!feof(fPointer))
    {
        fgets(commands,150, fPointer); // this takes each line from the file
    }

    *rooms = (char *) malloc(150 * sizeof(char)); //  memory allocation
    for (int i = 0; i < 150; i++)
    {
        if (rooms[i] != NULL)
        {
            *rooms[i] = commands[i]; // supposed to give rooms the string
        }
    }

    fclose(fPointer);// close file

    return *rooms; // return pointer
 }

我希望我并不愚蠢,因为我觉得现在!感谢:)

I hope I'm not as stupid as I feel right now! Thanks :)

编辑:我愚蠢,因为我觉得合适的话

edit: I AM as stupid as I felt right then

推荐答案

其他人指出一些你需要做出的改变。所以,看看他们在说什么,对如下的对比您的版本。这将有助于。

Others have pointed out some of the changes you'd need to make. So, take a look at what they've said and compare your version against the one below. This should help.

这是你的程序的修正版本。我有一些你的意图,根据您的code和数据的猜测[请原谅无偿风格清理]:

Here's a corrected version of your program. I had to guess at some of your intent, based upon your code and data [please pardon the gratuitous style cleanup]:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <malloc.h>

char **
importLevel()
{
    FILE *fPointer;
    char commands[150];                 // file line buffer
    int roomcnt = 0;
    char *cp;
    char *bp;
    char **rooms = NULL;                // set up for memory allocation

    fPointer = fopen("Level", "r");     // Opens text file to read
    if (fPointer == NULL) {
        printf("importLevel: unable to open file -- %s\n",strerror(errno));
        exit(1);
    }

    while (1) {
        // this takes each line from the file
        cp = fgets(commands, sizeof(commands), fPointer);
        if (cp == NULL)
            break;

        // setup buffer for strtok
        bp = commands;

        // parse all words on line
        while (1) {
            // NOTE: the first assumes you want "g8,7" within a room
            // the second assumes you want "g8,7" as two separate rooms
#if 1
            cp = strtok(bp," \n");
#else
            cp = strtok(bp," ,\n");
#endif

            // strtok wants this on 2nd and subsequent loops for this line
            bp = NULL;

            // bug out if no more words on this line
            if (cp == NULL)
                break;

            // increase room list size (allow space for terminator)
            // NOTE: rooms will be preserved when we return from this function
            rooms = realloc(rooms,sizeof(char *) * (roomcnt + 2));

            // NOTE: cp is pointing to something in our stack frame that
            // will go away when we return or when we read the next line
            // so we must preserve it in the heap now
            cp = strdup(cp);

            // add the contents of the room
            rooms[roomcnt] = cp;

            // advance count of number of rooms
            ++roomcnt;
        }
    }

    // add terminator to list
    if (rooms != NULL)
        rooms[roomcnt] = NULL;

    fclose(fPointer);                   // close file

    return rooms;                       // return pointer
}


P.S。不要心疼。不管如何经历了一个程序员,我们的所有的使哑巴的错误。而且,我们让他们的每个的一天。欢迎来到俱乐部!


P.S. Don't feel bad. No matter how experienced a programmer is, we all make "dumb" mistakes. And, we make them every day. Welcome to the club!

这篇关于字符串正确的内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆