避免使用malloc? [英] Avoid using malloc?

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

问题描述

当我制作指针时,我已经读过,如果我想在另一个

函数中使用它,我需要先将它malloc,否则它会在
$ b $之后消失b函数返回。在这段代码中我不使用malloc,但它仍然可以工作,并且

函数print_me()打印正确的文本。


为什么我的工作完成尽管我不要使用malloc?


johs

解决方案

Johs32说:

当我创建一个指针时,我已经读过如果我想在另一个函数中使用它,我需要先将它malloc,否则它会在函数返回后消失。在这段代码中




在哪个代码?


-

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

电子邮件:rjh在上面的域名(但显然放弃了www)


Johs32 schrieb:

当我制作指针时,我已经读过,如果我想在另一个
函数中使用它,我需要先将它malloc,否则它会在
函数返回后消失。在这段代码中,我不使用malloc,但它仍然可以工作,并且函数print_me()打印正确的文本。

为什么它可以工作,尽管我不使用malloc?



请提供编译代码,理想情况下是你所指的

情况 - 否则总会有

误解的危险。


示例1:以下是合法的:


void foo(long * bar);

void baz(void);


int main(无效)

{

baz();

返回0;

}


void baz(无效)

{

long qux = 17;

long * quux =& qux;

foo(quux);

}


void foo(长* bar)

{

* bar / = 2;

}


例2:以下是不合法的:


long * foo(void);

void baz(void);


int main(无效)

{

baz();

返回0;

}


void baz(无效)

{

long * quux = foo();

* quux - = 2;

}


void foo(长* bar)

{

长条= 42;

长* qux =& bar ;

返回qux;

}


我没有测试任何一个例子。

即使你处于例2的情况,它完全有可能它的工作原理 - 现在。这可以在另一个操作系统上编译时改变
,或者用b $ b平台或其他编译器,或者甚至只需要在你的代码中添加另一个函数



例3:malloc() - 例2的版本;合法


#include< stdlib.h>


long * foo(无效);

void baz (无效);


int main(无效)

{

baz();

返回0;

}


void baz(无效)

{

long * quux = foo();

if(quux!= NULL){

* quux - = 2;

}

免费(quux);

}


虚假foo(长*酒吧)

{

long * qux = malloc(sizeof * qux);

if(qux!= NULL){

* qux = 42;

}

返回qux;

}

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。


Johs32写道:

当我做了一个我已经读过的指针,如果我想在另一个
函数中使用它,我需要先将它malloc,否则它会在
函数返回后消失。在这段代码中,我不使用malloc,但它仍然可以工作,并且函数print_me()打印正确的文本。

为什么它可以工作,尽管我不使用malloc?



盲目的愚蠢运气是最可能的答案。


如果这是您正在寻找的可靠性水平

for your program,然后继续使用它。


btw:什么代码?


---- ==发表通过Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News == ----
http: //www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- =东海岸和西海岸服务器农场 - 通过加密实现全隐私= ----


When I make a pointer I have read that if I would like to use it in another
function, I need to malloc it first else it will disapear after the
function returns. In this code I do not use malloc, but it still works and
the function print_me() prints the correct text.

Why does it work eventhough I do not use malloc?

johs

解决方案

Johs32 said:

When I make a pointer I have read that if I would like to use it in
another function, I need to malloc it first else it will disapear after
the function returns. In this code



In which code?

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


Johs32 schrieb:

When I make a pointer I have read that if I would like to use it in another
function, I need to malloc it first else it will disapear after the
function returns. In this code I do not use malloc, but it still works and
the function print_me() prints the correct text.

Why does it work eventhough I do not use malloc?



Please provide compiling code, ideally stripped down to the
situation you are referring to -- otherwise there is always
the danger of misunderstandings.

Example 1: The following is legal:

void foo (long *bar);
void baz (void);

int main (void)
{
baz();
return 0;
}

void baz (void)
{
long qux = 17;
long *quux = &qux;
foo(quux);
}

void foo (long *bar)
{
*bar /= 2;
}

Example 2: The following is not legal:

long *foo (void);
void baz (void);

int main (void)
{
baz();
return 0;
}

void baz (void)
{
long *quux = foo();
*quux -= 2;
}

void foo (long *bar)
{
long bar = 42;
long *qux = &bar;
return qux;
}

I did not test either example.
Even if you are in the situation of example 2, it is
perfectly possible that it works -- now. This can
change when compiling it on another operating system,
platform or with another compiler or even when just
adding another function to your code.

Example 3: malloc()-Version of Example 2; legal

#include <stdlib.h>

long *foo (void);
void baz (void);

int main (void)
{
baz();
return 0;
}

void baz (void)
{
long *quux = foo();
if (quux != NULL) {
*quux -= 2;
}
free(quux);
}

void foo (long *bar)
{
long *qux = malloc(sizeof *qux);
if (qux != NULL) {
*qux = 42;
}
return qux;
}
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Johs32 wrote:

When I make a pointer I have read that if I would like to use it in another
function, I need to malloc it first else it will disapear after the
function returns. In this code I do not use malloc, but it still works and
the function print_me() prints the correct text.

Why does it work eventhough I do not use malloc?



Blind stupid luck is the most likely answer.

If that is the level of reliability you are looking
for in your programs, then go ahead and use it.

btw: What code?

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


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

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