如何在运行时将指针设置为 Null (C++)

发布于 2024-09-27 21:39:16 字数 274 浏览 3 评论 0原文

现在我有一个指针设置为二维数组中的一行。我希望该指针停止指向该行,但稍后我将使用该指针来做其他事情。我只想知道如何在初始化并指向一行后取消设置指针。

double* tempRow;
tempRow = (double*) malloc(size * sizeof(double));
   ...
tempRow = NULL;

不会取消 tempRow 变量与数组行的链接。为什么不呢?

我想知道我是否应该使用 C 语言。使用向量时会产生开销吗?

Right now I have a pointer set to a row in my 2D array. I want that pointer to stop pointing to that row, but I will be using the pointer later for something else. I just want to know how to unset the pointer after it is initialized and has pointed to a row.

double* tempRow;
tempRow = (double*) malloc(size * sizeof(double));
   ...
tempRow = NULL;

doesn't unlink the tempRow variable from the array row. Why not?

I wonder if I should be using C then instead. Will there be overhead when using a vector?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泅人 2024-10-04 21:39:16

虽然您已编写将 tempRow 设置为 NULL,但它不会释放您分配的内存。为此,您需要

free(tempRow);
tempRow = NULL;

但是,如果您按照标签建议使用 C++,则最好使用 C++ new/delete,

double* tempRow;
tempRow = new double[size];
   ...
delete [] tempRow;
tempRow = NULL;

您甚至可以使用 STL 来为您处理内存分配。

std::vector<double> tempRow(size);
// You can access the data, in a similar way to an array
tempRow[5] = tempRow[4]+tempRow[3];

// If you really need to access the underlying pointer, (To pass to another 
// function for example) you can do this. Note that the pointer will be valid
// until the vector is destroyed or modified in certain ways that can cause the
// vector to reallocate its memory. So you can't use this to pass data to a 
// function that destroys or takes ownership of the passed in pointer.

fn_requiring_pointer( &temp[0] );

// The memory used in tempRow will get cleaned up automatically when the 
// object goes out of scope
//
// If I really need to free up the memory used in it early I can use a swap 
// hack. (iirc tempRow.clear() isn't guaranteed to release the memory)
std::vector<double>().swap(tempRow); // Unneeded in most cases.

尝试将 tempRow 指针重用于不相关的内容可能也没有必要。只需创建一个具有不同名称的新指针即可。在多个不同的不相关目的中重复使用变量可能会使代码以后很难理解。

While you have written will set tempRow to NULL, it wont release the memory you have allocated. For that you need

free(tempRow);
tempRow = NULL;

However if you're using C++ as the tags suggest, you'd be better off using C++ new/delete

double* tempRow;
tempRow = new double[size];
   ...
delete [] tempRow;
tempRow = NULL;

you can even use the STL to handle your memory allocation for you.

std::vector<double> tempRow(size);
// You can access the data, in a similar way to an array
tempRow[5] = tempRow[4]+tempRow[3];

// If you really need to access the underlying pointer, (To pass to another 
// function for example) you can do this. Note that the pointer will be valid
// until the vector is destroyed or modified in certain ways that can cause the
// vector to reallocate its memory. So you can't use this to pass data to a 
// function that destroys or takes ownership of the passed in pointer.

fn_requiring_pointer( &temp[0] );

// The memory used in tempRow will get cleaned up automatically when the 
// object goes out of scope
//
// If I really need to free up the memory used in it early I can use a swap 
// hack. (iirc tempRow.clear() isn't guaranteed to release the memory)
std::vector<double>().swap(tempRow); // Unneeded in most cases.

Also trying to reuse the tempRow pointer for something unrelated is probably not necessary. Just create a new pointer with a different name. Reusing a variable form multiple different unrelated purposes can make code very hard to understand later.

好菇凉咱不稀罕他 2024-10-04 21:39:16

我也是 C++ 新手,但不久前,有人告诉我,使用 std::vector 是处理数据数组的更安全的方法。

  • 添加更多元素时自动重新分配。
  • #include 中的内容一起使用的迭代器。
  • 通过 .at(index) 元素访问进行边界保护。
  • 不需要混乱的指针跟踪。
  • 使用 operator[] 进行 C 数组样式访问。
  • RAII。

你可以像这样声明一个向量:

std::vector<double> tempRow(size);

tempRow[0] = 3.00;
tempRow[1] = 1.00;

// no need to use delete[] or free(), it will destruct itself
// and relinquish its resources automatically.

I'm new at C++ as well, but a while ago, someone told me that using std::vector is a much safer approach to handling arrays of data.

  • Automatic re-allocation when adding more elements.
  • Iterators for use with stuff from #include <algorithm>.
  • Bounds-protection with .at(index) element access.
  • No messy pointer-tracking required.
  • C-array style access with operator[].
  • RAII.

You would declare a vector like this:

std::vector<double> tempRow(size);

tempRow[0] = 3.00;
tempRow[1] = 1.00;

// no need to use delete[] or free(), it will destruct itself
// and relinquish its resources automatically.
策马西风 2024-10-04 21:39:16

您展示的示例应该可以工作。
此外,如果您在创建 temRow NULL 之前没有释放内存,则 <强>内存泄漏

double* tempRow;
tempRow = (double*) malloc(size * sizeof(double));
   ...
free(tempRow);  // free the memory.
tempRow = NULL; // reset the pointer.
   ...
tempRow = &some_other_double_var; // reuse the pointer.

The example you've shown should work.
Also if you've not freed the memory before making temRow NULL, you are leaking memory.

double* tempRow;
tempRow = (double*) malloc(size * sizeof(double));
   ...
free(tempRow);  // free the memory.
tempRow = NULL; // reset the pointer.
   ...
tempRow = &some_other_double_var; // reuse the pointer.
铃予 2024-10-04 21:39:16

似乎不起作用?

这是最糟糕的投诉,也是解决方案提供商的噩梦。

你的意思是你得到编译错误?

如果是,您是否包含 ?using namespace std;

Doesn't seem to work?

That's the worst complaint and a solution providers's nightmare.

Do you mean you get a compilation error?

If yes, did you include <cstdio>? and using namespace std;

给我一枪 2024-10-04 21:39:16

用什么方法不行呢?在 C++ 中“取消设置”指针的正常方法是:

tempRow = 0;

但是假设您已包含正确的标头或以其他方式对 NULL 有正确的定义,那么您所拥有的应该没问题。

顺便说一句,在丢失指针之前,您应该首先在该内存上调用 free() ,否则会出现内存泄漏(假设您有充分的理由使用 C 风格 < code>malloc/free 而不是更 kosher C++ new/delete)。

Doesn't work in what way? The normal way to "unset" a pointer in C++ is with:

tempRow = 0;

but what you have should be fine, assuming you've included the correct headers or otherwise have the correct definition for NULL.

As an aside, you should first call free() on that memory before losing the pointer, otherwise you'l have a memory leak (and this is assuming you have a good reason to use C-style malloc/free instead of the more kosher C++ new/delete).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文