c에서 구조체 복사 관련해서 질문이용

fallsmile의 이미지

다음과 같은 3개의 구조체가 있습니다.
typedef struct RelFileNode
{
int spcNode; /* tablespace */
int dbNode; /* database */
int relNode; /* relation */
} RelFileNode;

typedef struct buftag {
RelFileNode rnode; /* physical relation identifier */
int forkNum;
int blockNum; /* blknum relative to begin of reln */
} BufferTag;

typedef struct iclbuftag {
RelFileNode rnode; /* physical relation identifier */
int forkNum;
int blockNum; /* blknum relative to begin of reln */
int lagNum; /* LAG number */
} IclBufferTag;

IclBufferTag는 기존 BufferTag에 하나의 필드(lagNum)만 더 추가한 것이고요

이런 상태에서 만약에서

(질문1)c에서 구조체 복사를 아래와 같이 수행한다면...
/*** example code ***/
IclBufferTag tag1, tag2;
RelFileNode rel1;
rel1.spcNode = 1;

tag1.rnode = rel1;

tag2 = tag1
/*** example code ***/
tag2의 rnode에 rel1에 셋팅된 값이 제대로 복사가 되나요?
ㅎㅎ c에서는 구조체 복사 시 기본 타입만 된다해서... 흠 구조체 안의 구조체는 복사가 안되나해서요

(질문2) c에서 조금 다른 구조체의 복사를 memcpy로 수행했을 경우

/*** example code ***/
BufferTag tag1;
IclBufferTag tag2;

RelFileNode rel1;
rel1.spcNode = 1;

tag1.rnode = rel1;

memcpy(&tag2, &tag1, sizeof(BufferTag));
/*** example code ***/

이런 예제 코드에서 하고자하는 바는 IclBufferTag가 BufferTag랑 다른 것은 다 같고 마지막 필드만 하나
추가된 것이므로, 마지막 필드만 제외하고 BufferTag에서 설정한 값을 다 복사하고 싶은 것인데

제대로 복사가 안되더군요... RelFileNode의 값이 깨지게 되는데
이유를 잘 모르겠습니다.
두 구조체 간에 필드 사이를 padding하는 방식이 달라져서 그렇게 되는 것일까요?

실제 테스트 코드는 linux 환경에서 gcc 컴파일러로 돌리고 있습니다.

질문2에 대해서는 왜 문제가 생기는지 원인을 알려주신다면 정말 감사하겠습니다.

댓글 달기

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