如何分配内存? [英] How to allocate memory?

查看:206
本文介绍了如何分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建我的节目一个链表,我使用malloc我不能分配内存的结构指针()。如何在GCC分配内存变量?示例程序如下。如何使它在海湾合作委员会的工作?

 #包括LT&;&stdio.h中GT;
#包括LT&;&alloc.h GT;
结构节点
{    int数据;
    结构节点*链接;};
无效插入(结构节点* P,INT D)
{
    结构节点*温度;
    TEMP =的malloc(sizeof的(结构节点));
        TEMP-GT&;数据= D;
    TEMP-GT&;链接= NULL;        如果(P == NULL)
        {
            P =温度;
        }
        其他{
        而(对GT;!链接= NULL)
            P = P->链接;    P->链接=温度;
        }
}
无效DISP(结构节点* P)
{
    而(P!= NULL)
        {
        的printf(%d个\\ N,P->数据);
        P = P->链接;
        }
}诠释的main()
{结构节点* P;
p值= NULL;
插入(对,7);
插入(对,9);
DISP(P);}

我遇到的错误是:

 第18行:错误:alloc.h:没有这样的文件或目录
在功能'插入':
第13行:警告:内建函数'的malloc'不兼容的隐式声明


解决方案

的malloc <文件stdlib.h> 。包括

阅读该功能的手册页就会给你相关信息。这不是编译器相关。

I am trying to create a linked list in my program and I am not able to allocate memory to the structure pointer using malloc(). How do I allocate memory to variables in GCC? The sample program is given below. How to make it work in gcc?

#include<stdio.h>
#include <alloc.h>
struct node
{

    int data;
    struct node * link;

};
void insert (struct node *p, int d)
{
    struct node *temp;
    temp = malloc(sizeof(struct node));
        temp->data=d;
    temp->link=NULL;

        if(p==NULL)
        {
            p=temp;
        }
        else{
        while(p->link!=NULL)
            p=p->link;

    p->link=temp;
        }
}
void disp(struct node *p)
{
    while(p!=NULL)
        {
        printf("%d\n",p->data);
        p=p->link;
        }


}

int main()
{

struct node *p;
p=NULL;
insert(p,7);
insert(p,9);
disp(p);

}

The error I'm encountering is:

Line 18: error: alloc.h: No such file or directory
In function 'insert':
Line 13: warning: incompatible implicit declaration of built-in function 'malloc'

解决方案

malloc is in <stdlib.h>. Include that.

Reading the man page for that function would have given you that information. It's not compiler-dependent.

这篇关于如何分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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