免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 7928 | 回复: 30
打印 上一主题 下一主题

请教内存分配问题,急,在线等,谢谢大家 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-12-10 10:15 |只看该作者 |倒序浏览
typedef struct _XImage {
    int width, height;          /* size of image */
    int xoffset;                /* number of pixels offset in X direction */
    int format;                 /* XYBitmap, XYPixmap, ZPixmap */
    char *data;                 /* pointer to image data */
    int byte_order;             /* data byte order, LSBFirst, MSBFirst */
    int bitmap_unit;            /* quant. of scanline 8, 16, 32 */
    int bitmap_bit_order;       /* LSBFirst, MSBFirst */
    int bitmap_pad;             /* 8, 16, 32 either XY or ZPixmap */
    int depth;                  /* depth of image */
    int bytes_per_line;         /* accelarator to next line */
    int bits_per_pixel;         /* bits per pixel (ZPixmap) */
    unsigned long red_mask;     /* bits in z arrangment */
    unsigned long green_mask;
    unsigned long blue_mask;
    XPointer obdata;            /* hook for the object routines to hang on */
    struct funcs {              /* image manipulation routines */
        struct _XImage *(*create_image)();
#if NeedFunctionPrototypes
        int (*destroy_image)        (struct _XImage *);
        unsigned long (*get_pixel)  (struct _XImage *, int, int);
        int (*put_pixel)            (struct _XImage *, int, int, unsigned long);
        struct _XImage *(*sub_image)(struct _XImage *, int, int, unsigned int, unsigned int);
        int (*add_pixel)            (struct _XImage *, long);
#else
        int (*destroy_image)();
        unsigned long (*get_pixel)();
        int (*put_pixel)();
        struct _XImage *(*sub_image)();
        int (*add_pixel)();
#endif
        } f;
} XImage;


上面是一副图像的结构,我现在定义了 XImage *replay_ximg[MAX];
想对它分配内存空间,我是这样做的:  replay_ximg=malloc(MAX * sizeof(XImage *));
编译通不过,请教高手指点下,谢谢!!

论坛徽章:
0
2 [报告]
发表于 2007-12-10 11:03 |只看该作者
XImage **replay_ximg;
replay_ximg = (XImage**)malloc(MAX * sizeof(XImage*));

论坛徽章:
0
3 [报告]
发表于 2007-12-10 11:09 |只看该作者
你定义的变量XImage *replay_ximg[MAX];这样replay_ximg就是一个两重指针,如果你只是需要MAX_SIZE的XImage,那就定义一重指针就够了,如果非用两重指针,那就对replay_ximg[0]...replay_ximg[19]每一个都分配地址。

论坛徽章:
0
4 [报告]
发表于 2007-12-10 11:12 |只看该作者
还是报错:conflicting type for 'replay_ximg'
             previous declaration of 'replay_ximg'
             initializer element is not constant

是不是结构的问题呢....难道只能给结构中的data分配空间,其他的不行么,大家指点下...谢谢

论坛徽章:
0
5 [报告]
发表于 2007-12-10 11:14 |只看该作者
原帖由 martin4096 于 2007-12-10 11:09 发表
你定义的变量XImage *replay_ximg[MAX];这样replay_ximg就是一个两重指针,如果你只是需要MAX_SIZE的XImage,那就定义一重指针就够了,如果非用两重指针,那就对replay_ximg[0]...replay_ximg[19]每一个都分配地址。



有道理....我这样分配确实是不行的....得分开....没有一次性分配的办法么

论坛徽章:
0
6 [报告]
发表于 2007-12-10 11:43 |只看该作者
单个分配也不行啊.....郁闷了...

论坛徽章:
0
7 [报告]
发表于 2007-12-10 11:58 |只看该作者
你试了我的方法么?

论坛徽章:
0
8 [报告]
发表于 2007-12-10 12:14 |只看该作者
你只是要分配空间 ?
只需要这样:
XImage *replay_ximg;

replay_ximg = (XImage *)calloc(MAX, sizeof(XImage));

GCC 对类型检查很严格

论坛徽章:
0
9 [报告]
发表于 2007-12-10 12:37 |只看该作者
原帖由 CU黑社会大当家 于 2007-12-10 11:58 发表
你试了我的方法么?


             conflicting type for 'replay_ximg'
             previous declaration of 'replay_ximg'
             initializer element is not constant


还是报错哦.....

论坛徽章:
0
10 [报告]
发表于 2007-12-10 12:39 |只看该作者
原帖由 Atlantiscw 于 2007-12-10 12:14 发表
你只是要分配空间 ?
只需要这样:
XImage *replay_ximg;

replay_ximg = (XImage *)calloc(MAX, sizeof(XImage));

GCC 对类型检查很严格



             conflicting type for 'replay_ximg'
             previous declaration of 'replay_ximg'
             initializer element is not constant


也是这样报错哦.....
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP