确定malloc的大小 [英] Determine the size of malloc

查看:101
本文介绍了确定malloc的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i不知道字符串的输入大小是什么。


因此我无法在malloc函数之前获得输入。


i想要确定malloc的大小而不提前输入。


如何处理它?<​​br />

解决方案

文章< 86 ******************************* ***@w4g2000prd.g ooglegroups.com>,

< jt ***** @ naver.comwrote:


> ;我不知道字符串的输入大小是什么。

所以我无法在malloc函数之前获得输入。

我想确定malloc的大小而不用提前输入。

如何处理它?



分配一个合理的金额,如果它溢出realloc()它。


- Richard

-

:wq


jt *****@naver.com 写道:


>

i不知道输入的大小是多少字符串是。所以我无法获得

在malloc函数之前输入。我想要确定

malloc的大小而不提前输入。



/ *获取足够的内存来保存s * /
的副本
void * mallocforstring(char * s){

void * p;


if((p = malloc(1 + strlen(s)))return p;

else {

put(无内存可用);

退出(EXIT_FAILURE);

}

}


-

[邮件]:Chuck F(cinefalconer at maineline dot net)

[page]:< http:// cbfalconer.home.att.net>

尝试下载部分。

**发自 http://www.teranews.com


2008年5月18日13:10,Jens Thoms Toerring写道:


一些友好的人已经花时间写了一个

函数就这样做了,参见例如ggets()函数通过

CB Falconer:



*请*不要将此垃圾推荐给unsus预计可能实际使用它的新手b
。 ggroup的许多问题已经在这个小组中再次讨论过了,并且Falconer在拒绝接受任何批评时总是表现出他通常的b
猪头脑。 br />

OP应调查其实现的任何函数作为标准由

实现:GNU getline()或fgetln()或类似的。



i don''t know what input size of string is.

so i can''t gets inputsize before malloc function.

i want determine the size of malloc without get inputsize in advance.

how to deal with it?

解决方案

In article <86**********************************@w4g2000prd.g ooglegroups.com>,
<jt*****@naver.comwrote:

>i don''t know what input size of string is.

so i can''t gets inputsize before malloc function.

i want determine the size of malloc without get inputsize in advance.

how to deal with it?

Allocate a reasonable amount, and if it overflows realloc() it.

-- Richard
--
:wq


jt*****@naver.com wrote:

>
i don''t know what input size of string is. so i can''t gets
inputsize before malloc function. i want determine the size of
malloc without get inputsize in advance.

/* Get sufficient memory to hold a copy of s */
void *mallocforstring(char *s) {
void *p;

if ((p = malloc(1 + strlen(s))) return p;
else {
puts(" No memory available");
exit(EXIT_FAILURE);
}
}

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **


On 18 May 2008 at 13:10, Jens Thoms Toerring wrote:

Some friendly people have already taken the time to write a
function just doing this, see e.g. the ggets() function by
C.B. Falconer:

*Please* don''t recommend this garbage to unsuspecting newbies who might
actually use it. The many problems of ggets have been discussed over and
over again in this group, and Falconer has always shown his usual
pig-headedness in refusing to accept any criticism.

The OP should investigate whatever functions are provided as standard by
his implementation: GNU getline() or fgetln() or similar.


这篇关于确定malloc的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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