Chinaunix

标题: 请教内存分配问题,急,在线等,谢谢大家 [打印本页]

作者: fanzhijie875151    时间: 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 *));
编译通不过,请教高手指点下,谢谢!!
作者: CU黑社会大当家    时间: 2007-12-10 11:03
XImage **replay_ximg;
replay_ximg = (XImage**)malloc(MAX * sizeof(XImage*));
作者: martin4096    时间: 2007-12-10 11:09
你定义的变量XImage *replay_ximg[MAX];这样replay_ximg就是一个两重指针,如果你只是需要MAX_SIZE的XImage,那就定义一重指针就够了,如果非用两重指针,那就对replay_ximg[0]...replay_ximg[19]每一个都分配地址。
作者: fanzhijie875151    时间: 2007-12-10 11:12
还是报错:conflicting type for 'replay_ximg'
             previous declaration of 'replay_ximg'
             initializer element is not constant

是不是结构的问题呢....难道只能给结构中的data分配空间,其他的不行么,大家指点下...谢谢
作者: fanzhijie875151    时间: 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]每一个都分配地址。



有道理....我这样分配确实是不行的....得分开....没有一次性分配的办法么
作者: fanzhijie875151    时间: 2007-12-10 11:43
单个分配也不行啊.....郁闷了...
作者: CU黑社会大当家    时间: 2007-12-10 11:58
你试了我的方法么?
作者: Atlantiscw    时间: 2007-12-10 12:14
你只是要分配空间 ?
只需要这样:
XImage *replay_ximg;

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

GCC 对类型检查很严格
作者: fanzhijie875151    时间: 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


还是报错哦.....
作者: fanzhijie875151    时间: 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


也是这样报错哦.....
作者: CU黑社会大当家    时间: 2007-12-10 12:50
原帖由 fanzhijie875151 于 2007-12-10 12:37 发表


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


还是报错哦.....



你是怎么编译的, 环境是什么? 下面是你的代码,  除去下面一行:
XPointer obdata;            /* hook for the object routines to hang on */
后的, 在我的机器上编译是没问题的.



  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>

  4. typedef struct _XImage {
  5.     int width, height;          /* size of image */
  6.     int xoffset;                /* number of pixels offset in X direction */
  7.     int format;                 /* XYBitmap, XYPixmap, ZPixmap */
  8.     char *data;                 /* pointer to image data */
  9.     int byte_order;             /* data byte order, LSBFirst, MSBFirst */
  10.     int bitmap_unit;            /* quant. of scanline 8, 16, 32 */
  11.     int bitmap_bit_order;       /* LSBFirst, MSBFirst */
  12.     int bitmap_pad;             /* 8, 16, 32 either XY or ZPixmap */
  13.     int depth;                  /* depth of image */
  14.     int bytes_per_line;         /* accelarator to next line */
  15.     int bits_per_pixel;         /* bits per pixel (ZPixmap) */
  16.     unsigned long red_mask;     /* bits in z arrangment */
  17.     unsigned long green_mask;
  18.     unsigned long blue_mask;
  19.     struct funcs {              /* image manipulation routines */
  20.         struct _XImage *(*create_image)();
  21. #if NeedFunctionPrototypes
  22.         int (*destroy_image)        (struct _XImage *);
  23.         unsigned long (*get_pixel)  (struct _XImage *, int, int);
  24.         int (*put_pixel)            (struct _XImage *, int, int, unsigned long);
  25.         struct _XImage *(*sub_image)(struct _XImage *, int, int, unsigned int, unsigned int);
  26.         int (*add_pixel)            (struct _XImage *, long);
  27. #else
  28.         int (*destroy_image)();
  29.         unsigned long (*get_pixel)();
  30.         int (*put_pixel)();
  31.         struct _XImage *(*sub_image)();
  32.         int (*add_pixel)();
  33. #endif
  34.         } f;
  35. } XImage;


  36. int main()
  37. {
  38.         XImage **replay_ximg;
  39.         replay_ximg = (XImage**)malloc(10 * sizeof(XImage*));
  40.         printf("ok\n");
  41.         return 0;
  42. }


复制代码

作者: fanzhijie875151    时间: 2007-12-10 13:03
我环境是红帽9.0   

既然你那里可以通过,难道是其他的问题.....让我在看看....谢谢你啊...
作者: CU黑社会大当家    时间: 2007-12-10 13:13
俺用VC也能够编译通过的...
作者: fanzhijie875151    时间: 2007-12-10 13:25
我把这段程序单独拿出来写成XImage **replay_ximg;
        replay_ximg = (XImage**)malloc(10 * sizeof(XImage*));
也可以通过....
但同样和我其他的源代码放到一起就出现了上面的问题.....很是奇怪
作者: fanzhijie875151    时间: 2007-12-10 13:26
如果我写成XImage *replay_ximg[max]; 的话

那么如何分配内存呢
作者: CU黑社会大当家    时间: 2007-12-10 13:32
原帖由 fanzhijie875151 于 2007-12-10 13:25 发表
我把这段程序单独拿出来写成XImage **replay_ximg;
        replay_ximg = (XImage**)malloc(10 * sizeof(XImage*));
也可以通过....
但同样和我其他的源代码放到一起就出现了上面的问题.....很是奇怪



这个, 你只有再检查一下其它地方了.
作者: fanzhijie875151    时间: 2007-12-10 13:33
把main这样写的话什么错都不报.....同样的和我其他源代码放一起时就有问题了....
int main()
{
        int max;
        XImage **replay_ximg;
        replay_ximg = (XImage**)malloc(max * sizeof(XImage*));
        printf("ok\n");
        return 0;
}
作者: CU黑社会大当家    时间: 2007-12-10 13:38
[quote]原帖由 [i]fanzhijie875151[/i] 于 2007-12-10 13:26 发表 [url=http://linux.chinaunix.net/bbs/redirect.php?goto=findpost&pid=6432832&ptid=916318][img]http://linux.chinaunix.net/bbs/images/common/back.gif[/img][/url]
如果我写成XImage *replay_ximg[max]; 的话

那么如何分配内存呢 [/quote]

此时你这个数组是在栈上的, 空间已经分配了, 你现在要做的就是为每一个元素再分配其具体的空间, 即:
replay_ximg[i] = (XImage*)malloc(sizeof(Ximage));
作者: fanzhijie875151    时间: 2007-12-10 13:49
谢谢啊....我试试
作者: 融化的冰山    时间: 2007-12-10 14:34
conflicting type for 'replay_ximg'
             previous declaration of 'replay_ximg'
             initializer element is not constant


这个错误应该是replay_ximg重复定义了吧,你看一下其他地方是不是定义了一个全局的replay_ximg。
作者: fanzhijie875151    时间: 2007-12-10 14:38
我只定义了一个replay_ximg哦......有没有别的可能呢
作者: longda    时间: 2007-12-10 15:06
你这个代码很像xorg里面的代码

我的思路是

rely_image = (XImage **)malloc(MAX * sizeof(void *));
memset( rely_image, 0, sizeof(void *) * MAX );

for( i = 0; i < MAX; i++ )
{
     rely_image = (XImage *)malloc(sizeof(XImage));
     if( rely_image  == NULL )
     {
         /* ERROR Treatement */
     }

}
作者: fanzhijie875151    时间: 2007-12-10 16:58
我试下........谢谢啊.........
作者: fanzhijie875151    时间: 2007-12-10 17:38
问题解决了.......谢谢大家啊.....
作者: MMMIX    时间: 2007-12-10 17:46
数组名是不可修改的左值,也即不能给数组名赋值。
作者: fanzhijie875151    时间: 2007-12-10 17:51
谢谢啊.................
作者: CU黑社会大当家    时间: 2007-12-10 18:28
原帖由 fanzhijie875151 于 2007-12-10 17:38 发表
问题解决了.......谢谢大家啊.....


怎么解决的?
作者: fanzhijie875151    时间: 2007-12-11 10:15
用了个循环给replay_ximg[0]到replay_ximg[max-1]分别分配空间.....这样就可以了.....
作者: longda    时间: 2007-12-11 13:33
呵呵,还是用我的方法搞定
作者: fanzhijie875151    时间: 2007-12-11 18:31
恩,谢谢了..........
作者: lxy9805287    时间: 2008-01-05 22:23
XImage   *b[MAX];
for(i=0;i<MAX;i++)
    b[i] = (XImage*)malloc(MAX * sizeof(XImage*));




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2