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

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

问题描述

所以,我真的不知道如何把标题这个时候。首先我想说,我已经看到了这个网页有关,如果问题是有关功课的警示几个意见。
我的是,但它也完成了,我只是想进一步了解正在发生的事情与code。

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.

我有2行code的我不太在code和我一起工作理解。这项工作是关于让任何文件作为参数(如果是0的文件,从标准输入读取),并将其打印在标准输出倒退。所有这一切,谈论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;

和问题是,免费不工作,它不释放所占用的空间(也许我用错了?在其他地方,我已经知道如何使用它,但不是这一次)。随着测试我做什么,我已阅读,我认为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);

如果页头被用来复制我要放入数组线的确切长度,所以我可以,在这之后,只是打印文本倒退。

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不同(当前版本)。标准说(7.20.3.1为C11,为7.22.3.1 C1X)

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

所以,使用免费和不依赖于的realloc

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

当通过的char *字符串处理永远记住,以包括空终止一个额外的字符 \\ 0 。这是显示其中字符串端(另一个是一个明确的串长度)通常的方式。

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

在使用的malloc 免费记住,他们必须完全匹配。您需要免费确切的指针(值),通过的malloc 的realloc

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),而不是免费的(),并使用malloc一个字符串+1的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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