2010-10-19 124 views
0

现在我有一个指针设置为我的二维数组中的一行。我希望该指针停止指向该行,但我将在稍后使用指针来进行其他操作。我只想知道如何在指针初始化并指向一行后取消设置。如何在运行时将指针设置为空(C++)

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

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

不知我是否应该用C来代替。使用矢量时会有开销吗?

+3

那么,你从学习这本书吗?这是非常糟糕的C++代码,很简单,并且被任何[入门书]所覆盖(http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。您应该选择一个,以便您可以学习C++。答案当然是使用'std :: vector '。 “看起来不起作用”为 – GManNickG 2010-10-19 06:14:13

+1

-1。 – 2010-10-19 07:31:24

+0

似乎不起作用的是指代码上面的句子。即使用提供的代码“在初始化指针并指向一行后取消设置指针......似乎不起作用。” – Stuart 2010-10-19 16:04:53

回答

4

似乎不工作?

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

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

如果是的话,你有没有包括<cstdio>?using namespace std;

2

不以何种方式工作?正常途径“未设置” C++中的指针是:

tempRow = 0; 

,但你必须要细,假设你已经包括了正确的头或其他什么有NULL正确的定义。另外,在丢失指针之前,你应该首先调用free(),否则你会发生内存泄漏(并且这是假设你有一个很好的理由使用C风格malloc/free而不是更多的Kosher C++ new/delete)。

11

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

free(tempRow); 
tempRow = NULL; 

但是如果你使用C++作为标签建议,你会更好使用C++新/删除

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指针的东西不相关可能没有必要。只需创建一个不同名称的新指针。重复使用变量形式多个不同的不相关的目的可能会使代码很难在以后明白。

+6

我听说'std :: vector tempRow(size)'是使用'new []'和'delete []'的好替代方法,但我是C++ noob,因此请带上盐。 – dreamlax 2010-10-19 06:10:04

+1

同意std :: vector 往往更好,如果你不真的需要指针。添加到答复中。 – 2010-10-19 06:12:28

+0

@ dreamlax这是一个很好的建议,因为矢量处理内存。 – Rudi 2010-10-19 06:22:08

4

您显示的示例应该可以工作。
此外,如果你没有做temRowNULL之前释放的内存,你是泄漏内存。

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. 
+0

我对内存分配了解不多。不释放只是未分配内存?因为那不是我想要的。我希望稍后在程序中重用内存。我可以释放并设置为NULL,但是随后我不得不在每次使用它时重新分配指针,我宁愿避免开销。也许我误解了,因为在你的例子中,你正在释放,设置为NULL,然后在没有malloc调用的情况下重用它。它是否正确? – Stuart 2010-10-19 16:13:37

+0

@Stuart:当您将指针设置为NULL并且您不想免费调用时,您需要确保有另一种方法可以分配内存。假设你可能有另一个指向它的指针。如果你没有这样的方式,那么你正在泄漏内存,那就是没有办法去解决这个问题。 – codaddict 2010-10-19 16:20:37

+0

@Stuart:很难理解你在问什么。 'free()'释放内存。如果你想保留内存供以后重用,你需要保留一个指针。如果你做'tempRow = NULL;'没有'free(tempRow);'首先,你会得到最糟糕的两个:不可重用的内存,甚至不能重新分配。顺便说一句,你可能会做得更好,只是“释放内存并在以后获取新内存”。开销不太可能会那么糟糕,而且你似乎并不熟悉内存分配。 – 2010-10-19 16:24:37

6

我在C新型++为好,但前一阵子,有人告诉我,使用std::vector是处理数据的阵列,更安全的方法。

  • 添加更多元素时自动重新分配。
  • 迭代器与#include <algorithm>的东西一起使用。
  • 边界保护与.at(index)元素访问。
  • 没有杂乱的指针跟踪需要。
  • C型阵列样式访问operator[]
  • 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.