检查 C 中二维指针数组是否有用户定义的值?

发布于 2024-10-10 11:29:23 字数 378 浏览 9 评论 0原文

示例代码:

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 技术交流群。

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

发布评论

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

评论(5

魂牵梦绕锁你心扉 2024-10-17 11:29:23

使用 malloc 分配的内存内容(以及在堆栈上分配的变量)是未定义,因此它很可能是任何内容。通常,您会得到用零填充的空间(因为操作系统清空了其他进程使用的内存页)或先前使用这些内存页的残留物(如果内存页属于您的进程,通常会出现这种情况),但这是对于幕后发生的情况,C 标准不提供任何保证。

所以,一般来说,没有“默认值”,也没有办法检查你的内存是否已被更改;但是,您可以使用神奇值来初始化您使用的内存块,您确定这些值不会用作“真实数据”,但这只是应用程序内部的约定。

幸运的是,对于浮点变量,您可以使用几个神奇的值(例如安静的 NaN)来实现此目的;一般来说,您可以使用 中定义的宏 NANfloat 设置为 NaN。

顺便说一句,您不应该读取未初始化的 floatdouble ,因为它们存储的常用格式(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 a float to NaN.

By the way, you shouldn't read uninitialized floats and doubles, 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.

濫情▎り 2024-10-17 11:29:23

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.

橘虞初梦 2024-10-17 11:29:23

扩展 Matteo Italia 的好答案:

单个数组的初始化代码如下所示:

float* row;

row = malloc( numNodes*sizeof(float) );
for (int i=0; i<numNodes; ++i) {
    row[i] = nanf(); // set a Not-a-Number magic value of type float
}

(我将由您自行更改多维数组的值)

然后某处

float value = ...; // read the array
if (isnan(value)) {
    // not initialized
} else {
    // initialized - do something with this
}

:重要的是要记住:NaN == NaN 将产生 false,因此最好使用 isnan(),而不是 == 测试该值是否存在。

Extending the good answer of Matteo Italia:

The code of initialization of a single array would look like:

float* row;

row = malloc( numNodes*sizeof(float) );
for (int i=0; i<numNodes; ++i) {
    row[i] = nanf(); // set a Not-a-Number magic value of type float
}

(I'll leave it up to you to change this for your multi-dimensional array)

Then somewhere:

float value = ...; // read the array
if (isnan(value)) {
    // not initialized
} else {
    // initialized - do something with this
}

One thing is important to remember: NaN == NaN will yield false, so it's best to use isnan(), not == to test for the presence of this value.

我不吻晚风 2024-10-17 11:29:23

在 C 中,自动变量不会自动初始化。如果您想要的话,您需要显式地将变量设置为 0。

对于 malloc 来说也是如此,它不会初始化它分配的堆上的空间。如果你想初始化它,你可以使用calloc

a = malloc( numNodes*sizeof(float*) ); // no need to initialize this
for ... {
  a[i] = calloc( numNodes-1, sizeof(float) );
}

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 use calloc if you want to initialize it:

a = malloc( numNodes*sizeof(float*) ); // no need to initialize this
for ... {
  a[i] = calloc( numNodes-1, sizeof(float) );
}
眼眸 2024-10-17 11:29:23

在填充之前,我注意到数组中的每个块已经保存了这个值:-431602080.000000 并且不是 NULL。这是为什么?

malloc() 不会初始化它分配的内存。如果您想要 0 初始化,则需要使用 calloc()

void *calloc(size_t nelem, size_t elsize);

calloc() 函数为 nelem< 数组分配未使用的空间/code> 元素,每个元素的大小(以字节为单位)为 elsize。该空间应初始化为所有位 0。

Before populating, I noticed that each block in the array already holds this value: -431602080.000000 and not NULL. Why is this?

malloc() doesn't initialize the memory which it allocates. You need to use calloc() if you want 0 initialization

void *calloc(size_t nelem, size_t elsize);

The calloc() function allocates unused space for an array of nelem elements each of whose size in bytes is elsize. The space shall be initialized to all bits 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文