凭证数组

发布于 2024-11-18 10:58:52 字数 600 浏览 6 评论 0原文

我正在开发凭证提供者和凭证。所以我有这个类 SampleProviderSampleCredential。当我明确声明 SampleProvider 有一个或两个或恒定数量的 SampleCredential 时,效果很好,通过声明:

SampleCredential * _pCredential[2]

但现在,我希望它是动态分配的。所以我会这样:

SampleCredential * *_pCredential

然后在方法 SetUsageScenario() 中,代码将从 txt 文件中读取凭据数量,并分配它:

(*_pCredential) = new SampleCredential[numberCount];

但它不起作用。我在那条线上不断收到错误。它说

访问冲突写入位置 0x00000000 ;

你知道这里发生了什么以及该怎么做吗?

I'm developing a credential provider and credential. So I have this class SampleProvider and SampleCredential. It works well when I specifically declare that SampleProvider has one, or two, or a constant number of SampleCredential, by declaring:

SampleCredential * _pCredential[2]

But now, I want it to be dynamically allocated. So I will have this:

SampleCredential * *_pCredential

And then inside the method SetUsageScenario(), the code will read the number of credentials from a txt file, and allocate it:

(*_pCredential) = new SampleCredential[numberCount];

But it is not working. I keep getting error on that line. It says

Access violation writing location 0x00000000 ;

Do you know what happens here and what to do?

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

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

发布评论

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

评论(1

人疚 2024-11-25 10:58:52

_pCredential 可能尚未初始化并且仍为 NULL。尝试通过“(*_pCredential)”取消引用空指针将导致访问冲突。

也许你就是想这么做?

_pCredential = new SampleCredential*[numberCount];

这将分配一个指向 SampleCredential 对象的指针数组。然后,您可以像这样分配每个 SampleCredential 对象:

_pCredential[0] = new SampleCredential();
// etc.

完成后记得释放内存:

for (int i = 0; i < numberCount; i++) {
    delete _pCredential[i];
}
delete [] _pCredential;

_pCredential has probably not been initialized yet and is still NULL. Attempting to dereference the null pointer via "(*_pCredential)" would then result in your access violation.

Maybe you meant to do this?

_pCredential = new SampleCredential*[numberCount];

That will allocate an array of pointers to SampleCredential objects. You can then allocate each SampleCredential object like this:

_pCredential[0] = new SampleCredential();
// etc.

Remember to free the memory when you're done:

for (int i = 0; i < numberCount; i++) {
    delete _pCredential[i];
}
delete [] _pCredential;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文