凭证数组
我正在开发凭证提供者和凭证。所以我有这个类 SampleProvider
和 SampleCredential
。当我明确声明 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
_pCredential 可能尚未初始化并且仍为 NULL。尝试通过“(*_pCredential)”取消引用空指针将导致访问冲突。
也许你就是想这么做?
这将分配一个指向 SampleCredential 对象的指针数组。然后,您可以像这样分配每个 SampleCredential 对象:
完成后记得释放内存:
_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?
That will allocate an array of pointers to SampleCredential objects. You can then allocate each SampleCredential object like this:
Remember to free the memory when you're done: