0

私はこのサンプルプログラムを実行しており、出力からそれがどのマシンタイプであるかを知ることができると思われます。1つまたは2つの値を検査することによるものだと確信していますが、この検査をどのように実行する必要がありますか?

/* pointers.c - Test pointers
 * Written 2012 by F Lundevall
 * Copyright abandoned. This file is in the public domain.
 *
 * To make this program work on as many systems as possible,
 * addresses are converted to unsigned long when printed.
 * The 'l' in formatting-codes %ld and %lx means a long operand. */

#include <stdio.h>
#include <stdlib.h>

int * ip; /* Declare a pointer to int, a.k.a. int pointer. */
char * cp; /* Pointer to char, a.k.a. char pointer. */

/* Declare fp as a pointer to function, where that function
 * has one parameter of type int and returns an int.
 * Use cdecl to get the syntax right, http://cdecl.org/ */
int ( *fp )( int );

int val1 = 111111;
int val2 = 222222;

int ia[ 17 ]; /* Declare an array of 17 ints, numbered 0 through 16. */
char ca[ 17 ]; /* Declare an array of 17 chars. */

int fun( int parm )
{
  printf( "Function fun called with parameter %d\n", parm );
  return( parm + 1 );
}

/* Main function. */
int main()
{
  printf( "Message PT.01 from pointers.c: Hello, pointy World!\n" );

  /* Do some assignments. */
  ip = &val1;
  cp = &val2; /* The compiler should warn you about this. */
  fp = fun;

  ia[ 0 ] = 11; /* First element. */
  ia[ 1 ] = 17;
  ia[ 2 ] = 3;
  ia[ 16 ] = 58; /* Last element. */

  ca[ 0 ] = 11; /* First element. */
  ca[ 1 ] = 17;
  ca[ 2 ] = 3;
  ca[ 16 ] = 58; /* Last element. */

  printf( "PT.02: val1: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &val1, val1, val1 );
  printf( "PT.03: val2: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &val2, val2, val2 );
  printf( "PT.04: ip: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &ip, (long) ip, (long) ip );
  printf( "PT.05: Dereference pointer ip and we find: %d \n", *ip );
  printf( "PT.06: cp: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &cp, (long) cp, (long) cp );
  printf( "PT.07: Dereference pointer cp and we find: %d \n", *cp );

  *ip = 1234;
  printf( "\nPT.08: Executed *ip = 1234; \n" );
  printf( "PT.09: val1: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &val1, val1, val1 );
  printf( "PT.10: ip: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &ip, (long) ip, (long) ip );
  printf( "PT.11: Dereference pointer ip and we find: %d \n", *ip );
  printf( "PT.12: val1: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &val1, val1, val1 );

  *cp = 1234; /* The compiler should warn you about this. */
  printf( "\nPT.13: Executed *cp = 1234; \n" );
  printf( "PT.14: val2: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &val2, val2, val2 );
  printf( "PT.15: cp: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &cp, (long) cp, (long) cp );
  printf( "PT.16: Dereference pointer cp and we find: %d \n", *cp );
  printf( "PT.17: val2: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &val2, val2, val2 );

  ip = ia;
  printf( "\nPT.18: Executed ip = ia; \n" );
  printf( "PT.19: ia[0]: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &ia[0], ia[0], ia[0] );
  printf( "PT.20: ia[1]: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &ia[1], ia[1], ia[1] );
  printf( "PT.21: ip: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &ip, (long) ip, (long) ip );
  printf( "PT.22: Dereference pointer ip and we find: %d \n", *ip );

  ip = ip + 1; /* add 1 to pointer */
  printf( "\nPT.23: Executed ip = ip + 1; \n" );
  printf( "PT.24: ip: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &ip, (long) ip, (long) ip );
  printf( "PT.25: Dereference pointer ip and we find: %d \n", *ip );

  cp = ca;
  printf( "\nPT.26: Executed cp = ca; \n" );
  printf( "PT.27: ca[0]: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &ca[0], ca[0], ca[0] );
  printf( "PT.28: ca[1]: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &ca[1], ca[1], ca[1] );
  printf( "PT.29: cp: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &cp, (long) cp, (long) cp );
  printf( "PT.30: Dereference pointer cp and we find: %d \n", *cp );

  cp = cp + 1; /* add 1 to pointer */
  printf( "\nPT.31: Executed cp = cp + 1; \n" );
  printf( "PT.32: cp: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &cp, (long) cp, (long) cp );
  printf( "PT.33: Dereference pointer cp and we find: %d \n", *cp );

  ip = ca; /* The compiler should warn you about this. */
  printf( "\nPT.34: Executed ip = ca; \n" );
  printf( "PT.35: ca[0]: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &ca[0], ca[0], ca[0] );
  printf( "PT.36: ca[1]: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &ca[1], ca[1], ca[1] );
  printf( "PT.37: ip: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &ip, (long) ip, (long) ip );
  printf( "PT.38: Dereference pointer ip and we find: %d \n", *ip );

  cp = ia; /* The compiler should warn you about this. */
  printf( "\nPT.39: Executed cp = ia; \n" );
  printf( "PT.40: cp: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &cp, (long) cp, (long) cp );
  printf( "PT.41: Dereference pointer cp and we find: %d \n", *cp );

  printf( "\nPT.42: fp: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &fp, (long) fp, (long) fp );
  printf( "PT.43: Dereference fp and see what happens.\n" );
  val1 = (*fp)(42);
  printf( "PT.44: Executed val1 = (*fp)(42); \n" );
  printf( "PT.45: val1: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &val1, val1, val1 );

  return( 0 );
}

出力

Message PT.01 from pointers.c: Hello, pointy World!
PT.02: val1: stored at 21e50 (hex); value is 111111 (dec), 1b207 (hex)
PT.03: val2: stored at 21e54 (hex); value is 222222 (dec), 3640e (hex)
PT.04: ip: stored at 21eb8 (hex); value is 138832 (dec), 21e50 (hex)
PT.05: Dereference pointer ip and we find: 111111
PT.06: cp: stored at 21e6c (hex); value is 138836 (dec), 21e54 (hex)
PT.07: Dereference pointer cp and we find: 0

PT.08: Executed *ip = 1234;
PT.09: val1: stored at 21e50 (hex); value is 1234 (dec), 4d2 (hex)
PT.10: ip: stored at 21eb8 (hex); value is 138832 (dec), 21e50 (hex)
PT.11: Dereference pointer ip and we find: 1234
PT.12: val1: stored at 21e50 (hex); value is 1234 (dec), 4d2 (hex)

PT.13: Executed *cp = 1234;
PT.14: val2: stored at 21e54 (hex); value is -771529714 (dec), d203640e (hex)
PT.15: cp: stored at 21e6c (hex); value is 138836 (dec), 21e54 (hex)
PT.16: Dereference pointer cp and we find: -46
PT.17: val2: stored at 21e54 (hex); value is -771529714 (dec), d203640e (hex)

PT.18: Executed ip = ia;
PT.19: ia[0]: stored at 21e74 (hex); value is 11 (dec), b (hex)
PT.20: ia[1]: stored at 21e78 (hex); value is 17 (dec), 11 (hex)
PT.21: ip: stored at 21eb8 (hex); value is 138868 (dec), 21e74 (hex)
PT.22: Dereference pointer ip and we find: 11

PT.23: Executed ip = ip + 1;
PT.24: ip: stored at 21eb8 (hex); value is 138872 (dec), 21e78 (hex)
PT.25: Dereference pointer ip and we find: 17

PT.26: Executed cp = ca;
PT.27: ca[0]: stored at 21e58 (hex); value is 11 (dec), b (hex)
PT.28: ca[1]: stored at 21e59 (hex); value is 17 (dec), 11 (hex)
PT.29: cp: stored at 21e6c (hex); value is 138840 (dec), 21e58 (hex)
PT.30: Dereference pointer cp and we find: 11

PT.31: Executed cp = cp + 1;
PT.32: cp: stored at 21e6c (hex); value is 138841 (dec), 21e59 (hex)
PT.33: Dereference pointer cp and we find: 17

PT.34: Executed ip = ca;
PT.35: ca[0]: stored at 21e58 (hex); value is 11 (dec), b (hex)
PT.36: ca[1]: stored at 21e59 (hex); value is 17 (dec), 11 (hex)
PT.37: ip: stored at 21eb8 (hex); value is 138840 (dec), 21e58 (hex)
PT.38: Dereference pointer ip and we find: 185664256

PT.39: Executed cp = ia;
PT.40: cp: stored at 21e6c (hex); value is 138868 (dec), 21e74 (hex)
PT.41: Dereference pointer cp and we find: 0

PT.42: fp: stored at 21e70 (hex); value is 69288 (dec), 10ea8 (hex)
PT.43: Dereference fp and see what happens.
Function fun called with parameter 42
PT.44: Executed val1 = (*fp)(42);
PT.45: val1: stored at 21e50 (hex); value is 43 (dec), 2b (hex)
4

2 に答える 2

5

なぜだけではないのですか

unsigned short u = 0xABCD;
if ( *(const char *)&u == 0xAB )
   /*big endian*/;
else
   /*little endian*/;

動作する理由は次のとおりです。ビッグエンディアンアーキテクチャでは、最上位バイトが(プリミティブ型の)変数内のすべてのバイトの最下位アドレスを持ち、IOWの最上位バイトが最初になります。リトルエンディアンアーキテクチャでは、その逆です。最下位バイトが最初に来ます。このコードは、アドレスが最小のバイトに数値の最上位バイトが含まれているかどうかをチェックするだけです。

エンディアンの詳細はこちら

于 2012-10-18T10:58:38.740 に答える
3

はい、出力行を見れば、このコードがビッグエンディアンまたはリトルエンディアンのどちらのマシンで実行されているかを判断できますPT.07: Dereference pointer cp and we find: 0。逆参照されたポインターcp0ビッグエンディアンのマシンである場合、それが14の場合、リトルエンディアンです。

理由:

cpval2はcharへのポインタであり、値が。であるint変数のアドレスが割り当てられます222222。参照を解除cpすると、最初のバイトはビッグエンディアンval2ではバイナリですが、リトルエンディアンでは取得されます。0000 00000000 1110

于 2012-10-18T11:54:11.400 に答える