【问题标题】:C++ Dynamic Memory Allocation. Assigning a Dynamically allocated array of char* return variable to a char variable?C++ 动态内存分配。将动态分配的 char* 返回变量数组分配给 char 变量?
【发布时间】:2016-02-26 18:28:57
【问题描述】:

所以我试图添加一个动态分配的char*,它是从名为

的过程返回的
GetFileInputFromUser();

我为过程中的返回变量动态分配内存。

char* pstrReturnValue;

// Prime the loop
cout << "please enter a file location on your local machine (type 1 for default location) ";
cin >> strFileInputFromUser;

// Default location?
if (*(strFileInputFromUser + intIndex) == '1')
{
    // yes, Get the length
    intLength = strlen(strDefaultFileLocation);

    // Allocate memory
    pstrReturnValue = new char[intLength];

    // copy default location in
    for (intIndex = 0; intIndex <= intLength; intIndex += 1)
    {
        *(pstrReturnValue + intIndex) = *(strDefaultFileLocation + intIndex);
    }

return pstrReturnValue;

我需要将返回变量添加到另一个字符数组(动态分配)。

void main()
{
bool blnResult = false;
ifstream ifsDataFile;
udtNodeType* pudtHeadNode = 0;
udtNodeType* pudtCurrentNode = 0;
udtNodeType* pudtNextNode = 0;
char* strFile;

udtNodeType udtReturnNode;

// Get the file path
strFile = GetFileInputFromUser();

blnResult = OpenFile(strFile, ifsDataFile);

// was it successful?
if (blnResult == true)
{
    // Yes
    // populate the list and, return the top node
    udtReturnNode = *PopulateList(pudtNextNode, pudtCurrentNode, pudtHeadNode, &ifsDataFile);

    // All done, close the file
    ifsDataFile.close();

    delete[] strFile;

    // Delete the list
    DeleteList(pudtNextNode, pudtCurrentNode, pudtHeadNode);

    // Delete proof
    PrintList(pudtNextNode, pudtCurrentNode, pudtHeadNode);
}

system("pause");
}

这是一个练习,根据要求,GetFileInputFromUser() 必须是 char*,它必须返回该指针,并且我必须在处理 LinkedList 之前删除该指针。我的事情是这样,我知道如何动态分配内存,我知道如何删除所说的内存,我的问题是我不能只为静态分配的char[]分配一个值,我不能循环返回值来分配它导致我必须能够将返回值转化为某些东西。

我尝试将char* GetFileInputFromUser() 更改为char&amp; GetFileInputFromUser(),希望我可以在内存中的位置获取地址,看看我是否可以以某种方式取消引用返回值。

如果你注释掉delete[] strFile 并运行它,它运行良好,但我正在泄漏内存,这在我看来不好。我知道有办法做到这一点,但谷歌没有帮助,我从昨晚凌晨 1 点开始就一直坚持这一点。

感谢大家的帮助。

【问题讨论】:

  • 如果你想使用字符串,然后使用std::string,那么你不会遇到内存分配问题,也不会遇到越界写入问题,你现在正在这样做。但是,您 确实 终止了字符串,但是(这将导致越界写入)但我猜这更幸运。
  • 关于泄漏,如果OpenFile 失败,那么无论如何你都会泄漏内存。
  • 我不会忘记终止字符串,因为当我将 UserInput 添加到新的动态分配的 char[] 时,我也复制了终止符,我只是在发布之前通过调试器进行检查回复。我不能使用我喜欢的 std::String,但这不符合要求。

标签: c++ arrays memory-management char heap-corruption


【解决方案1】:

如果我理解正确,您想将一个字符串添加到另一个字符串。正确的?如果是这样,您可以使用 strcat()

【讨论】:

  • 是否可以将返回的 char 数组添加到另一个 char 数组?
  • char 数组是指向 char 的指针:char*。所以它应该工作。 strcat() 有两个参数:strcat(char *dest, const char *src);src 将附加到 dest。所以 dest 必须有足够的大小来容纳 src
  • char* strFile[50]; strcat(strFile, GetFileInputFromUser());我输入了它,不起作用引发异常。 *EDIT 如果我声明 char* strFile;然后输入 strFile = new char[50];然后 strcat 工作,但没有预期的结果,但如果我使用 strcpy();我得到了我一直在寻找的结果。
  • 如果你从char* strFile[50];中删除*,它会起作用
【解决方案2】:

感谢 Askarali,您让我走上了正确的道路,下面的代码是在不出现损坏堆异常的情况下获得所需结果所需要的。谢谢

void main()
{
    char* strFile;

    strFile = new char[50];

    // Get the file path
    strcpy(strFile, GetFileInputFromUser());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 2017-08-27
    相关资源
    最近更新 更多