讨教怎么样为C语言的结构体数组分配/释放内存?谢谢
struct emp
{
char name[10];
float salary;
};
int main(int argc,char* argv[])
{
struct emp array_emp[1000000];
struct emp
{
char name[10];
float salary;
};
int main(int argc,char* argv[])
{
struct emp array_emp[1000000];
解决方案
2
http://blog.csdn.net/q345852047/article/details/7348038
30
#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; }
1
第12行array_emp=malloc(1000000*sizeof(struct emp));
应改为
array_emp=(struct emp *)malloc(1000000*sizeof(struct emp));
应改为
array_emp=(struct emp *)malloc(1000000*sizeof(struct emp));
1
也许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 //
1
每弄过→没弄过
1
struct Point{ int x; int y; } Point* pt; pt = (Point*)malloc(10 * sizeof(Point)); //do sth free(pt);
然后就可以用下标来操作了
pt[0].x = 12;
1
来看看11楼的回答
1
是原因是只有1条数据的缘故吧
1
高手本人这样定义似乎可以了,这样定义,本人没法根据表的记录数来分配内存了,必须写死记录数了?
struct emp
{
char name[1000000][10];
float salary[1000000];
};
但是和如下定义有什么区别马?
struct emp_record
{
char name[10];
float salary;
};
个人感觉本质上来说没区别,但是一般不选择上面的,不方便。
struct emp
{
char name[1000000][10];
float salary[1000000];
};
但是和如下定义有什么区别马?
struct emp_record
{
char name[10];
float salary;
};
个人感觉本质上来说没区别,但是一般不选择上面的,不方便。
1
#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,其它的就不想看了,人生的差距最大不过如此……