c에서 구조체의 주소와 구조체 첫번째 요소의 주소가 항상 같나

moonzoo의 이미지

C언어에서 구조체의 주소와 구조체 첫번째 요소의

주소가 항상 같음이 보장되나여?

예를 들면

struct _Super{
   struct _First first;
   ~~~
   ~~~
};

struct _Super super;

위와 같은 코드가 있을경웨

&super == &(super.first) 가 항상 참일까요?
(단 type은 배제하구요.)

익명 사용자의 이미지

네 맞습니다. 항상 보장이 됩니다.

C99 6.7.2.1 Structure and union specifiers

13 Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a
bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.

keedi의 이미지

Quote:

기본적으로 구조체형의 멤버는 선언된 순서대로 메모리에 배치된다.
즉 먼저 선언된 멤버가 구조체형 대상체를 위해 할당된 전체 메모리의
낮은 번지를 차지한다는 뜻이다.

구조체의 첫번째 선언 멤버는 구조체의 가장 처음에 배치가 된다.

모든 데이터 형은 자신에게 적절한 정렬 제한을 갖는다.
그렇기 때문에 구조체의 시작 부분을 제외한 멤버 사이에 추가되는
빈 공간(바이트)를 패딩, 또는 홀 이라고 한다.
이 패딩은 임의의 값으로 채워져 있다.

c 언어 펀더맨탈에는 저렇게 나와 있네요.
음 그렇다면 일단 보장받을수 있을것 같긴 한데...

(기본적으로라는 말은 근데 다르게도 할 수 있다는 뉘앙스가 ... 흠... 아닌가요)

----
use perl;

Keedi Kim

doldori의 이미지

keedi wrote:
(기본적으로라는 말은 근데 다르게도 할 수 있다는 뉘앙스가 ... 흠... 아닌가요)

뉘앙스라는 것이 워낙 미묘해서 그렇게 해석될 수도 있는지 없는지 모르겠지만... ^^;
아닙니다.
moonzoo의 이미지

답변 감사합니다.

제가 물어본 이유는..

C++의 인터페이스와 같은 느낌을.

C에서 살려보고자 함이었습니다.

여러개의 비슷한 성질의 구조체 앞단에

인터페이스 역할을 하는 구조체(함수 포인터로 이루어진)를 심어놓고

제어해보자는 취지였는데요.

몇가지 테스트를 해 본결과 만족스럽네요.

익명 사용자의 이미지

Object-Oriented Programming with ANSI C 란 이름의 문서를 소개합니다.

중간부터 ooc 프리프로세서를 이용한 방법으로 변질되긴 하는데... 전반부의 내용은 제법 재미있습니다.

http://www.planetpdf.com/codecuts/pdfs/ooc.pdf

현재 하고 계신 일에 보탬이 되길 바랍니다. :-)

정태영의 이미지

moonzoo wrote:
답변 감사합니다.

제가 물어본 이유는..

C++의 인터페이스와 같은 느낌을.

C에서 살려보고자 함이었습니다.

여러개의 비슷한 성질의 구조체 앞단에

인터페이스 역할을 하는 구조체(함수 포인터로 이루어진)를 심어놓고

제어해보자는 취지였는데요.

몇가지 테스트를 해 본결과 만족스럽네요.

glib 과 gtk 가 그런 식으로 짜여져 있습니다 :)

덕분에 심심하면 타입캐스팅이 -_-;;;;;

오랫동안 꿈을 그리는 사람은 그 꿈을 닮아간다...

http://mytears.org ~(~_~)~
나 한줄기 바람처럼..

moonzoo의 이미지

Anonymous wrote:
Object-Oriented Programming with ANSI C 란 이름의 문서를 소개합니다.

중간부터 ooc 프리프로세서를 이용한 방법으로 변질되긴 하는데... 전반부의 내용은 제법 재미있습니다.

http://www.planetpdf.com/codecuts/pdfs/ooc.pdf

현재 하고 계신 일에 보탬이 되길 바랍니다. :-)

감사합니다~

많은 도움이 될 것 같습니다!

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.