|
방법1:
typedef struct {
unsigned char T;
unsigned char S;
unsigned short M;
}S_t;
typedef struct {
unsigned short H;
unsigned char M;
S_t S[1];
} P_t;
int Count = ???;
int TotalSize = sizeof(P_t) - sizeof(S_t) + (sizeof(S_t) * Count);
P_t *Proto = (P_t *) new unsigned char[TotalSize];
방법2:
typedef struct {
unsigned char T;
unsigned char S;
unsigned short M;
}S_t;
struct P_t{
unsigned short H;
unsigned char M;
S_t *S;
P_t():S(NULL){
}
P_t(int count){
if(count>0){
S = new S_t[count];
}else{
S = NULL;
}
}
~P_t(){
delete[] S;
}
};
P_t *Proto = new P_t(Count);
방법3:
typedef struct {
unsigned char T;
unsigned char S;
unsigned short M;
}S_t;
typedef struct {
unsigned short H;
unsigned char M;
S_t *S;
} P_t;
int Count = ???;
int TotalSize = sizeof(P_t) + (sizeof(S_t) * Count);
P_t *Proto = (P_t *) new unsigned char[TotalSize];
Proto->S = (S_t*)(Proto+1);
한수동 님이 쓰신 글 :
: typedef struct {
: unsigned char T;
: unsigned char S;
: unsigned short M;
: }S_t;
:
: typedef struct {
: unsigned short H;
: unsigned char M;
: S_t *S;
: } P_t;
:
: int Count = 사용자가 원하는 만큼의 수;
: int TotalSize = sizeof(P_t) - sizeof(S_t *) + (sizeof(S_t) * Count);
: P_t *Proto = (P_t *) new unsigned char[TotalSize];
:
: 제가 S_t 라는 스트럭쳐를 하나 만들고요
: 그리고 P_t 라는 스트럭쳐에
: S_t 스트럭쳐 하나를 포인터로 선언 했습니다
:
: 그리고 나서 new 를 이용해서 제가 원하는 사이즈만큼 메모리 공간을 잡았습니다
: (Count는 제가 원하는 S_t의 수입니다)(이렇게 한 이유는 S_t가 몇개가 필요한지 모르기 때문입니다)
:
: 그리고 나서 Proto->S[0].T 이런식으로 불러내면 자동완성 기능에는 문제 없이 뜨고 컴파일도 잘 되는데
:
: 이상하게 프로그램상에서 Proto->S[0].T 구문을 부르려고 하면 자꾸 Memory Violation 이 뜹니다 왜 그런거죠?
:
: 그래서 스트럭쳐에 있는 S를 포인터로 잡지 않고 S[] 이런 배열 식으로 잡으니까 되네요;; 포인터로 잡으나 배열로 잡으나 같은 포인터인데 왜 그런지 모르겠네요 ㅡㅡ;;
: (혹시나 해서 #pragma pack 으로 묶어 보기도 했습니다)
|