malloc指针返回NULL(求大神指教)

何幕年 2015-03-17 02:42:57
inventor_main.c

int main()
{
Invrec *new_rec;
int n_parts;
int cost,supplier;

n_parts =8;
cost = 88;
supplier =9;

/*new_rec = create_subassy_record(n_parts);
if(new_rec == NULL)
{
printf("error!\n");
return 0;
}
printf("n_parts=%d,%3type=%d",new_rec->info.subassy->n_parts,(int)(new_rec->type));
discard_inventory_record(new_rec);*/

new_rec = create_part_record(cost,supplier);
if(new_rec == NULL)
{
printf("error!\n");
return 0;
}

printf("costs %d dollars the supplier is %d\n",new_rec->info.part->cost,new_rec->info.part->supplier);

discard_inventory_record(new_rec);

return 0;
}



inventor.h

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

typedef struct
{
int cost;
int supplier;
}Partinfo;

typedef struct
{
int n_parts;
struct SUBASSYPART
{
char partno[10];
short quan;
}*part;
}Subassyinfo;

typedef struct
{
char partno[10];
int quan;
enum {PART,SUBASSY} type;
union
{
Partinfo *part;
Subassyinfo *subassy;
}info;
}Invrec;

Invrec *create_subassy_record(int n_parts);
Invrec *create_part_record(int cost,int supplier);
void discard_inventory_record(Invrec *record);





#include "inventor.h"

Invrec *create_subassy_record(int n_parts)
{
Invrec *new_rec;

new_rec = malloc(sizeof(new_rec));
if(new_rec != NULL)
{
new_rec->info.subassy=malloc(sizeof( Subassyinfo ));
if(new_rec->info.subassy != NULL)
{
new_rec->info.subassy->part=malloc(sizeof(struct SUBASSYPART)*n_parts);
if(new_rec->info.subassy->part != NULL)
{
new_rec->type = SUBASSY;
new_rec->info.subassy->n_parts = n_parts;

return new_rec;
}
free(new_rec->info.subassy);
}
free(new_rec);
}
return NULL;
}
Invrec *create_part_record(int cost,int supplier)
{
Invrec *new_rec;

new_rec = malloc(sizeof(new_rec));
if(new_rec != NULL)
{
new_rec->info.part=malloc(sizeof(Partinfo));
if(new_rec->info.part !=NULL)
{
new_rec->type=PART;
new_rec->info.part->cost=cost;
new_rec->info.part->supplier=supplier;

return new_rec;
}
free(new_rec);
}
return NULL;

}

void discard_inventory_record(Invrec *record)
{
if(record->type == SUBASSY)
{
free(record->info.subassy->part);
free(record->info.subassy);
}
else
{
free(record->info.part);
}
free(record);
}



编译运行的时候在Invrec * create_subassy_record(int n_parts)中的
new_rec->info.subassy->part=malloc(sizeof(struct SUBASSYPART)*n_parts);
返回一个NULL指针。
不知道为什么在此处没有分配内存。
...全文
404 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
何幕年 2015-03-20
  • 打赏
  • 举报
回复
引用 6 楼 QIULANZHU 的回复:
sizeof(struct SUBASSYPART)这个结构你在哪里定义了。。 =============================== typedef struct { int n_parts; struct SUBASSYPART { char partno[10]; short quan; }*part; }Subassyinfo;
struct SUBASSYPART{...}*part不是定义了吗?
xionggch 2015-03-20
  • 打赏
  • 举报
回复
需要把 struct SUBASSYPART { char partno[10]; short quan; } 拿出来定义,不能放在结构体里定义,作用域不对
much0726 2015-03-20
  • 打赏
  • 举报
回复
是结构体里面定义一个结构体吗?这个联合结构不能被识别。
何幕年 2015-03-19
  • 打赏
  • 举报
回复
返回一直是0 就是内存没有分配成功
晓石头 2015-03-19
  • 打赏
  • 举报
回复
sizeof(struct SUBASSYPART)这个结构你在哪里定义了。。
===============================
typedef struct
{
int n_parts;
struct SUBASSYPART
{
char partno[10];
short quan;
}*part;
}Subassyinfo;
Philipyexushen 2015-03-19
  • 打赏
  • 举报
回复
引用 3 楼 hlqhayq 的回复:
返回一直是0 就是内存没有分配成功
sizeof(struct SUBASSYPART)这个结构你在哪里定义了。。
晓石头 2015-03-19
  • 打赏
  • 举报
回复
struct SUBASSYPART
{
char partno[10];
short quan;
}*part;


前面是不是少了typedef?
一根烂笔头 2015-03-18
  • 打赏
  • 举报
回复
单步调试+测试输出
此后三年 2015-03-17
  • 打赏
  • 举报
回复
sizeof(struct SUBASSYPART); 这个结构在哪里, 编译器访问不到这个结构体。。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧