在C中,为什么有人在释放之前投下指针?(In C, why do some people cast the pointer before freeing it?)

编程入门 行业动态 更新时间:2024-10-27 20:32:56
在C中,为什么有人在释放之前投下指针?(In C, why do some people cast the pointer before freeing it?)

我正在处理一个旧的代码库,几乎每一次调用free()都使用一个转换参数。 例如,

free((float *)velocity); free((float *)acceleration); free((char *)label);

其中每个指针都是相应的(和匹配)类型。 我完全看不到这一点。 这是非常古老的代码,所以我不知道是否是K&R的事情。 如果是这样,我实际上希望支持可能需要这样做的旧编译器,所以我不想删除它们。

有使用这些演员的技术原因吗? 我甚至没有看到使用它们的实际原因。 释放之前提醒自己数据类型的要点是什么?

编辑:这个问题不是另一个问题的重复。 另一个问题是这个问题的一个特例,我认为如果接近的选民将会阅读所有的答案,这是很明显的。

Colophon:我给“复选”复选标记,因为这是一个真正的理由,为什么这可能需要做; 然而,关于它是一个预先ANSI C定制的答案(至少在一些程序员中)似乎是我在这种情况下使用的原因。 许多人在这里有很多好处。 谢谢你的贡献。

I'm working on an old code base and pretty much every invocation of free() uses a cast on its argument. For example,

free((float *)velocity); free((float *)acceleration); free((char *)label);

where each pointer is of the corresponding (and matching) type. I see no point in doing this at all. It's very old code, so I'm left wondering if it's a K&R thing. If so, I actually wish to support the old compilers that may have required this, so I don't want to remove them.

Is there a technical reason to use these casts? I don't even see much of a pragmatic reason to use them. What's the point of reminding ourselves of the data type right before freeing it?

EDIT: This question is not a duplicate of the other question. The other question is a special case of this question, which I think is obvious if the close voters would read all the answers.

Colophon: I'm giving the "const answer" the checkmark because it is a bonafide real reason why this might need to be done; however, the answer about it being a pre-ANSI C custom (at least among some programmers) seems to be the reason it was used in my case. Lots of good points by many people here. Thank you for your contributions.

最满意答案

如果指针是const则可能需要转换来解决编译器警告。 这是一个代码引发警告而不转换参数的示例:

const float* velocity = malloc(2*sizeof(float)); free(velocity);

而编译器(gcc 4.8.3)则说:

main.c: In function ‘main’: main.c:9:5: warning: passing argument 1 of ‘free’ discards ‘const’ qualifier from pointer target type [enabled by default] free(velocity); ^ In file included from main.c:2:0: /usr/include/stdlib.h:482:13: note: expected ‘void *’ but argument is of type ‘const float *’ extern void free (void *__ptr) __THROW;

如果你使用free((float*) velocity); 编译器停止抱怨。

Casting may be required to resolve compiler warnings if the pointers are const. Here is an example of code that causes a warning without casting the argument of free:

const float* velocity = malloc(2*sizeof(float)); free(velocity);

And the compiler (gcc 4.8.3) says:

main.c: In function ‘main’: main.c:9:5: warning: passing argument 1 of ‘free’ discards ‘const’ qualifier from pointer target type [enabled by default] free(velocity); ^ In file included from main.c:2:0: /usr/include/stdlib.h:482:13: note: expected ‘void *’ but argument is of type ‘const float *’ extern void free (void *__ptr) __THROW;

If you use free((float*) velocity); the compiler stops complaining.

更多推荐

本文发布于:2023-04-27 21:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329031.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   投下   people   freeing   pointer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!