메모리 복사와 Byte order?

cg3980의 이미지

A4 BB 94 DE D1 06 40 4D A1 6A BF D5 01 79 D6 AC
9B FC DA 09 BE 2B E0 4B 8D 9A 04 EA 4F 3C 56 2A
00 08 00 00 00 00 00 00 FF 17 0E 00 00 00 00 00
01 00 00 00 00 00 00 80 42 00 61 00 73 00 69 00
63 00 20 00 64 00 61 00 74 00 61 00 20 00 70 00
61 00 72 00 74 00 69 00 74 00 69 00 6F 00 6E 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

위는 실제 하드디스크에 저장되어있는 byte들입니다.

이 byte는 128byte이고 구조체에 저장시키려고 합니다.

typedef struct __GPTable {
U8 PartitonTypeGUID[16];
U8 PartitionGUID[16];
U64 PartitionStartingLBA;
U64 PartitionEndeingLBA;
U64 PartitionAtts;
U8 PartitonName[72];
} GPTable;

GPTable PT;

memcpy(&PT, HDDdump + GUIDoffset, sizeof(U8) * Gheader.SizeOfEntries);

위와같은 구조체에 실제로 128byte를 넣어줬고(구조체 크기도 128byte입니다.)
제가 필요한 PartitionStartingLBA 멤버를 printf("%016lX", PT.PartitionStartingLBA)로 출력해봤는데
00 00 00 00 00 00 08 00로 나타났습니다.

신기하게도 LittleEndian으로 변환해야 하는 PartitionStarting LBA, PartitionEndingLBA이 Little Endian으로 변하고, 나머지는 Big Endian으로 유지되었습니다.(Partition Atts는 사실 변환되면 안되는데 변환됨..)

memcpy가 자동으로 바꿔주는 기능이 있는건가요?

jachin의 이미지

A4 BB 94 DE D1 06 40 4D A1 6A BF D5 01 79 D6 AC
9B FC DA 09 BE 2B E0 4B 8D 9A 04 EA 4F 3C 56 2A
00 08 00 00 00 00 00 00 FF 17 0E 00 00 00 00 00
01 00 00 00 00 00 00 80 42 00 61 00 73 00 69 00
63 00 20 00 64 00 61 00 74 00 61 00 20 00 70 00
61 00 72 00 74 00 69 00 74 00 69 00 6F 00 6E 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 08 00

얼핏 보면 아래 답변이 도움이 될 것 같습니다만... 아직 제가 뭐가 문제점인지 이해하지 못했네요...

https://stackoverflow.com/questions/4544372/memory-alignment-attribute-aligned-8

익명 사용자의 이미지

설마 memcpy가 그걸 알아서 해줄리가요.

그냥 리틀 엔디안으로 저장되어 있있기 때문에 리틀 엔디안 CPU에서 변환없어도 올바르게 보이는 것입니다.

나머지는 빅 엔디안으로 유되어었다고 하셨는데, Partition Atts는 변환되었다고 표현하셨으니까,
남은 것들은 U8 들 뿐인데, 이것들은 말씀하시는 엔디안과는 관련 없습니다.

익명 사용자의 이미지

C언어에서 흔히 쓰는 저수준 파일 입출력 함수들은 바이트열 단위로 동작합니다.
memcpy도 마찬가지입니다.
위 두 함수 모두 Endian 따위는 신경도 쓰지 않습니다.

Endian에 맞게 바이트열을 해석해준 함수는 printf("%016lX", PT.PartitionStartingLBA) 이지요.
지정한 위치의 바이트열을 64비트 정수로 해석해서 16진수로 출력하라고 지시했으니 그대로 한 겁니다.
별로 신기할 것도 없습니다. 컴퓨터는 늘 시키는 대로 하죠.

댓글 달기

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