怀疑char *和char [] [英] Doubt with char* and char[]

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

问题描述

您好!


我会写一个片段来说明我的疑问:


摘录1:

void foo(int l)

{

char * tmp;

tmp =(char *)malloc(l * sizeof(char)) ;


//玩得开心


免费(tmp);


}


摘录2:

void foo(int l)

{

char tmp [l];


//做什么


}


如果我使用片段有什么区别? 1或2?


提前致谢!

Hello!

I will write a snippet to illustrate my doubt:

snippet 1:
void foo( int l )
{
char* tmp;
tmp = (char*) malloc( l * sizeof( char ) );

// DO SOMETHING

free( tmp );

}

snippet 2:
void foo( int l )
{
char tmp[l];

// DO SOMETHING

}

What is the difference in advantage if I use snippet 1 or 2?

Thanks in advance!

推荐答案

" Kamui Shirow" < su ******* @ gmail.comschrieb im Newsbeitrag

news:11 ********************** @ q69g2000hsb.googlegr oups.com ...
"Kamui Shirow" <su*******@gmail.comschrieb im Newsbeitrag
news:11**********************@q69g2000hsb.googlegr oups.com...

你好!


我会写一个片段来说明我的疑问:


片段1:

void foo(int l)

{

char * tmp;

tmp =(char *)malloc(l * sizeof(char));
Hello!

I will write a snippet to illustrate my doubt:

snippet 1:
void foo( int l )
{
char* tmp;
tmp = (char*) malloc( l * sizeof( char ) );



放弃演员并添加#include s< tdlib.h>。 sizeof(char)也是1,

定义

你还需要检查malloc的返回值来检测失败。

drop the cast and add #include s<tdlib.h>. Also sizeof (char) is 1 by
definition
Also you''d need to check malloc''s return value to detect failure.


//做什么


免费(tmp);


}

片段2:

void foo(int l)

{

char tmp [l];
< br $>
//做什么


}


如果我使用1或2代码片段,有什么区别?
// DO SOMETHING

free( tmp );

}

snippet 2:
void foo( int l )
{
char tmp[l];

// DO SOMETHING

}

What is the difference in advantage if I use snippet 1 or 2?



小部件2需要更少的打字,你不会忘记打电话给

free(),所以它更少容易发生内存泄漏。


再见,Jojo

Snippet 2 requires far less typing and you can''t accidently forget to call
free(), so it''s less prone to memory leaks.

Bye, Jojo


Kamui Shirow< su ******* @ gmail。 comwrites:
Kamui Shirow <su*******@gmail.comwrites:

snippet 1:

void foo(int l)

{

char * tmp;

tmp =(char *)malloc(l * sizeof(char));
snippet 1:
void foo( int l )
{
char* tmp;
tmp = (char*) malloc( l * sizeof( char ) );



sizeof(char)总是1,所以没必要乘以它。


我不喜欢建议不要转换malloc()的返回值:


* ANSI C中不需要强制转换。


*转换它的回报值可以掩盖#include

< stdlib.h>的失败,这会导致未定义的行为。


*如果你转换为错误的类型事故,奇怪的失败可以

结果。


在特殊情况下,投出返回值

malloc可能是有意义的( )。例如,PJ Plauger有充分的理由想要将他的

代码编译为C和C ++,并且C ++需要演员,因为他在文章中解释了&b
。 9s******************@nwrddc01.gnilink.net>。

然而,Plauger的情况确实很少见。大多数程序员应该将代码编写为C或C ++,而不是两者的交集。

sizeof(char) is always 1, so it''s unnecessary to multiply by it.

I don''t recommend casting the return value of malloc():

* The cast is not required in ANSI C.

* Casting its return value can mask a failure to #include
<stdlib.h>, which leads to undefined behavior.

* If you cast to the wrong type by accident, odd failures can
result.

In unusual circumstances it may make sense to cast the return value of
malloc(). P. J. Plauger, for example, has good reasons to want his
code to compile as both C and C++, and C++ requires the cast, as he
explained in article <9s*****************@nwrddc01.gnilink.net>.
However, Plauger''s case is rare indeed. Most programmers should write
their code as either C or C++, not in the intersection of the two.


//做什么


免费(tmp);


}


片段2:

void foo(int l)

{

char tmp [l];


// DO SOMETHING


}


如果我使用代码片段1或2,有什么区别?
// DO SOMETHING

free( tmp );

}

snippet 2:
void foo( int l )
{
char tmp[l];

// DO SOMETHING

}

What is the difference in advantage if I use snippet 1 or 2?



如果无法分配tmp的内存

,则代码片段为代码提供恢复的机会。在代码段2中,程序产生了未定义的行为,并且如果内存不可用,可能会崩溃。


Snippet 1有机会延长数组的生命周期<超出函数返回的
。另一方面,在代码片段2

程序员不能忘记免费拨打电话。


许多编译器还没有实现可变长度数组
来自C99的
支持需要代码片段2才能编译。


我会使用代码片段1的形式,除非我知道我的代码会

永远不会移植到C99之前的编译器。实际上,这意味着我的代码很少使用可变长度数组。

-

Ben Pfaff
< a rel =nofollowhref =http://benpfaff.orgtarget =_ blank> http://benpfaff.org


Kamui Shirow说:
Kamui Shirow said:

您好!


我会写一个片段来说明我的疑问:

片段1:

void foo(int l)

{

char * tmp;

tmp =(char *)malloc(l * sizeof(char));


// DO SOMETHING


free(tmp);


}
Hello!

I will write a snippet to illustrate my doubt:

snippet 1:
void foo( int l )
{
char* tmp;
tmp = (char*) malloc( l * sizeof( char ) );

// DO SOMETHING

free( tmp );

}



更好:


#include< stdlib.h> ;


void foo(size_t n)

{

char * tmp = malloc(n * sizeof * tmp);

if(tmp!= NULL)

{

/ *做点什么* /

free(tmp);

}

}

Better:

#include <stdlib.h>

void foo(size_t n)
{
char *tmp = malloc(n * sizeof *tmp);
if(tmp != NULL)
{
/* do something */
free(tmp);
}
}


snippet 2:

void foo(int l)

{

char tmp [l];


// DO SOMETHING < br $>

}


如果我使用代码片段1或2,有什么区别?
snippet 2:
void foo( int l )
{
char tmp[l];

// DO SOMETHING

}

What is the difference in advantage if I use snippet 1 or 2?



Snippet 1不需要C99,让你可以控制失败。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

Snippet 1 doesn''t require C99, and gives you control over failure.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


这篇关于怀疑char *和char []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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