pointer 크기 관련해서 좀 헷갈리네요

pch2180의 이미지

안녕하세요. 매일 눈팅만하다가 조금 헷갈리는 점이 있어서 첫글을 작성하네요.

64bit OS 기준으로 pointer의 data size 크기가 8bytes라고 알고 있습니다.
그러면 pointer - 1 이면 실제 pointer - 0x8과 같지 않나요?
근데 실제 테스트를 해보니 pointer - 0x4와 같네요 ㅠㅠ

pointer가 아니라 int 사이즈로 가는거 같은데 뉴비에게 가르침을 주시면 감사하겠습니다!

int main(){
 
	int *ptr = 0;
	int size;
 
	scanf("%d", &size);
	ptr = malloc(size);
 
	printf("after malloc...\n");
	printf("allocated at %p\n", ptr);
	printf("prev_size : %08p | size : %08p\n",(ptr-2), (ptr-1));
        ....
}
 
result : 
0
after malloc...
allocated at 0x56415e8fea80
prev_size : 0x56415e8fea78 | size : 0x56415e8fea7c

익명 사용자의 이미지

Quote:
64bit OS 기준으로 pointer의 data size 크기가 8bytes라고 알고 있습니다.

항상 그런 건 아닙니다. 코너 케이스가 몇 가지 있긴 한데 뭐 대충 그렇게 알고 계셔도 당장은 문제 없지요.

Quote:
그러면 pointer - 1 이면 실제 pointer - 0x8과 같지 않나요?

아니요. 왜 그렇게 생각하시는지?

정수 J와 포인터 P에 대해서 P - J는 P와 같은 타입을 가지며, P가 길이 n짜리 배열 x의 x[i]를 가리키고 있을 경우[86] 0 ≤ i - j ≤ n이면 P - J는 x[i - j]를 가리키게 됩니다. 그렇지 않으면 미정의 동작입니다.

[86] P가 가리키는 것이 배열 원소가 아닐 경우, 길이 1짜리 배열을 가리키고 있는 것으로 간주합니다.

뭐 표준은 그렇다는 거고... 대체로 컴파일러가 그냥 P - J * sizeof(*P)로 계산해 버립니다. 간단히 말씀드리면, 귀하께서 포인터 ptr를 int * 타입으로 선언했기 때문에 P - J * sizeof(int)로 계산되고 있다는 겁니다. 아마도 귀하의 시스템에서 sizeof(int) = 4겠지요.

혹시 심심하시면 int *ptr 대신 double *ptr이나 int **ptr 같은 걸로도 시도해 보세요.

pch2180의 이미지

P - J * sizeof(*P)로 동작하는 거군요! 좋은 정보 감사합니다

ifree의 이미지

int *pt_int;
double *pt_double;

...

++pt_int; //4 증가(Windows)
++pt_double; //8 증가(Windows)

pch2180의 이미지

감사합니당

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.