请教如何为C语言的结构体数组分配/释放内存?谢谢

startstop2015 2015-09-08 03:33:26
请教如何为C语言的结构体数组分配/释放内存?谢谢
struct emp
{
char name[10];
float salary;
};


int main(int argc,char* argv[])
{
struct emp array_emp[1000000];
...全文
2769 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_24853447 2016-08-10
  • 打赏
  • 举报
回复
如果结构体中有一个char类型的指针,请问怎么释放这个结构体中的内存,需要保证内存不被泄漏
初見的畫面 2015-09-23
  • 打赏
  • 举报
回复
引用 19 楼 startstop2015 的回复:
[quote=引用 18 楼 u013470052 的回复:] 大神我这样定义似乎可以了,这样定义,我没法根据表的记录数来分配内存了,必须写死记录数了? struct emp { char name[1000000][10]; float salary[1000000]; }; 但是和如下定义有什么区别马? struct emp_record { char name[10]; float salary; };
个人感觉本质上来说没区别,但是一般不选择上面的,不方便。[/quote] 下面的不能用,除非是静态变量[/quote] 为什么:???
苏叔叔 2015-09-10
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct emp {
    char name[10];
    float salary;
};
int main() {
    struct emp *array_emp;

    array_emp=malloc(1000000*sizeof(struct emp));
    if (NULL==array_emp) {
        printf("Can not malloc 1000000 emp!\n");
        return 1;
    }
    strcpy(array_emp[999999].name,"123456789");
    array_emp[999999].salary=50000.0f;
    free(array_emp);
    return 0;
}
看到5w的salary,其它的就不想看了,人生的差距最大不过如此……
赵4老师 2015-09-10
  • 打赏
  • 举报
回复
吹牛不犯法,吹牛不用上税,你吹我吹大家吹!
苏叔叔 2015-09-10
  • 打赏
  • 举报
回复
引用 21 楼 zhao4zhong1 的回复:
[quote=引用 20 楼 zhangxiangDavaid 的回复:] [quote=引用 5 楼 zhao4zhong1 的回复:]
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct emp {
    char name[10];
    float salary;
};
int main() {
    struct emp *array_emp;

    array_emp=malloc(1000000*sizeof(struct emp));
    if (NULL==array_emp) {
        printf("Can not malloc 1000000 emp!\n");
        return 1;
    }
    strcpy(array_emp[999999].name,"123456789");
    array_emp[999999].salary=50000.0f;
    free(array_emp);
    return 0;
}
看到5w的salary,其它的就不想看了,人生的差距最大不过如此……[/quote] 现在人不都和余佳文一样嘛![/quote] 他说分一亿,我说:给我5w就行!
赵4老师 2015-09-10
  • 打赏
  • 举报
回复
引用 20 楼 zhangxiangDavaid 的回复:
[quote=引用 5 楼 zhao4zhong1 的回复:]
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct emp {
    char name[10];
    float salary;
};
int main() {
    struct emp *array_emp;

    array_emp=malloc(1000000*sizeof(struct emp));
    if (NULL==array_emp) {
        printf("Can not malloc 1000000 emp!\n");
        return 1;
    }
    strcpy(array_emp[999999].name,"123456789");
    array_emp[999999].salary=50000.0f;
    free(array_emp);
    return 0;
}
看到5w的salary,其它的就不想看了,人生的差距最大不过如此……[/quote] 现在人不都和余佳文一样嘛!
startstop2015 2015-09-09
  • 打赏
  • 举报
回复
引用 18 楼 u013470052 的回复:
大神我这样定义似乎可以了,这样定义,我没法根据表的记录数来分配内存了,必须写死记录数了? struct emp { char name[1000000][10]; float salary[1000000]; }; 但是和如下定义有什么区别马? struct emp_record { char name[10]; float salary; };
个人感觉本质上来说没区别,但是一般不选择上面的,不方便。[/quote] 下面的不能用,除非是静态变量
初見的畫面 2015-09-08
  • 打赏
  • 举报
回复
大神我这样定义似乎可以了,这样定义,我没法根据表的记录数来分配内存了,必须写死记录数了? struct emp { char name[1000000][10]; float salary[1000000]; }; 但是和如下定义有什么区别马? struct emp_record { char name[10]; float salary; }; [/quote] 个人感觉本质上来说没区别,但是一般不选择上面的,不方便。
startstop2015 2015-09-08
  • 打赏
  • 举报
回复
引用 11 楼 zhao4zhong1 的回复:
每弄过→没弄过
大神我这样定义似乎可以了,这样定义,我没法根据表的记录数来分配内存了,必须写死记录数了? struct emp { char name[1000000][10]; float salary[1000000]; }; 但是和如下定义有什么区别马? struct emp_record { char name[10]; float salary; };
startstop2015 2015-09-08
  • 打赏
  • 举报
回复
引用 15 楼 u014749886 的回复:
[quote=引用 14 楼 startstop2015 的回复:] [quote=引用 11 楼 zhao4zhong1 的回复:] 每弄过→没弄过
static struct emp { char name[10]; float salary; }; static struct emp array_emp[1000000]; 大神,如果这样定义,是可以SELECT INTO :array_emp 100万条记录的 但是您的定义方法不行,没法SELECT INTO 100万行,只能INTO 1条记录 [/quote]是因为只有1条数据的缘故吧[/quote] 有100多万行记录
Mortred_cp 2015-09-08
  • 打赏
  • 举报
回复
引用 14 楼 startstop2015 的回复:
[quote=引用 11 楼 zhao4zhong1 的回复:] 每弄过→没弄过
static struct emp { char name[10]; float salary; }; static struct emp array_emp[1000000]; 大神,如果这样定义,是可以SELECT INTO :array_emp 100万条记录的 但是您的定义方法不行,没法SELECT INTO 100万行,只能INTO 1条记录 [/quote]是因为只有1条数据的缘故吧
startstop2015 2015-09-08
  • 打赏
  • 举报
回复
引用 11 楼 zhao4zhong1 的回复:
每弄过→没弄过
static struct emp { char name[10]; float salary; }; static struct emp array_emp[1000000]; 大神,如果这样定义,是可以SELECT INTO :array_emp 100万条记录的 但是您的定义方法不行,没法SELECT INTO 100万行,只能INTO 1条记录
pricks 2015-09-08
  • 打赏
  • 举报
回复
来看看11楼的回答
xian_wwq 2015-09-08
  • 打赏
  • 举报
回复

struct Point{
int x;
int y;
}

Point* pt;
pt = (Point*)malloc(10 * sizeof(Point));
//do sth
free(pt);
然后就可以用下标来操作了 pt[0].x = 12;
赵4老师 2015-09-08
  • 打赏
  • 举报
回复
每弄过→没弄过
赵4老师 2015-09-08
  • 打赏
  • 举报
回复
也许PRO*C和标准C不同。每弄过PRO*C,爱莫能助。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct emp {
    char name[10];
    float salary;
};
int main() {
    struct emp *array_emp;
    int i;

    array_emp=(struct emp *)malloc(1000000*sizeof(struct emp));
    if (NULL==array_emp) {
        printf("Can not malloc 1000000 emp!\n");
        return 1;
    }
    for (i=0;i<1000000;i+=10000) {
        sprintf(array_emp[i].name,"%d",i);
        array_emp[i].salary=(float)i;
    }
    for (i=0;i<1000000;i+=10000) {
        printf("%d %s %f\n",i,array_emp[i].name,array_emp[i].salary);
    }
    free(array_emp);
    return 0;
}
//0 0 0.000000
//10000 10000 10000.000000
//20000 20000 20000.000000
//30000 30000 30000.000000
//40000 40000 40000.000000
//50000 50000 50000.000000
//60000 60000 60000.000000
//70000 70000 70000.000000
//80000 80000 80000.000000
//90000 90000 90000.000000
//100000 100000 100000.000000
//110000 110000 110000.000000
//120000 120000 120000.000000
//130000 130000 130000.000000
//140000 140000 140000.000000
//150000 150000 150000.000000
//160000 160000 160000.000000
//170000 170000 170000.000000
//180000 180000 180000.000000
//190000 190000 190000.000000
//200000 200000 200000.000000
//210000 210000 210000.000000
//220000 220000 220000.000000
//230000 230000 230000.000000
//240000 240000 240000.000000
//250000 250000 250000.000000
//260000 260000 260000.000000
//270000 270000 270000.000000
//280000 280000 280000.000000
//290000 290000 290000.000000
//300000 300000 300000.000000
//310000 310000 310000.000000
//320000 320000 320000.000000
//330000 330000 330000.000000
//340000 340000 340000.000000
//350000 350000 350000.000000
//360000 360000 360000.000000
//370000 370000 370000.000000
//380000 380000 380000.000000
//390000 390000 390000.000000
//400000 400000 400000.000000
//410000 410000 410000.000000
//420000 420000 420000.000000
//430000 430000 430000.000000
//440000 440000 440000.000000
//450000 450000 450000.000000
//460000 460000 460000.000000
//470000 470000 470000.000000
//480000 480000 480000.000000
//490000 490000 490000.000000
//500000 500000 500000.000000
//510000 510000 510000.000000
//520000 520000 520000.000000
//530000 530000 530000.000000
//540000 540000 540000.000000
//550000 550000 550000.000000
//560000 560000 560000.000000
//570000 570000 570000.000000
//580000 580000 580000.000000
//590000 590000 590000.000000
//600000 600000 600000.000000
//610000 610000 610000.000000
//620000 620000 620000.000000
//630000 630000 630000.000000
//640000 640000 640000.000000
//650000 650000 650000.000000
//660000 660000 660000.000000
//670000 670000 670000.000000
//680000 680000 680000.000000
//690000 690000 690000.000000
//700000 700000 700000.000000
//710000 710000 710000.000000
//720000 720000 720000.000000
//730000 730000 730000.000000
//740000 740000 740000.000000
//750000 750000 750000.000000
//760000 760000 760000.000000
//770000 770000 770000.000000
//780000 780000 780000.000000
//790000 790000 790000.000000
//800000 800000 800000.000000
//810000 810000 810000.000000
//820000 820000 820000.000000
//830000 830000 830000.000000
//840000 840000 840000.000000
//850000 850000 850000.000000
//860000 860000 860000.000000
//870000 870000 870000.000000
//880000 880000 880000.000000
//890000 890000 890000.000000
//900000 900000 900000.000000
//910000 910000 910000.000000
//920000 920000 920000.000000
//930000 930000 930000.000000
//940000 940000 940000.000000
//950000 950000 950000.000000
//960000 960000 960000.000000
//970000 970000 970000.000000
//980000 980000 980000.000000
//990000 990000 990000.000000
//
startstop2015 2015-09-08
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
第12行array_emp=malloc(1000000*sizeof(struct emp)); 应改为 array_emp=(struct emp *)malloc(1000000*sizeof(struct emp));
大神,按照您说的,这样我只保存了一条记录到这个结构体数组 EXEC SQL Select name, salary into :array_emp from t where rownum<=1000000;
startstop2015 2015-09-08
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct emp {
    char name[10];
    float salary;
};
int main() {
    struct emp *array_emp;

    array_emp=malloc(1000000*sizeof(struct emp));
    if (NULL==array_emp) {
        printf("Can not malloc 1000000 emp!\n");
        return 1;
    }
    strcpy(array_emp[999999].name,"123456789");
    array_emp[999999].salary=50000.0f;
    free(array_emp);
    return 0;
}
大神,我用的是PRO*C,这样写,保存到结构体数组里的记录只有1条,不是100万条
赵4老师 2015-09-08
  • 打赏
  • 举报
回复
第12行array_emp=malloc(1000000*sizeof(struct emp)); 应改为 array_emp=(struct emp *)malloc(1000000*sizeof(struct emp));
startstop2015 2015-09-08
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct emp {
    char name[10];
    float salary;
};
int main() {
    struct emp *array_emp;

    array_emp=malloc(1000000*sizeof(struct emp));
    if (NULL==array_emp) {
        printf("Can not malloc 1000000 emp!\n");
        return 1;
    }
    strcpy(array_emp[999999].name,"123456789");
    array_emp[999999].salary=50000.0f;
    free(array_emp);
    return 0;
}
谢谢大神,这样就算是定义了这个数组的元素个数了?
加载更多回复(5)

69,369

社区成员

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

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