[英]C++ Pointers and Memory Allocation
I am taking a C++ class in school and was give a few lines of code with errors and I'm having trouble finding all 4 errors.我在学校参加了 C++ class 并给出了几行有错误的代码,我无法找到所有 4 个错误。 The code is supposed to print the number "302" I'm not good with pointers.
该代码应该打印数字“302”我不擅长指针。 Here is the code
这是代码
int main () {
int* ptr;
int* temp;
int x;
ptr = int;
*ptr = 3;
cout << ptr << endl;
x=0;
temp = x;
cout<<*temp<< endl;
ptr = int;
*ptr = 2;
cout<<*ptr-*temp <<endl;
return 0;
}
The two errors i have found so far are到目前为止我发现的两个错误是
Include <iostream>
to sort out cout
, endl
errors.包括
<iostream>
以解决cout
和endl
错误。 Typically C++ compiler needs to know from where these functions are coming.通常 C++ 编译器需要知道这些函数来自哪里。
#include <iostream>
use std::cout
and std::endl
instead of just cout
and endl
.使用
std::cout
和std::endl
而不仅仅是cout
和endl
。
new
operator was missed in ptr
and temp
pointers ptr
和temp
指针中缺少new
运算符
ptr = new int;
temp = new int;
Note, as you are dynamically allocating memory for ptr
and temp
, ensure it is removed via delete ptr
, delete temp
after its usage.请注意,当您为
ptr
和temp
动态分配 memory 时,请确保通过delete ptr
将其删除,并在使用后delete temp
。
Other answers already pointed out the problems, their explanation and a possible solution, except for problem in this:其他答案已经指出了问题,他们的解释和可能的解决方案,除了这个问题:
- temp = x needs to be a pointer, *temp = x
temp = x 需要是一个指针,*temp = x
No, you are wrong here.不,你在这里错了。 Pointer
temp
is uninitialised and dereferencing an uninitialised pointer ( *temp
) will lead to Undefined Behaviour .指针
temp
未初始化,取消引用未初始化的指针 ( *temp
) 将导致未定义的行为。 Two ways you can solve it:两种方法可以解决它:
First , allocate memory to temp
首先,将memory分配给
temp
temp = new int;
and then you can do this然后你可以这样做
*temp = x;
In this, the memory allocated to temp
will have an independent copy of value of x
.在这种情况下,分配给
temp
的 memory 将具有x
值的独立副本。 Make any changes to value of x
does not reflect in the content of memory pointed by temp
pointer and vice versa.对
x
值的任何更改都不会反映在temp
指针指向的 memory 的内容中,反之亦然。 Make sure to deallocate the storage allocated to temp
, once you done with it because the memory it is pointing to is allocated dynamically.确保释放分配给
temp
的存储,一旦你完成它,因为它指向的 memory 是动态分配的。
Second , assign address of x
to temp
pointer其次,将
x
的地址分配给temp
指针
temp = &x;
temp
pointer is pointing to x
. temp
指针指向x
。 Make any changes to the value of x
and *temp
will give the current value of x
and vice versa.对
x
的值进行任何更改, *temp
将给出x
的当前值,反之亦然。 No need to deallocate the temp
because the memory it is pointing to is not allocated dynamically.不需要取消分配
temp
文件,因为它指向的 memory 不是动态分配的。
For less line of code i prefer using namespace std globally, this will resolve the problem of using std::cout everytime对于更少的代码行,我更喜欢全局使用命名空间 std,这将解决每次使用 std::cout 的问题
using namespace std;
other than that you will have to allocate memory to the pointer after declaring it which can be done by using the new operator(don't forget to deallocate the memory after ur work is done using the delete operator), so instead of ptr = int;
除此之外,您必须在声明指针后将 memory 分配给指针,这可以通过使用 new 运算符来完成(不要忘记在使用 delete 运算符完成工作后释放 memory),而不是
ptr = int;
use利用
ptr = new int;
also using temp = x;
也使用
temp = x;
is wrong as u are assigning the address of pointer temp to a varible value, using temp = &x;
是错误的,因为您使用
temp = &x;
should be the right approach应该是正确的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.