English 中文(简体)
Can 我明确告诉一项职能,即不接受点数与非飞跃变量的点数?
原标题:Can I tell a function explicitly to not accept pointers to non-heap variables?

我已注意到,g++足够聪明,可以确定某一职能何时回到临时/当地变量,例如:

int *foobar()
{
      int a;
      return &a;
}

Will result in:

 warning: address of local variable ‘a’ returned

是否有办法可以确定一种功能原型,只接收汇编者可以说不是临时性的点。 因此,我要说,我有职能。

 barfoo(int *a_int);

如果有人把地方/临时物体的点子带进去,我是否可以告诉G++? 这将禁止人们与无效的点人进行禁忌,并有可能挽救某些令人厌恶的问题。

例:

   void barfoo(int *a)
   {
        cerr << a << endl;
   };

   void foobar()
   {
        int a;
        barfoo(&a);
   }

我希望汇编者抱怨“禁运(和计划)”。

最佳回答

我认为,让汇编者加以执行是没有任何办法的,但你可以较早通过使用小型编辑发现一些事例。

void someFunc(int * mustBeHeap) {
   assert(0!=malloc_size(mustBeHeap));
   //do stuff
}

不幸的是,你们会从这样的法典中获得虚假的肯定:

void someOtherFunc() {
    int * myInts=(int *)malloc(sizeof(int)*20);
    someFunc(&(myInts[3]));
}

它以新的、推动的:集合等形式赢得了出色的工作。 事实上,你会从一切事情上得到虚假的肯定。

此外,小型水稻也不符合标准。

<><>Edit>:

在审视你上述关于拥有所有权的评论后,它像我描述的一些不实正面的东西一样,事实上也是你想要发现的情况,因为你打算把记忆从点上解放出来。

问题回答

你可以指示汇编者将这一警告列为错误。 但是,人们不正确地认识到如何界定你的问题。 接受当地点人的职能是:

int a;
...
do_something (&a);
printf ("%d
", a);

做你想要避免的事情是完全有用的。 海合会/G++不能猜测,当职能的最初打电话者已经离开电话时,使用参考/点名的功能将存放在某个地方,随后再使用。

你们也许能够找到一个汇编者,把警告作为错误处理(正如你应该说的!),但我不相信你会找到办法,去做你要求做的事情。

You can tell gcc to treat warnings as errors with -Werror option. Or make the specified warning as error -Werror=. 3.8 Options to Request or Suppress Warnings.

In c++ you could write a "guaranteed heap pointer" template class that contains a single real pointer inside but has tightly restricted constructors such that the only way to create one was by new. Overload enough operators, and you could have it behave almost like a naked pointer, except that it will have no conversion from such a pointer.

(I have my doubts about whether that will be worth the trouble, though).

从来没有像现在这样使用C++的原材料,没有很好的理由。 在这种情况下,由于你计划对点人拥有所有权,接受反映这一点的物体。 Either auto_ptrunique_ptr, 如果你想要独有,或commd_ptr。 如果你没有当家作主,就接受(可能)参考。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签