I am exploring some programs which contain thousands of lines in them over a range of files with almost as many variables and pointers in them. Whenever i encounter a variable, i have to trace it backwards in all the files to check whether its a simple pointer or an array, causing utter inconvenience. Is there a way that i make a function that tells me if there are more than one memory blocks associated with that pointer? Or is there a built in function for that, just giving binary answer..!!!
问问题
100 次
3 回答
1
简短的回答是否定的——即使在运行时也很难判断指针是否指向数组。
如果您使用一个好的 IDE,它可能会让您将鼠标悬停在变量名上并向您显示定义,这在很多情况下会为您提供所需的答案。
我使用 Eclipse,我发现它非常擅长告诉我变量的类型。其他人将使用其他 IDES;YMMV。
于 2012-08-29T09:49:34.803 回答
0
这段代码可能会对您有所帮助。
#include <iostream>
using namespace std;
typedef char true_type;
typedef struct{ char one; char two;} false_type;
template <size_t N, typename T>
true_type test_func( T (&anarr)[N]);
false_type test_func( ... );
{
template <typename T>
bool is_an_array( const T& a) // const reference is important !!
if (sizeof (test_func(a)) == sizeof(true_type) ) return true;
else return false;
}
int main()
{
char testarr[10] = {'a','b','c','d','e','f','g','h','i','j'};
if (is_an_array(testarr) ) cout << "testarr is an array" << endl; else cout <<
"testarr is not an array" << endl;
char a_char = 'R';
if (is_an_array(a_char)) cout << "a_char is an array" << endl; else cout << "a_char is
not an array" << endl;
}
于 2012-08-31T06:02:28.113 回答
0
您可以尝试使用交叉引用工具。有可能它的解析器足够笨,不会像成熟的 IDE 那样受到错误的阻碍。Source Navigator是我几年前玩过的。
于 2012-08-29T10:15:16.120 回答