C ++程序当方法返回值分配给int时崩溃 [英] C++ Program Crashes when method return value is assigned to an int
问题描述
这个问题现在吹起我的脑海。
This problem is blowing my mind right now.
int main()
{
char inputChar;
char *buffer = nullptr;
int size = 0;
read(buffer); //this is the line causing problems...
int numberOfFrames = (size / MAX_FRAME_SIZE) + 1;
frame array[numberOfFrames];
for(int i = 0; i < size; i++)
{
buffer[i] = appendParityBit(buffer[i]);
}
constructFrame(buffer, size, array);
transmitFrames(array, numberOfFrames);
}
int read(char *buffer)
{
int fileSize;
ifstream myfile ("inputFile");
if (myfile.is_open())
{
fileSize = getFileLength(myfile);
buffer = new char[fileSize];
myfile.read(buffer, fileSize);
myfile.close();
}
return fileSize;
}
int getFileLength(ifstream &myfile)
{
myfile.seekg(0, ios::end);
int size = (int) myfile.tellg() - 1;
myfile.seekg(0, ios::beg);
return size;
}
现在如果我做
cout << read(buffer);
在导致问题的行上,我收到一个整数回来...伟大,完美。但如果我尝试做
on the line that is causing problems, i receive an integer back...great, perfect. but if i try to do
size = read(buffer);
我的程序崩溃了...我失去了。
my program crashes...i'm at a loss.
推荐答案
你是通过值传递一个变量(如果它是一个指针或不是无关紧要)。在接收端,函数将传递的内容的本地副本,与本地副本和poof一起使用,当函数返回时,本地副本将消失。
You are passing a variable by value (doesn't matter if it is a pointer or not). On the receiving end, the function makes a local copy of what is passed, works with the local copy, and poof, the local copy goes away when the function returns.
这发生,不管你传递的是否是一个指针。例如,取这个简单的代码:
This occurs regardless of whether what you're passing is a pointer or not. For example, take this simple code:
void foo(int x)
{
x = 10;
}
int main()
{
int val = 0;
foo(val);
cout << val; // how come val is still 0 and not 10?
}
注意 val
仍为0,即使函数正在更改要传递的参数。要解决此问题,请将引用传递给将要更改的值:
Note that val
is still 0, even though the function is changing the parameter that is being passed. To fix this problem, you pass a reference to the value that will be changed:
void foo(int& x)
{
x = 10;
}
int main()
{
int val = 0;
foo(val);
cout << val; // now val is 10
}
使用指针,规则不会改变。您需要传递一个指针的引用,以使更改反映回调用者:
With pointers, the rules don't change. You need to pass a reference to the pointer to have the change reflect back to the caller:
int read(char*& buffer)
{
int fileSize;
ifstream myfile ("inputFile");
if (myfile.is_open())
{
fileSize = getFileLength(myfile);
buffer = new char[fileSize];
myfile.read(buffer, fileSize);
myfile.close();
}
return fileSize;
}
现在在该函数不是本地副本,而是对您传递的变量的引用。
Now the buffer
in that function is not a local copy, but a reference to the variable you passed.
另一种方法(更多的是C风格)是传递一个指针到你想改变的东西。您要更改指针值,因此您将指针传递给指针:
The other method (which is more "C" style) is to pass a pointer to the thing you want to change. You want to change the pointer value, so you pass a pointer to the pointer:
int read(char** buffer)
{
int fileSize;
ifstream myfile ("inputFile");
if (myfile.is_open())
{
fileSize = getFileLength(myfile);
*buffer = new char[fileSize];
myfile.read(buffer, fileSize);
myfile.close();
}
return fileSize;
}
// the caller
char *buffer;
//...
read(&buffer);
当然,我们必须更改语法,因为它是一个正在传递的指针,因此我们需要解除引用。
Of course, we have to change the syntax since it is a pointer that is being passed, thus we need to dereference it.
这篇关于C ++程序当方法返回值分配给int时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!