'sizeof'에 해당되는 글 2건

  1. 2011.03.12 atmega128 은 8bit 프로세서임! + 변수의 크기
  2. 2009.04.22 sizeof() 는 언제 계산될까?
embeded/AVR (ATmega,ATtiny)2011. 3. 12. 22:13
WINAVR로 프로그램을 하긴 했지만, sizeof로 일일이 확인해본게 아니라 긴가민가 했는데
일단 ATMEGA128은 8bit 프로세서이고, int 형은 2byte이다.


ATmega128   8-bit AVR Microcontroller, 128KB Flash, 64-pin
                   View Parameters



[링크 : http://blog.daum.net/lucifer_blog/38]

 The concept is quite simple. The file types.h includes the ANSI-required file limits.h. It then explicitly tests each of the predefined data types for the smallest type that matches signed and unsigned 1-, 8-, 16-, and 32-bit variables. The result is that my data type UCHAR is guaranteed to be an 8-bit unsigned variable, INT is guaranteed to be a 16-bit signed variable, and so forth. In this manner, the following data types are defined: BOOLEAN, CHAR, UCHAR, INT, UINT, LONG, and ULONG.

Posted by 구차니
Programming/C Win32 MFC2009. 4. 22. 11:25
sizeof()는 type의 크기나 문자열의 길이를 알아 낼때 쓰이는 유용한 녀석이다
근데 문제는 생긴 꼴은 function() 이라서 대충 보면 함수 같은데
엄밀하게 이녀석은 operator이다.(연산자)

그런 이유로, sizeof()는 runtime시에 값이 치환되는것이 아닌 compile time에 값이 치환된다.


#include "stdio.h"
#include "stdlib.h"
#include "string.h"

void main()
{
	char str[] = "Hello World";
	char *str2 = NULL;

	printf("sizeof(char)  %d\n",sizeof(char));
	printf("sizeof(short) %d\n",sizeof(short));
	printf("sizeof(int)   %d\n",sizeof(int));
	printf("sizeof(str)   %d\n",sizeof(str));
	printf("sizeof(*str)  %d\n",sizeof(*str));
	
	str2 = (char*)malloc(12);
	printf("sizeof(str2)  %d\n",sizeof(str2));
	printf("sizeof(*str2) %d\n",sizeof(*str2));

	free(str2);
}

sizeof(char)  1
sizeof(short) 2
sizeof(int)   4
sizeof(str)   12
sizeof(*str)  1
sizeof(str2)  4
sizeof(*str2) 1

처럼 runtime시에 할당되는 크기는 알 수가 없다.
위의 것은 12바이트 문자열이므로(11 + 1byte NULL) 12가 나왔지만
아래는 *char 즉, 포인터 형으로 4byte가 나온것이다.

[링크 : http://www.velocityreviews.com/forums/t635338-sizeof-calculated-at-compile-time-or-run-time.html
Posted by 구차니