[問題] 在struct宣告dynamic array的問題
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
ubuntu 12.04
問題(Question):
用vector宣告不確定大小的struct array 此struct有dynamic array
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
typedef struct Test{
int n;
char* test;
Test(int);
~Test();
}Test;
Test::Test(int i){
n = i;
test = (char*)malloc(sizeof(char) * n);
}
Test::~Test(){
free(test);
}
int main()
{
int i;
scanf("%d",&i);
std::vector<Test> test(i,Test(10));
printf("%d\n",test[1].n);
return 0;
}
補充說明(Supplement):
好像宣告完後 就會馬上進入 desctructor @@
所以Memory就會出錯 假如不要宣告dynamic array就不會有錯誤= =
怎麼會降....
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.45.237.106
→
04/05 11:20, , 1F
04/05 11:20, 1F
→
04/05 11:22, , 2F
04/05 11:22, 2F
→
04/05 11:23, , 3F
04/05 11:23, 3F
→
04/05 11:24, , 4F
04/05 11:24, 4F
→
04/05 11:25, , 5F
04/05 11:25, 5F
→
04/05 11:40, , 6F
04/05 11:40, 6F
→
04/05 11:41, , 7F
04/05 11:41, 7F
推
04/05 11:44, , 8F
04/05 11:44, 8F
推
04/05 11:50, , 9F
04/05 11:50, 9F
→
04/05 11:52, , 10F
04/05 11:52, 10F
→
04/05 11:53, , 11F
04/05 11:53, 11F
→
04/05 12:03, , 12F
04/05 12:03, 12F
→
04/05 12:03, , 13F
04/05 12:03, 13F
→
04/05 12:21, , 14F
04/05 12:21, 14F
→
04/05 12:22, , 15F
04/05 12:22, 15F
→
04/05 12:22, , 16F
04/05 12:22, 16F
→
04/05 12:24, , 17F
04/05 12:24, 17F
→
04/05 12:25, , 18F
04/05 12:25, 18F
→
04/05 12:26, , 19F
04/05 12:26, 19F
推
04/05 12:53, , 20F
04/05 12:53, 20F
→
04/05 12:54, , 21F
04/05 12:54, 21F
→
04/05 12:55, , 22F
04/05 12:55, 22F
→
04/05 12:55, , 23F
04/05 12:55, 23F
→
04/05 13:05, , 24F
04/05 13:05, 24F
→
04/05 13:06, , 25F
04/05 13:06, 25F
→
04/05 13:10, , 26F
04/05 13:10, 26F
→
04/05 13:11, , 27F
04/05 13:11, 27F
→
04/05 13:14, , 28F
04/05 13:14, 28F
→
04/05 13:16, , 29F
04/05 13:16, 29F
→
04/05 13:17, , 30F
04/05 13:17, 30F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章