检查 C 中二维指针数组是否有用户定义的值?
示例代码:
float** a;
a = (float**) malloc(numNodes * sizeof(float*));
for(int i=0; i<`numNodes`; i++)
{
a[i] = (float*)malloc((numNodes-1) * sizeof(float));
}
我正在上面创建一个动态二维数组。在填充之前,我注意到数组中的每个块已经保存了这个值:-431602080.000000 并且不是 NULL。这是为什么?
在某些情况下,并非数组中的所有空格都被使用。
所以,我的查询很简单,有没有一种优雅的方法来检查每个块是否具有此默认值或用户定义的值?
提前致谢。
Sample code:
float** a;
a = (float**) malloc(numNodes * sizeof(float*));
for(int i=0; i<`numNodes`; i++)
{
a[i] = (float*)malloc((numNodes-1) * sizeof(float));
}
I am creating a dynamic 2d array above. Before populating, I noticed that each block in the array already holds this value: -431602080.000000 and not NULL. Why is this?
There are situations where not all spaces within the array are used.
So, my query is simple, Is there an elegant way to check if the each block has this default value or a user defined value?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
malloc
分配的内存内容(以及在堆栈上分配的变量)是未定义,因此它很可能是任何内容。通常,您会得到用零填充的空间(因为操作系统清空了其他进程使用的内存页)或先前使用这些内存页的残留物(如果内存页属于您的进程,通常会出现这种情况),但这是对于幕后发生的情况,C 标准不提供任何保证。所以,一般来说,没有“默认值”,也没有办法检查你的内存是否已被更改;但是,您可以使用神奇值来初始化您使用的内存块,您确定这些值不会用作“真实数据”,但这只是应用程序内部的约定。
幸运的是,对于浮点变量,您可以使用几个神奇的值(例如安静的 NaN)来实现此目的;一般来说,您可以使用
中定义的宏NAN
将float
设置为 NaN。顺便说一句,您不应该读取未初始化的
float
和double
,因为它们存储的常用格式(IEEE 754)包含一些神奇的值(例如信号NaN),在读取它们时可能会引发算术异常,因此如果您的未初始化内存恰好包含此类位模式,您的应用程序可能会崩溃。The content of memory allocated with
malloc
(as well as of variables allocated on the stack) is undefined, so it may very well be anything. Usually you get space filled with zeroes (because the OS blanks memory pages that were used by other processes) or residues of the previous use of those memory pages (this is often the case if the memory page belonged to your process), but this is what happens under the hood, the C standard does not give any guarantees.So, in general there's no "default value" and no way to check if your memory has been changed; however you can init the memory blocks you use with magic values that you're sure that will not be used as "real data", but it'll be just a convention internal to your application.
Luckily, for floating point variables there are several magic values like quiet NaN you can use for this purpose; in general you can use the macro
NAN
defined in<math.h>
to set afloat
to NaN.By the way, you shouldn't read uninitialized
float
s anddouble
s, since the usual format they are stored in (IEEE 754) contains some magic values (like the signaling NaN) that can raise arithmetic exceptions when they are read, so if your uninitialized memory happens to contain such bit pattern your application will probably crash.C 运行时不需要初始化您未自己初始化的任何内存,并且它们保存的值本质上是上次使用该内存时留下的随机垃圾。您必须首先将它们全部显式设置为 NULL 或使用 calloc。
C runtimes are not required to initialize any memory that you didn't initialize yourself and the values that they hold are essentially random garbage left over from the last time that memory was used. You will have to set them all to NULL explicitly first or use calloc.
扩展 Matteo Italia 的好答案:
单个数组的初始化代码如下所示:
(我将由您自行更改多维数组的值)
然后某处
:重要的是要记住:
NaN == NaN
将产生 false,因此最好使用isnan()
,而不是==
测试该值是否存在。Extending the good answer of Matteo Italia:
The code of initialization of a single array would look like:
(I'll leave it up to you to change this for your multi-dimensional array)
Then somewhere:
One thing is important to remember:
NaN == NaN
will yield false, so it's best to useisnan()
, not==
to test for the presence of this value.在 C 中,自动变量不会自动初始化。如果您想要的话,您需要显式地将变量设置为 0。
对于
malloc
来说也是如此,它不会初始化它分配的堆上的空间。如果你想初始化它,你可以使用calloc
:In C automatic variables doesn't get automatically initialized. You need to explicitly set your variable to 0, if it's what you want.
The same is true for
malloc
that does'n initialize the space on the heap it allocates. You can usecalloc
if you want to initialize it:malloc()
不会初始化它分配的内存。如果您想要0
初始化,则需要使用calloc()
calloc()
函数为nelem< 数组分配未使用的空间/code> 元素,每个元素的大小(以字节为单位)为
elsize
。该空间应初始化为所有位 0。malloc()
doesn't initialize the memory which it allocates. You need to usecalloc()
if you want0
initializationThe
calloc()
function allocates unused space for an array ofnelem
elements each of whose size in bytes iselsize
. The space shall be initialized to all bits 0.