C++ 动态内存分配。将动态分配的 char* return 变量数组分配给 char 变量?
C++ Dynamic Memory Allocation. Assigning a Dynamically allocated array of char* return variable to a char variable?
因此,我正在尝试添加一个动态分配的 char*
,它是从名为
的过程中 return 编辑的
GetFileInputFromUser();
我在过程中为 return 变量动态分配内存。
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;
我需要的是将 return 变量添加到另一个字符数组(动态分配)。
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*
它必须 return 该指针,我必须在处理 LinkedList 之前删除该指针.我的事情是这样的,我知道如何动态分配内存,我知道如何删除所述内存,我的问题是我不能只给静态分配的 char[]
赋值,我不能循环遍历 return 值来分配它,因为我必须能够将 return 值变成某种东西。
我已经尝试将 char* GetFileInputFromUser()
更改为 char& GetFileInputFromUser()
希望我可以在内存中的位置获取地址,看看我是否可以希望通过取消引用 return 不知何故价值。
如果你注释掉 delete[] strFile
和 运行 它,它 运行 没问题,但我正在泄漏内存,这在我看来是不好的。我知道有办法做到这一点,但 google 没有帮助,我从昨晚凌晨 1 点开始就一直坚持这个。
谢谢大家的帮助。
如果我没理解错的话,你想把一个字符串加到另一个字符串上。正确的?如果是这样,您可以只使用 strcat().
谢谢 Askarali,你让我走上了正确的道路,下面的代码是在没有损坏堆异常的情况下获得预期结果所需要的。谢谢
void main()
{
char* strFile;
strFile = new char[50];
// Get the file path
strcpy(strFile, GetFileInputFromUser());
}
因此,我正在尝试添加一个动态分配的 char*
,它是从名为
GetFileInputFromUser();
我在过程中为 return 变量动态分配内存。
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;
我需要的是将 return 变量添加到另一个字符数组(动态分配)。
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*
它必须 return 该指针,我必须在处理 LinkedList 之前删除该指针.我的事情是这样的,我知道如何动态分配内存,我知道如何删除所述内存,我的问题是我不能只给静态分配的 char[]
赋值,我不能循环遍历 return 值来分配它,因为我必须能够将 return 值变成某种东西。
我已经尝试将 char* GetFileInputFromUser()
更改为 char& GetFileInputFromUser()
希望我可以在内存中的位置获取地址,看看我是否可以希望通过取消引用 return 不知何故价值。
如果你注释掉 delete[] strFile
和 运行 它,它 运行 没问题,但我正在泄漏内存,这在我看来是不好的。我知道有办法做到这一点,但 google 没有帮助,我从昨晚凌晨 1 点开始就一直坚持这个。
谢谢大家的帮助。
如果我没理解错的话,你想把一个字符串加到另一个字符串上。正确的?如果是这样,您可以只使用 strcat().
谢谢 Askarali,你让我走上了正确的道路,下面的代码是在没有损坏堆异常的情况下获得预期结果所需要的。谢谢
void main()
{
char* strFile;
strFile = new char[50];
// Get the file path
strcpy(strFile, GetFileInputFromUser());
}