操纵和记忆分配 [英] manipulation and memory allocation

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

问题描述

当编写一个操作数据的函数(例如,字符串)时,

原则上分配/解除分配内存的好或坏主意

功能?例如,我有以下功能:


char * to_bits(int n,char * s,int b){

int i;


if(b == 0)

b = 32;


if(s!= NULL){


free(s);

}


s =(char *)malloc(sizeof(char)*(b + 1));


for(i = b - 1; i> -1; i--){


if(( 0x01<< i)& n){


s [b - i - 1] =''1'';

}

else {


s [b - i - 1] =''0'';

}

}


s [b] =''\ 0'';


返回s;

}

此函数将整数转换为其

位的字符串表示形式。如您所见,它在

命令中分配和释放自己的内存以返回一个新字符串。另一种方法是在函数之外分配

所需的内存,例如:将函数调用为

如下:


s = to_bits(n,s = malloc(TB_SSIZE),int b);


其中TB_SSIZE是字符串所需的长度。


方法好还是坏,为什么或为什么不呢?

解决方案

io ********* @ hotmail。 com 写道:

s =(char *)malloc(sizeof(char)*(b + 1));


s = malloc(b + 1);


演员阵容不好。

sizeof(char)是总是一个。


你可以更简单地发布一个关于malloc返回值的基本计划B,



比必须解释代码中缺少的原因。

if(s == NULL){

exit(EXIT_FAILURE) ;

}

此函数将整数转换为其
位的字符串表示形式。正如您所看到的,它会分配和释放自己的内存,以便返回一个新字符串。
另一种方法是在函数外部分配所需的内存,例如:调用函数
如下:

s = to_bits(n,s = malloc(TB_SSIZE),int b);

其中TB_SSIZE是所需的长度字符串。

是方法好还是坏,为什么或为什么不呢?




我喜欢第二种方法。

可能会输出一大堆字符串

并且你可以为所有字符串使用相同的缓冲区,

即是说s可能是一个自动数组。


char s [TB_SSIZE + 1];

http://groups-beta.google.com/group/...87e58d46 ?hl = en


-

pete


pete写道:< blockquote class =post_quotes> io*********@hotmail.com 写道:

s =(char *)malloc(sizeof(char)*(b + 1)) ;
s = malloc(b + 1);

演员阵容很糟糕。
sizeof(char)总是一个。



好​​吧,我认为它取决于平台。

我喜欢第二种方法。
可能会输出一大堆字符串
并且你可以为所有这些使用相同的缓冲区,
也就是说s可以是一个自动数组。

char s [TB_SSIZE + 1];




这是一个很好的理由,谢谢。


io ********* @ hotmail.com 写道:

编写一个操作数据的函数(用于例如,一个字符串),原则上在该函数中分配/解除分配内存是一个好主意还是坏主意?例如,我有以下功能:

char * to_bits(int n,char * s,int b){
int i;

if(b == 0)
b = 32;

if(s!= NULL){

免费;
}

s =(char *)malloc(sizeof(char)*(b + 1));

for(i = b - 1; i> -1; i--) {

if((0x01<< i)& n){

s [b - i - 1] =''1'';
}
其他{

s [b - i - 1] =''0'';
}
}

s [b] =''\ 0'';

返回s;
}

此函数将整数转换为其
位。正如您所看到的,它会分配和释放自己的内存,以便返回一个新的字符串。替代方案是在功能之外分配所需的存储器,例如,调用函数
如下:

s = to_bits(n,s = malloc(TB_SSIZE),int b);

其中TB_SSIZE是所需的长度字符串。

是方法好还是坏,为什么或为什么不呢?




第二种方法更灵活,例如客户想要

来使用非​​堆分配的数组,所以我的建议是:


/ * to_bits(n,s,b)返回..其中s至少包含b + 1个元素。 * /


char * to_bits(int n,/ * out * / char * s,int b)

{

int i;


断言((n> = 0)&&(b> = 0));

if(b == 0){b = 32; }

for(i = b - 1; i> -1; i--){

if((0x01<< i)& n) {s [b - i - 1] =''1''; }

else {s [b - i - 1] =''0''; }

}

s [b] =''\ 0'';

返回s;

}

- 八月


When writing a function that manipulates data (for example, a string),
is it in principle a good or bad idea to allocate/deallocate memory
within that function? For example, I have the following function:

char * to_bits(int n, char *s, int b) {
int i;

if(b == 0)
b = 32;

if(s != NULL) {

free(s);
}

s = (char *)malloc(sizeof(char) * (b + 1));

for(i = b - 1; i > -1; i--) {

if((0x01 << i) & n) {

s[b - i - 1] = ''1'';
}
else {

s[b - i - 1] = ''0'';
}
}

s[b] = ''\0'';

return s;
}
This function converts an integer into a string representation of its
bits. As you can see, it allocates and deallocates its own memory in
order to return a new string. The alternative would be allocating the
required memory outside of the function, e.g. calling the function as
follows:

s = to_bits(n, s = malloc(TB_SSIZE), int b);

Where TB_SSIZE is the required length of the string.

Is either method good or bad, and why or why not?

解决方案

io*********@hotmail.com wrote:

s = (char *)malloc(sizeof(char) * (b + 1));
s = malloc(b + 1);

The cast is bad.
sizeof(char) is one always.

It would have been simpler for you to have posted a rudimentary plan B,
concerning the return value of malloc,
than to have to explain why it''s missing from the code.

if (s == NULL) {
exit(EXIT_FAILURE);
}
This function converts an integer into a string representation of its
bits. As you can see, it allocates and deallocates its own memory in
order to return a new string.
The alternative would be allocating the
required memory outside of the function, e.g. calling the function as
follows:

s = to_bits(n, s = malloc(TB_SSIZE), int b);

Where TB_SSIZE is the required length of the string.

Is either method good or bad, and why or why not?



I like the second method.
It''s possible that a whole bunch of strings might be output
and that you could use the same buffer for all of them,
that is to say that s could be an automatic array instead.

char s[TB_SSIZE + 1];

http://groups-beta.google.com/group/...87e58d46?hl=en

--
pete


pete wrote:

io*********@hotmail.com wrote:

s = (char *)malloc(sizeof(char) * (b + 1));
s = malloc(b + 1);

The cast is bad.
sizeof(char) is one always.



OK, I thought it was platform dependant.
I like the second method.
It''s possible that a whole bunch of strings might be output
and that you could use the same buffer for all of them,
that is to say that s could be an automatic array instead.

char s[TB_SSIZE + 1];



That''s a good reason, thanks.


io*********@hotmail.com wrote:

When writing a function that manipulates data (for example, a string),
is it in principle a good or bad idea to allocate/deallocate memory
within that function? For example, I have the following function:

char * to_bits(int n, char *s, int b) {
int i;

if(b == 0)
b = 32;

if(s != NULL) {

free(s);
}

s = (char *)malloc(sizeof(char) * (b + 1));

for(i = b - 1; i > -1; i--) {

if((0x01 << i) & n) {

s[b - i - 1] = ''1'';
}
else {

s[b - i - 1] = ''0'';
}
}

s[b] = ''\0'';

return s;
}
This function converts an integer into a string representation of its
bits. As you can see, it allocates and deallocates its own memory in
order to return a new string. The alternative would be allocating the
required memory outside of the function, e.g. calling the function as
follows:

s = to_bits(n, s = malloc(TB_SSIZE), int b);

Where TB_SSIZE is the required length of the string.

Is either method good or bad, and why or why not?



The second approach is more flexible, for instance if the client wants
to use a non-heap allocated array, so my suggestion is:

/* to_bits(n, s, b) returns... where s holds at least b + 1 elements. */

char * to_bits(int n, /*out*/ char *s, int b)
{
int i;

assert((n >= 0) && (b >= 0));
if(b == 0) { b = 32; }
for(i = b - 1; i > -1; i--) {
if((0x01 << i) & n) { s[b - i - 1] = ''1''; }
else { s[b - i - 1] = ''0''; }
}
s[b] = ''\0'';
return s;
}
-- august


这篇关于操纵和记忆分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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