malloc和功能 [英] malloc and functions

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

问题描述

我遇到了malloc和gcc的问题。有什么问题

我的代码还是这个编译器错误?


我正在运行这个程序:


#include< stdio.h>

#include< stdlib.h>


typedef struct pxl {

double lon,lat;

double x,y,z;

double px,py;

}像素;

struct chn {

int index;

struct nde * node;

};


struct nde {

像素位置;

struct chn * forward;

struct chn * reverse;

int * chain_num;

int size;

};


typedef struct chn chain;

typedef struct nde node;


node * createnode();


main()

{


node * startn;


startn = createnode();


startn-> size = 2;

startn-> forward = malloc(2 * sizeof(chain *));

startn-> reverse = malloc(2 * sizeof(chain *));


printf(" sf%p\ nsr%p\\\
\ n",

startn-> ;转发,

开始 - >反转

);


printf(" sf%p \ nsr%p \ n \\ nn,&#;

startn->转发,

startn->反转

);
< br $>
}


节点* createnode()

{

节点*节点;

node = malloc(sizeof(node));

返回节点;

}


我得到以下内容输出。


sf 0x660168

sr 0x660178

sf 0xa383731

sr 0x660178


startn->前进点在两个printfs之间发生变化,尽管

它们之间没有代码。

解决方案

raphfrk< ra ***** @ netscape.netwrites:


>

node * createnode()

{

node * node;

node = malloc(sizeof(node));



只需快速浏览一下,上面的混淆了我。


节点?试试node * pnode ...


不知道是否能解决你的问题。


7月15日,4:09 pm,Richard< rgr ... @ gmail.comwrote:


raphfrk< raph ... @ netscape.netwrites:


node * createnode()

{

node * node;

node = malloc(sizeof(node) );



只需快速浏览一下,上面的混淆了我。


节点?试试node * pnode ...


不知道它是否能解决您的问题。



是的,修正了它。我一直试图弄清楚它多年来

(大约3个小时)。 gcc必须误解我想要的东西。它虽然没有给出警告,但是b $ b没有发出警告。


非常感谢。


< blockquote> 7月15日上午10:57,raphfrk< raph ... @netscape.netwrote:


我遇到了malloc和gcc的问题。有什么问题

我的代码还是这个编译器错误?


我正在运行这个程序:


#include< stdio.h>

#include< stdlib.h>


typedef struct pxl {

double lon,lat;

double x,y,z;

double px,py;

}像素;


struct chn {

int index;

struct nde * node;

};


struct nde {

像素位置;

struct chn * forward;

struct chn * reverse;

int * chain_num;

int size;

};


typedef struct chn chain;

typedef struct nde node;


node * createnode();


main()



''int main(void)''更好。


{


节点* startn;


startn = createnode();


startn-> size = 2;

startn-> forward = malloc(2 * sizeof(chain *));

startn-> reverse = malloc(2 * sizeof(链*) ));


printf(" sf%p\ nsr%p\\\
\ n",

startn-> forward,

startn->反转

);


printf(" sf%p\ nsr%p\ nn \\ n",

startn-> forward,

startn-> reverse

);



%p转换说明符需要一个void *参数,你需要

适当地投射你的参数。


}


node * createnode()

{

node * node;



哎呀。对typedef和变量使用相同的名称(特别是一个

是指向该类型的指针)是丑陋的,并且有可能导致

很大的混乱,请参阅下面。


node = malloc(sizeof(node));



提示:sizeof是否适用于typedef名称或变量名称?

这可能不是你所期望的。 />
您还应该检查malloc的返回值是否失败。


返回节点;

}


我得到以下输出。


sf 0x660168

sr 0x660178


sf 0xa383731

sr 0x660178

startn->前进点在两个printfs之间发生变化,尽管

之间没有代码他们。



修复上述问题,如果您仍有问题,请告诉我们。


Robert Gamble


I am having an issue with malloc and gcc. Is there something wrong
with my code or is this a compiler bug ?

I am running this program:

#include <stdio.h>
#include <stdlib.h>

typedef struct pxl {
double lon, lat;
double x,y,z;
double px,py;
} pixel;
struct chn {
int index;
struct nde *node;
};

struct nde {
pixel position;
struct chn *forward;
struct chn *reverse;
int *chain_num;
int size;
};

typedef struct chn chain;
typedef struct nde node;

node *createnode( );

main()
{

node *startn;

startn = createnode( );

startn->size = 2;
startn->forward = malloc( 2 * sizeof( chain * ) );
startn->reverse = malloc( 2 * sizeof( chain * ) );

printf("sf %p\nsr %p\n\n",
startn->forward,
startn->reverse
);

printf("sf %p\nsr %p\n\n",
startn->forward,
startn->reverse
);

}

node *createnode( )
{
node *node;
node = malloc( sizeof(node) );
return node;
}

I get the following output.

sf 0x660168
sr 0x660178

sf 0xa383731
sr 0x660178

The startn->forward point changes between the two printfs despite
there being no code between them.

解决方案

raphfrk <ra*****@netscape.netwrites:

>
node *createnode( )
{
node *node;
node = malloc( sizeof(node) );

Just a quick glance, the above "confused me".

node? Try "node * pnode" ...

No idea if it will fix your problem.


On Jul 15, 4:09 pm, Richard <rgr...@gmail.comwrote:

raphfrk <raph...@netscape.netwrites:

node *createnode( )
{
node *node;
node = malloc( sizeof(node) );


Just a quick glance, the above "confused me".

node? Try "node * pnode" ...

No idea if it will fix your problem.

Yeah, that fixed it. I have been trying to figure it out for ages
(well about 3 hours). gcc must be misinterpreting what I wanted. It
didn''t give a warning though.

Thanks alot.


On Jul 15, 10:57 am, raphfrk <raph...@netscape.netwrote:

I am having an issue with malloc and gcc. Is there something wrong
with my code or is this a compiler bug ?

I am running this program:

#include <stdio.h>
#include <stdlib.h>

typedef struct pxl {
double lon, lat;
double x,y,z;
double px,py;
} pixel;

struct chn {
int index;
struct nde *node;
};

struct nde {
pixel position;
struct chn *forward;
struct chn *reverse;
int *chain_num;
int size;
};

typedef struct chn chain;
typedef struct nde node;

node *createnode( );

main()

''int main (void)'' is better.

{

node *startn;

startn = createnode( );

startn->size = 2;
startn->forward = malloc( 2 * sizeof( chain * ) );
startn->reverse = malloc( 2 * sizeof( chain * ) );

printf("sf %p\nsr %p\n\n",
startn->forward,
startn->reverse
);

printf("sf %p\nsr %p\n\n",
startn->forward,
startn->reverse
);

The %p conversion specifier expects a void * argument, you need to
cast your arguments appropriately.

}

node *createnode( )
{
node *node;

Yuck. Using the same name for a typedef and a variable (esp. one that
is a pointer to that type) is ugly and has the potential to cause
great confusion, see below.

node = malloc( sizeof(node) );

Hint: Does sizeof apply to the typedef name or the variable name?
It''s probably not what you expect.
You should also check the return value of malloc for failure.

return node;
}

I get the following output.

sf 0x660168
sr 0x660178

sf 0xa383731
sr 0x660178

The startn->forward point changes between the two printfs despite
there being no code between them.

Fix the issues mentioned above and let us know if you still have a
problem.

Robert Gamble


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

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