使用 realloc (X, 0) 而不是 free() 并使用长度为 +1 的字符串 malloc [英] Using realloc (X, 0) instead of free() and using malloc with length of a string +1

查看:48
本文介绍了使用 realloc (X, 0) 而不是 free() 并使用长度为 +1 的字符串 malloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这次我真的不知道如何放置标题.首先,我想说的是,如果问题与家庭作业"有关,我在此页面上看到了几条关于警告的评论.我的是,但它也完成了,我只是想进一步了解代码的情况.

So I don't really know how to put the title this time. First of all I'd like to say that I've seen several comments on this page about warning if the question is related to "homework". Mine is, but it's also completed and I just want to further understand what is going on with the code.

我也读过一段时间的帖子和书籍,但我觉得我仍然缺少一些东西.

I have also read posts and books for some time, but I think I am still missing things.

在我使用的代码中有两行代码我不太明白.这项工作是关于获取用作参数的任何文件(如果它是 0 个文件,则从 stdin 读取),并将其向后打印在标准输出上.所有这一切,在我试图放入标签时谈论 C.

I have 2 lines of code I don't quite understand in the code I worked with. The work is about getting whatever file is used as argument (if it's 0 files, it read from stdin), and print it on the standard output backwards. All of this, talking about C as I tried to put in the tag.

第一个问题是:

array = realloc (array, 0);

其中数组定义为

char **array;

问题是 free 不起作用,它没有释放使用的空间(也许我用错了?在其他地方我知道如何使用它,但这次不知道).根据我所做的测试和阅读的内容,我相信 realloc 正在做同样的事情,但我不是 100%.

And the problem is that free doesn't work, it does not free the space used (maybe I used it wrong? In other place I have known how to use it, but not this time). With the testing I have done and what I have read, I believe that realloc is doing the same, but I'm no 100%.

第二个是:

char* alloc = malloc (strlen ((char*)string)+1);

其中 alloc 用于复制我要放入数组的行的确切长度,因此我可以在此之后向后打印文本.

Where alloc is used to copy the exact length of the line I am going to put into an array, so I can, after that, just print the text backwards.

问题是为什么我必须使用 +1.我的意思是,如果我出于某种原因不使用它就不起作用,我尝试了不同的数字并且每次都有效,但是如果我不这样做 +1,它就无法正常工作.

And the question is why I have to use that +1. I mean if I don't use for some reason it doesn't work, I tried with different numbers and it works everytime, but if I don't do that +1 it does not work correctly.

我知道这个问题可能太含糊和写得太糟糕而无法真正回答,但我又一次不确定,我尽力解释自己(英语没有母语,因为这可能很明显).

I know probably the question is too vague and bad written to really be answered but again, I'm not sure about that and I did my best to explain myself (english no mother tongue as it's probably obvious).

推荐答案

realloc 在大小为 0 时的行为在 C11(当前版本)中有所不同.标准说(C11 为 7.20.3.1,C1x 为 7.22.3.1)

The behavior of realloc when the size is 0 is different in C11 (the current version). The standard says (7.20.3.1 for C11, 7.22.3.1 for C1x)

如果请求的空间大小为零,则行为为实现定义:要么返回空指针,要么返回行为就像大小是某个非零值,除了返回的指针不得用于访问对象

If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object

所以,使用free,不要依赖realloc.

So, use free and don't rely on realloc.

当通过char* 处理字符串时,请始终记住为空终止符 添加一个额外的字符.这是显示字符串结束位置的常用方法(另一种是显式字符串长度).

When dealing with strings via char* always remember to include one extra character for the null terminator . This is the usual way to show where the string ends (the other being an explicit string length).

在使用 mallocfree 时,请记住它们必须完全匹配.您需要free mallocrealloc 返回的确切指针(值).

When using malloc and free remember that they must be matched exactly. You need to free the exact pointer (value) returned by malloc or realloc.

这篇关于使用 realloc (X, 0) 而不是 free() 并使用长度为 +1 的字符串 malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆