与malloc混淆 [英] Confused with malloc

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

问题描述

我很抱歉提出这么愚蠢的问题,但我不能完全理解我的头
围绕malloc。使用gcc我一直用松散的C / C ++混合编程
(我想这实际上是c ++)。但是我已经开始搞乱了计划

9,而那种事情完全没有去那里:)。


分配内存是否正确我的结构?它适用于我的电脑,

但我很怀疑我做错了。


-

struct NODE;


struct NODE {

char string [80];

struct NODE * next;

}


int main()

{

struct NODE * first;


first =(struct NODE)malloc(sizeof(struct NODE));


first-> next =(struct NODE)malloc(sizeof( struct NODE));


免费(第一个);


返回0;

}


是否有必要对结构''首先'进行malloc?

I''m sorry for asking such a silly question, but I can''t quite get my head
around malloc. Using gcc I have always programmed in a lax C/C++ hybrid
(which I suppose is actually c++). But I have started messing around in Plan
9, and that sort of thing is totally no go there :).

Is this correct to allocate memory for my struct? It works on my computer,
but I''m suspicious that I''m doing it wrong.

--

struct NODE;

struct NODE {
char string[80];
struct NODE *next;
}

int main()
{
struct NODE *first;

first = (struct NODE) malloc(sizeof(struct NODE));

first->next = (struct NODE) malloc(sizeof(struct NODE));

free(first);

return 0;
}

Is it necessary to malloc the struct ''first''?

推荐答案

对不起,我看到一个错误代码已经,结构的结尾应该有一个分号


Sorry, I see one error in that code already, there should be a semicolon at
the end of the struct :).


Richard Hunt写道:
Richard Hunt wrote:
我很抱歉提出这么愚蠢的问题,但我不能完全理解马克思的问题。使用gcc我一直用松散的C / C ++混合编程(我想这实际上是c ++)。但是我已经开始乱搞Plan
9了,那种事情完全没有去那里:)。

为我的结构分配内存是否正确?它适用于我的电脑,
但我很怀疑我做错了。


不,这不正确。它甚至不应该编译。以下是我得到的

诊断:


" C:\ cygwin \ bin \ gcc.exe" -W -Wall -Wcast-align -Wwrite-strings

-Wno-sign-compare -Wno-unused-parameter -Wpointer-arith -ansi

-pedantic-errors -ggdb -c fun.c

fun.c:9:错误:'main''声明中的两个或多个数据类型

fun.c:在函数中` main'':

fun.c:12:警告:隐式声明函数`malloc''

fun.c:12:错误:转换为非标量类型请求

fun.c:14:错误:转换为非标量类型请求

fun.c:16:警告:隐式声明函数`free''

终止,退出代码为1

struct NODE;


这个前向声明没用。

struct NODE {
char string [80];
struct NODE * next; < br $>


fun.c:9:错误:声明'main'的两种或多种数据类型


你错过了这里有一个分号。

int main()


首选''int main(void)''。 "()"和(无效)意思是完全不同

C中的东西。

{
struct NODE * first;

first =(struct NODE)malloc(sizeof (struct NODE));


fun.c:12:警告:隐式声明函数`malloc''

malloc尚未声明,因此使用它会导致它是隐含的

声明返回''int''。这是错误的,导致行为是未定义的。

fun.c:12:错误:请求转换为非标量类型

你也投了错误的类型。


你可能不应该从malloc投出回报。它没有做任何有用的事情,使维护更加困难,并且可以隐藏错误。

如果你已经转换为正确的类型,并且编译器没有打扰

警告有关malloc的隐式声明(不需要,

,有些不要),那么你会有一个无声的情况
最后,考虑使用comp.lang.c批准的malloc习语:


p = malloc(N * sizeof( * p));


这比其他成语更容易出错,更自我维护。

first-> next =(struct NODE) malloc(sizeof(struct NODE));


如果第一个malloc失败,所有问题都与以前一样,加上未定义的行为(可能是导致崩溃的
)。

自由(第一);


内存泄漏。你从来没有放过第一个>接下来,现在你无法这样做。

返回0;
}

是否有必要对结构进行malloc ''first''?
I''m sorry for asking such a silly question, but I can''t quite get my head
around malloc. Using gcc I have always programmed in a lax C/C++ hybrid
(which I suppose is actually c++). But I have started messing around in Plan
9, and that sort of thing is totally no go there :).

Is this correct to allocate memory for my struct? It works on my computer,
but I''m suspicious that I''m doing it wrong.
No, it''s not correct. It shouldn''t even compile. Here are the
diagnostics I got:

"C:\cygwin\bin\gcc.exe" -W -Wall -Wcast-align -Wwrite-strings
-Wno-sign-compare -Wno-unused-parameter -Wpointer-arith -ansi
-pedantic-errors -ggdb -c fun.c
fun.c:9: error: two or more data types in declaration of `main''
fun.c: In function `main'':
fun.c:12: warning: implicit declaration of function `malloc''
fun.c:12: error: conversion to non-scalar type requested
fun.c:14: error: conversion to non-scalar type requested
fun.c:16: warning: implicit declaration of function `free''
Terminated with exit code 1

struct NODE;
This forward declaration is not useful.

struct NODE {
char string[80];
struct NODE *next;
}
fun.c:9: error: two or more data types in declaration of `main''

You missed a semicolon here.

int main()
Prefer ''int main(void)''. "()" and "(void)" mean completely different
things in C.
{
struct NODE *first;

first = (struct NODE) malloc(sizeof(struct NODE));
fun.c:12: warning: implicit declaration of function `malloc''

malloc has not been declared, so using it causes it to be implicitly
declared to return ''int''. This is wrong, and causes the behavior to be
undefined.
fun.c:12: error: conversion to non-scalar type requested

You''ve also cast to the wrong type.

You should probably not cast the return from malloc. It doesn''t do
anything useful, makes maintenance more difficult, and can hide errors.
If you had cast to the correct type, and the compiler had not bothered
to warn about the implicit declaration of malloc (it''s not required to,
and some don''t), then you''d have a silent case of undefined behavior.

Finally, consider using the comp.lang.c-approved malloc idiom:

p = malloc(N * sizeof(*p));

This is less error-prone and more self-maintaining than other idioms.

first->next = (struct NODE) malloc(sizeof(struct NODE));
All the same problems as before, plus undefined behavior (probably
causing a crash) if the first malloc fails.

free(first);
Memory leak. You never freed first->next, and now you have no way to do so.

return 0;
}

Is it necessary to malloc the struct ''first''?




我不确定我知道你的意思。 ''first''是指针,而不是结构。

如果你希望它有用,必须指向某个东西。一个

malloced对象是一种可能的选择。


在这个例子的上下文中,你根本不需要malloc。您可以

完成此操作:


struct NODE优先,下一个;

first.next = next;


-Kevin

-

我的电子邮件地址有效,但会定期更改。

请联系我使用最近发布的地址。



I''m not sure I know what you mean. ''first'' is a pointer, not a struct.
It has to be made to point to something if you want it to be useful. A
malloced object is one possible choice.

In the context of this example, you didn''t need malloc at all. You could
have done this:

struct NODE first, next;
first.next = next;

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


On Sun,28 Dec 2003 18:28:52 -0000,Richard Hunt < 01 **** @ fg.dfg>

写道:
On Sun, 28 Dec 2003 18:28:52 -0000, "Richard Hunt" <01****@fg.dfg>
wrote:
我很抱歉问这么愚蠢的问题,但我可以'在malloc附近,我的头脑很清楚。使用gcc我一直用松散的C / C ++混合编程(我想这实际上是c ++)。但是我已经开始乱搞Plan
9了,那种事情完全没有去那里:)。

为我的结构分配内存是否正确?它适用于我的电脑,
但我很怀疑我做错了。

-


你不应该'不要使用这个分隔符。许多新闻读者将以下所有内容视为签名材料。

struct NODE;

struct NODE {
char string [80];
struct NODE * next;
}


;

int main()

struct NODE *首先;

first =(struct NODE)malloc(sizeof(struct NODE));


这是违反约束的行为。你可能意味着演员阵容是

(struct NODE *)。


在C中你永远不应该从malloc转换回报。您还应该使用
#include stdlib.h来确保原型在范围内。首选的是
构造是


first = malloc(sizeof * first)

first-> next =(struct NODE )malloc(sizeof(struct NODE));


你应该在提交之前编译你的代码。

免费(第一);


您是否意识到这会造成内存泄漏?

返回0;
}

是否有必要malloc结构''第一''?
I''m sorry for asking such a silly question, but I can''t quite get my head
around malloc. Using gcc I have always programmed in a lax C/C++ hybrid
(which I suppose is actually c++). But I have started messing around in Plan
9, and that sort of thing is totally no go there :).

Is this correct to allocate memory for my struct? It works on my computer,
but I''m suspicious that I''m doing it wrong.

--
You shouldn''t use this separator. Many news readers treat everything
below as signature material.

struct NODE;

struct NODE {
char string[80];
struct NODE *next;
}
;

int main()
{
struct NODE *first;

first = (struct NODE) malloc(sizeof(struct NODE));
This is a constraint violation. You probably meant the cast to be
(struct NODE*).

In C you should never cast the return from malloc. You should also
#include stdlib.h to insure a prototype is in scope. The preferred C
construct is

first = malloc(sizeof *first)

first->next = (struct NODE) malloc(sizeof(struct NODE));
You ought to compile your code before submitting it.

free(first);
You do realize that this creates a memory leak?

return 0;
}

Is it necessary to malloc the struct ''first''?




如果你不是,首先 - >接下来甚至存在吗?

< ;<删除电子邮件的del>>



If you don''t, will first->next even exist?
<<Remove the del for email>>


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

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