struct 멤버가 포인터로 선언되었을때 동적할당
struct token_unit {
char *label;
char *operator;
char *operand[MAX_OPERAND];
char *comment;
//char nixbpe; // 추후 프로젝트에서 사용된다.
};
typedef struct token_unit token;
token *token_table[MAX_LINES];
static int token_line;
char ptr[10]
for (i = 0; i < MAX_LINES; i++){
token_table[i] = malloc(sizeof(token));
token_table[token_line]->label = malloc(sizeof(ptr) * sizeof(char));
token_table[token_line]->operator = malloc(sizeof(ptr) * sizeof(char));
token_table[token_line]->operand[0] = malloc(sizeof(ptr) * sizeof(char));
token_table[token_line]->comment = malloc(sizeof(ptr) * sizeof(char));
}
이렇게 동적할당을 받은 다음 토큰 멤버별에 각각 문자열을 넣으려고 하는데 struct멤버에 포인터가 쓰이고 또
struct 값을 포인터로 선언했을때 메모리 할당을 이런 식으로 하는게 맞을까요??..


참고해보세요.
//
포인터의 크기는 32비트에서 4바이트. 64비트에서 8바이트.로 표시됩니다.
포인터의 실제 크기를 확인해서 사용해보세요.
//
포인터는 할당한 만큼. 사용하시면 됩니다.
포인터는 할당했으면. 해제 해 주어야 합니다.
포인터는 중복 할당하는것은 좋지 않습니다.
각 함수에 인자값과 리턴값을 man 설명 페이지등에서 확인해 보시기 바랍니다.
//
구조체 패딩에 관해서 문의드립니다
https://kldp.org/node/19
구조체 패딩 비트에 대해서 질문 드립니다.
https://kldp.org/node/151648
패딩은 대충. 가장 큰 변수의 크기로. 각 변수에 공간을 채워준다고 생각하시면 편합니다.
//
웹 컴파일러 테스트 코드
http://codepad.org/LNoEWPpV
#include <stdio.h> #define MAX_OPERAND 10 #define MAX_LINES 10 //구조체는 변수의 크기가 다를 경우. 변수 공간을 채워주는. 패딩 현상이 발생하여. //구조체의 전체 크기가 다르게 표시 될 수 있습니다. //구조체 크기를 확인하거나. //컴파일러나 코드에서 패딩 크기나 옵션을 지정하는 방법이 있습니다. //운영체제 32비트 64비트에서도 확인해보시기 바랍니다. struct token_unit { char *label; char *operator; char *operand[MAX_OPERAND]; char *comment; //char nixbpe; // 추후 프로젝트에서 사용된다. }; typedef struct token_unit token; int main() { token *token_table[MAX_LINES]; static int token_line; char ptr[10]; int i; //c 에서는 변수 선언을 맨 위에 해주어야 컴파일 오류가 발생하지 않습니다. //필요에 따라서. 각 값은 초기화 하는것이 좋습니다. //signed unsigned 데이터 범위도 확인해보시기 바랍니다. //구조체의 인스턴스와 포인터 크기를 출력해서 확인한다. printf("sizeof(token) %d\n", sizeof(token)); printf("sizeof(ptr) * sizeof(char) %d\n",sizeof(ptr) * sizeof(char)); printf("sizeof(ptr) %d\n", sizeof(ptr)); printf("sizeof(char) %d\n", sizeof(char)); //메모리 생성 및 할당 for (i = 0; i < MAX_LINES; i++) { token_table[i] = malloc(sizeof(token)); } token_table[token_line]->label = malloc(sizeof(ptr) * sizeof(char)); token_table[token_line]->operator = malloc(sizeof(ptr) * sizeof(char)); token_table[token_line]->operand[0] = malloc(sizeof(ptr) * sizeof(char)); token_table[token_line]->comment = malloc(sizeof(ptr) * sizeof(char)); //메모리 해제 //메모리 해제 전에 할당 되었는지 확인하는 방법도 있을겁니다. free(token_table[token_line]->label ); free(token_table[token_line]->operator ); free(token_table[token_line]->operand[0]); free(token_table[token_line]->comment ); for (i = 0; i < MAX_LINES; i++) { free(token_table[i]); } return 0; }//출력 결과
sizeof(token) 52
sizeof(ptr) * sizeof(char) 10
sizeof(ptr) 10
sizeof(char) 1
http://codepad.org/ZPT1QOSQ
#include <stdio.h> typedef struct DF_STRUCT { char a; //1byte int b; //4byte }STRUCT; int main() { STRUCT st; printf("%d \n", sizeof(st)); return 0; }//출력 결과
8
http://codepad.org/VJT3K5dr
#include <stdio.h> //#pragma pack(push, 1); //#pragma pack(pop); //#pragma align() #pragma pack(1) typedef struct DF_STRUCT { char a; //1byte int b; //4byte }STRUCT; #pragma pack() /* default */ int main() { STRUCT st; printf("%d \n", sizeof(st)); return 0; }//출력 결과
5
----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.
매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.
각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com
sizeof(ptr[0]) * (sizeof(ptr)
sizeof(ptr[0]) * (sizeof(ptr) / sizeof(ptr[0]))) 을 쓰시던가 sizeof(ptr) 을 쓰시던가 둘 중의 하나만 하세요.
#include <stdio.h> #include <stdlib.h> #define MAX_LINES (7) #define MAX_OPERAND (3) typedef char token_str_t[10]; typedef struct { token_str_t *label; token_str_t *operator; token_str_t *operand[MAX_OPERAND]; token_str_t *comment; } token_unit_t; #define array_nr(x) (int)(sizeof(x)/sizeof((x)[0])) int main(void) { token_unit_t *token_table[MAX_LINES]; #define pr_size(x) printf("%s: %zd\n", #x, sizeof(x)) pr_size(token_str_t); pr_size(token_unit_t); pr_size(token_table); pr_size(token_table[0]); pr_size(*token_table[0]); pr_size(token_table[0]->label); pr_size(*token_table[0]->label); pr_size(token_table[0]->operand); pr_size(token_table[0]->operand[0]); pr_size(*token_table[0]->operand[0]); #undef pr_size int i; for (i = 0; i < array_nr(token_table); i++) { token_unit_t *token = malloc(sizeof(*token)); token->label = malloc(sizeof(*token->label)); token->operator = malloc(sizeof(*token->operator)); token->comment = malloc(sizeof(*token->comment)); int j; for (j = 0; j < array_nr(token->operand); j++) token->operand[j] = malloc(sizeof(*token->operand[0])); token_table[i] = token; } return 0; }댓글 달기