English 中文(简体)
在使用点人进行职能论证时,“未实现的当地变量”错误
原标题:"uninitialized local variable" error when using pointers for function arguments
  • 时间:2014-06-29 00:37:56
  •  标签:
  • c++

I have a function:

VOID GetOSVersion(PDWORD major, PDWORD minor, PDWORD build)
{
    OSVERSIONINFO osver;
    ZeroMemory(&osver, sizeof(OSVERSIONINFO));
    osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&osver);
    if(major)
    *major = osver.dwMajorVersion;
    if(minor)
    *minor = osver.dwMinorVersion;
    if(build)
    *build = osver.dwBuildNumber;
}

And I wanted to invoke it like this:

PDWORD major;
PDWORD minor;
PDWORD build;
GetOSVersion(major, minor, build);

我对这三个论点都犯了错误:

A. 尚未开始的当地变量

在我的领导下,我宣布<代码>major ,minor,build<>code>,并在这一职能中填满。 在援引法的前三行,已经为他们分配了空间。

我在这里肯定没有东西。 请允许我向我解释这一点吗?

最佳回答

问题在于:

DWORD major;
DWORD minor;
DWORD build;
GetOSVersion(&major, &minor, &build);

九:

VOID GetOSVersion(PDWORD major, PDWORD minor, PDWORD build)
{
    OSVERSIONINFO osver = {};
    osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&osver);
    if(major)
    *major = osver.dwMajorVersion;
    if(minor)
    *minor = osver.dwMinorVersion;
    if(build)
    *build = osver.dwBuildNumber;
}

DWORD major = 0;
DWORD minor = 0;
DWORD build = 0;
GetOSVersion(&major, &minor, &build);

PDWORD是DWORD的协调人。 所有三个参数均为产出参数。 在C/C++中,它是一种共同的用法:如果你想从一项功能中再回一个价值,那么你需要转站(或者在C++的情况下也提及)到一个变量:

int var = 0;
if(some_function_that_can_fail_and_write_result(&var))
 ;//do something

在您的情形下,你通过一个价值不变的职能。 这与:

void foo(int parameter);
// ...
int a;
foo(a);

你有多种途径:

参考通过未经初步确定的要点:

VOID GetOSVersion(PDWORD& major, PDWORD&, PDWORD&)
{
//...
major = new DWORD(osver.dwMajorVersion);

}
// usage:
PDWORD major;
GetOSVersion(major, ....);

//...
delete major;

参照所有参数:

VOID GetOSVersion(DWORD& major, DWORD&, DWORD&)
{
//...
major = osver.dwMajorVersion;

}
// usage:
DWORD major = 0;
GetOSVersion(major, ....);

利用你版本的GeOSVersion(GetOSVersion),但首先用这一回答中的固定办法。

问题回答

你们正在犯许多人在涉及要求提出论据的职能时所犯的错误。

如果一项职能需要点人作为理由,它并不意味着你盲目地宣布点人,将其交给职司。 该职能要求的是:一个现有的、有效的实体,即<条码>地址-of

DWORD maj或, min或, build;
GetOSVersion(&maj或, &min或, &build);

以上“DWORDs有效,所有已完成的工作都是将这些变量的地址传递给功能。

与此相关的其他错误(不是ug,因为它会带来预期结果,但仍然是“礼让”)是宣布点名,指出点到哪里是有效的,然后将其交给职能。 换言之:

PDWORD maj或, min或, build;
maj或 = new DWORD;
min或 = new DWORD;
build = new DWORD;
GetOSVersion(maj或, min或, build);
delete maj或;
delete min或;
delete build;

PDWORD pmaj或, pmin或, pbuild;
DWORD maj或, min或, build;
pmaj或 = &maj或;
pmin或 = &pmin或;
pbuild = &build;
GetOSVersion(pmaj或, pmin或, pbuild);

我看到了以这种方式撰写的法典。 这表明,方案管理员对于职能需要点人作为理由时意味着什么没有清楚的了解。 方案管理员错误地认为,应宣布点名must,使之具有一定的效力,然后通过点名。

Yes, you get the results without crashing, but it is a waste of time to call the allocat或 (new / delete), 或 if not calling the allocat或, create unnecessary pointer variables which unnecessarily obfuscates the code.

因此,最容易的方式是上述第一个例子。 只是宣布非点类型,只是通过地址。

你可能打算发表你们可以变的宣言,并将你的职责称为你们。

DWORD major;
DWORD minor;
DWORD build;
GetOSVersion(&major, &minor, &build);

You use pointers to reference the output parameters, thus these need to point them to valid memory addresses. You can refer to those variables to get a valid pointer using the address-of (&) operator as shown above.


有了一套标准,你可以参考参数加以利用,从而使得事情更加清楚。

VOID GetOSVersion(DWORD& major, DWORD& minor, DWORD& build) {
    OSVERSIONINFO osver;
    ZeroMemory(&osver, sizeof(OSVERSIONINFO));
    osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&osver);
    // Note there s no check needed if the pointers are valid!
    major = osver.dwMajorVersion;
    minor = osver.dwMinorVersion;
    build = osver.dwBuildNumber;
}

DWORD major;
DWORD minor;
DWORD build;
GetOSVersion(major, minor, build);

无需将<代码>新()分配器(和两者兼有正确的动态记忆分配管理)与以上任何样本放在1位。

这些都是要点。 他们没有看到你所分配的记忆。 他们没有“填满”这一功能,而是用来获取(未启用)记忆。

You are probably not getting an error but a warning (but you might have configured your complier to treat warnings as errors).

如果执行,你的方案将失败,因为你写给他们指出的记忆,但是由于这些记忆没有初步形成,它们含有无效/占有权的地址。

Possible solution

PDWORD major = new DWORD;
PDWORD minor = new DWORD;
PDWORD build = new DWORD;

<代码>PDWORD的定义为*DWORD

不要忘记删除。

<>:> 实际上,在轮椅上分配这些物品更为明智——见用户2451677的答复。





相关问题
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?

热门标签