第11話 const修飾子
const01.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
const int i=10;
int main( void )
{
printf("i=%d\n",i);
return 0 ;
}
const02.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#define c10 10
int main( void )
{
int i;
i=c10;
printf("i=%d",i);
return 0 ;
}
------------------------------const02.asm
.386p
ifdef ??version
if ??version GT 500H
.mmx
endif
endif
model flat
ifndef ??version
?debug macro
endm
endif
?debug S "02const.c"
?debug T "02const.c"
_TEXT segment dword public use32 'CODE'
_TEXT ends
_DATA segment dword public use32 'DATA'
_DATA ends
_BSS segment dword public use32 'BSS'
_BSS ends
DGROUP group _BSS,_DATA
_TEXT segment dword public use32 'CODE'
_main proc near
?live1@0:
;
; int main( void )
;
push ebp
mov ebp,esp
;
; {
; int i;
;
; i=c10;
;
@1:
mov eax,10
;
; printf("i=%d",i);
;
?live1@32: ; EAX = i
push eax
push offset s@
call _printf
add esp,8
;
;
; return 0 ;
;
?live1@48: ;
xor eax,eax
;
; }
;
@3:
@2:
pop ebp
ret
_main endp
_TEXT ends
_DATA segment dword public use32 'DATA'
s@ label byte
; s@+0:
db "i=%d",0
align 4
_DATA ends
_TEXT segment dword public use32 'CODE'
_TEXT ends
public _main
extrn _printf:near
?debug D "C:\Borland\Bcc55\Include\_nfile.h" 10339 10240
?debug D "C:\Borland\Bcc55\Include\_null.h" 10339 10240
?debug D "C:\Borland\Bcc55\Include\_defs.h" 10339 10240
?debug D "C:\Borland\Bcc55\Include\_stddef.h" 10339 10240
?debug D "C:\Borland\Bcc55\Include\stdio.h" 10339 10240
?debug D "02const.c" 13130 36376
end
const03.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
const int c10=10;
int main( void )
{
int i;
i=c10;
printf("i=%d",i);
return 0 ;
}
-------------------------------const03.asm
.386p
ifdef ??version
if ??version GT 500H
.mmx
endif
endif
model flat
ifndef ??version
?debug macro
endm
endif
?debug S "03const.c"
?debug T "03const.c"
_TEXT segment dword public use32 'CODE'
_TEXT ends
_DATA segment dword public use32 'DATA'
_DATA ends
_BSS segment dword public use32 'BSS'
_BSS ends
DGROUP group _BSS,_DATA
_DATA segment dword public use32 'DATA'
align 4
_c10 label dword
dd 10
_DATA ends
_TEXT segment dword public use32 'CODE'
_main proc near
?live1@0:
;
; int main( void )
;
push ebp
mov ebp,esp
;
; {
; int i;
;
; i=c10;
;
@1:
mov eax,dword ptr [_c10]
;
; printf("i=%d",i);
;
?live1@32: ; EAX = i
push eax
push offset s@
call _printf
add esp,8
;
;
; return 0 ;
;
?live1@48: ;
xor eax,eax
;
; }
;
@3:
@2:
pop ebp
ret
_main endp
_TEXT ends
_DATA segment dword public use32 'DATA'
s@ label byte
; s@+0:
db "i=%d",0
align 4
_DATA ends
_TEXT segment dword public use32 'CODE'
_TEXT ends
public _c10
public _main
extrn _printf:near
?debug D "C:\Borland\Bcc55\Include\_nfile.h" 10339 10240
?debug D "C:\Borland\Bcc55\Include\_null.h" 10339 10240
?debug D "C:\Borland\Bcc55\Include\_defs.h" 10339 10240
?debug D "C:\Borland\Bcc55\Include\_stddef.h" 10339 10240
?debug D "C:\Borland\Bcc55\Include\stdio.h" 10339 10240
?debug D "03const.c" 13130 36384
end
const04.h
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
const int CONST_VALUE = 10;
const04.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include "const04.h"
int main( void )
{
printf("const=%d\n", CONST_VALUE);
return 0 ;
}
const04a.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include "const04.h"
extern int sub( void );
int main( void )
{
printf("const=%d\n", CONST_VALUE);
sub();
return 0 ;
}
const04b.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include "const04.h"
int sub( void )
{
printf("const=%d\n", CONST_VALUE);
return 0 ;
}
const05.h
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
extern const int CONST_VALUE;
const05a.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include "const05.h"
const int CONST_VALUE = 10;
extern int sub( void );
int main( void )
{
printf("const=%d\n", CONST_VALUE);
sub();
return 0 ;
}
const05b.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include "const05.h"
int sub( void )
{
printf("const=%d\n", CONST_VALUE);
return 0 ;
}
const06.h
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
extern const int CONST_VALUE = 10;
const06a.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include "const06.h"
extern int sub( void );
int main( void )
{
printf("const=%d\n", CONST_VALUE);
sub();
return 0 ;
}
const06b.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
#include "const06.h"
int sub( void )
{
printf("const=%d\n", CONST_VALUE);
return 0 ;
}
const07.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
int main( void )
{
const int i=5;
i=10;
printf("i=%d\n",i);
return 0 ;
}
const08.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
const int c10=10;
int main( void )
{
int i;
int *pi;
i=c10;
printf("i=%d\n",i);
pi=&c10; /* コンパイラの警告あり */
*pi=20;
i=c10;
printf("i=%d\n",i);
return 0 ;
}
const09.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
struct t_const
{
int a;
int b;
};
int main( void )
{
const struct t_const c = {0, 0};
c.a = 1;
c.b = 2;
printf("c.a=%d, c.b=%d\n", c.a, c.b);
return 0 ;
}
const10.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
const struct t_const
{
int a;
int b;
};
int main( void )
{
struct t_const c = {0, 0};
c.a = 1;
c.b = 2;
printf("c.a=%d, c.b=%d\n", c.a, c.b);
return 0 ;
}
const11.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
struct t_const
{
const int a;
int b;
};
int main( void )
{
struct t_const c = {0, 0};
c.a = 1;
c.b = 2;
printf("c.a=%d, c.b=%d\n", c.a, c.b);
return 0 ;
}
const12.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
struct t_const0
{
int a;
int b;
};
struct t_const
{
const struct t_const0 a;
int b;
};
int main( void )
{
struct t_const c = { {0,0}, 0};
c.a.a = 1;
c.a.b = 2;
c.b = 2;
printf("c.a.a=%d, c.a.b=%d, c.b=%d\n", c.a.a, c.a.b, c.b);
return 0 ;
}
const13.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
struct t_const
{
int a;
int b;
};
struct t_const_rapper
{
const struct t_const child;
};
int main( void )
{
struct t_const_rapper c = { {0,0} };
c.child.a = 1;
c.child.b = 2;
printf("c.child.a=%d, c.child.b=%d\n", c.child.a, c.child.b);
return 0 ;
}
const14.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
struct t_const
{
const int a;
const b;
};
int main( void )
{
struct t_const c = { 0, 0 };
c.a = 1;
c.b = 2;
printf("c.a=%d, c.b=%d\n", c.a, c.b);
return 0 ;
}
const15.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
int i = 0;
const int func(void)
{
i = i + 1;
return i;
}
int main( void )
{
const int j;
j = func(); /* コンパイルエラー */
printf("j=%d\n", j);
return 0 ;
}
const16.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int i = 0;
const int func(void)
{
i = i + 1;
return i;
}
int main( void )
{
const int j = func();
printf("j=%d\n", j);
return 0 ;
}
const17.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int i = 0;
const int func(void)
{
i = i + 1;
return i;
}
int main( void )
{
int j = func();
printf("j=%d\n", j);
return 0 ;
}
const18.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int i = 0;
const int * func(void)
{
i = i + 1;
return &i;
}
int main( void )
{
const int *j; /* constが無いとコンパイラが警告を表示 */
j = func(); /* jがポイントする変数は参照のみ */
printf("*j=%d\n", *j);
return 0 ;
}
const19.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int value[] = { 0, 1, 2, 3, 4 };
const int * value_get(int n)
{
return &value[n];
}
void value_set(int n, int new_value)
{
value[n] = new_value;
return;
}
int main( void )
{
int i;
const int *j;
for(i = 0; i < 5; i++)
{
j = value_get(i);
printf("*j=%d, ", *j);
/* *j = *j + 10; */ /* コンパイルエラー */
value_set(i, *j + 10);
j = value_get(i);
printf("new *j=%d\n", *j);
}
return 0 ;
}
const20.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int inc(const int i)
{
return i + 1;
}
int main( void )
{
int i;
int j;
for(i = 0; i < 5; i++)
{
printf("i=%d, ", i);
j = inc(i);
printf("j=%d\n", j);
}
return 0 ;
}
const21.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int inc(const int *i)
{
return *i + 1;
}
int main( void )
{
int i;
int j;
for(i = 0; i < 5; i++)
{
printf("i=%d, ", i);
j = inc(&i);
printf("j=%d\n", j);
}
return 0 ;
}
const22.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int k;
int inc(void) const
{
return k + 1;
}
int main( void )
{
int i;
int j;
for(i = 0; i < 5; i++)
{
printf("i=%d, ", i);
k = i;
printf("j=%d\n", inc() );
}
return 0 ;
}
const23.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int main( void )
{
int const cc30=30;
cc30=10;
printf("cc30=%d\n",cc30);
return 0 ;
}
const24.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int i = 10;
int j = 20;
int main( void )
{
const int *pi=&i; /* iがconstであることを宣言 */
printf("pi=%x, i=%x\n",pi,&i);
*pi=10;
pi=&j;
printf("pi=%x, i=%x\n",pi,&i);
return 0 ;
}
const25.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int i = 10;
int j = 20;
int main( void )
{
int * const pi=&i; /* piがconstであることを宣言 */
printf("pi=%x, i=%x\n",pi,&i);
*pi=10;
pi=&j;
printf("pi=%x, i=%x\n",pi,&i);
return 0 ;
}
const26.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int i = 10;
int j = 20;
int main( void )
{
const int * const pi=&i; /* piとiの両方がconstであることを宣言 */
printf("pi=%x, i=%x\n",pi,&i);
*pi=10;
pi=&j;
printf("pi=%x, i=%x\n",pi,&i);
return 0 ;
}
const27.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int i = 10;
int j = 20;
int main( void )
{
const int *pi; /* piは非constである */
pi=&i; /* piがポイントするiがconstになる */
pi=&j; /* piがポイントするjがconstになる */
*pi=30;
printf("*pi=%d\n",*pi);
return 0 ;
}
const28.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int i = 10;
int j = 20;
int main( void )
{
int * const pi; /* piはconstである */
pi=&i;
pi=&j;
*pi=30;
printf("*pi=%d\n",*pi);
return 0 ;
}
const29.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int i = 10;
int j = 20;
int main( void )
{
int * const pi; /* piはconstである */
*pi=30;
printf("*pi=%d\n",*pi);
return 0 ;
}
const30.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int i = 10;
int main( void )
{
int * const pi=&i; /* piはconstである */
printf("*pi=%d\n",*pi);
*pi=20;
printf("*pi=%d\n",*pi);
return 0 ;
}
volatile01.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
volatile int i;
int main( void )
{
i = 0;
while( 1 )
{
if(i == 1)
break;
}
return 0;
}
-------------------------------
volatile01.asm
TITLE 01volatile.c
.386P
include listing.inc
if @Version gt 510
.model FLAT
else
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
_DATA SEGMENT DWORD USE32 PUBLIC 'DATA'
_DATA ENDS
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
_BSS SEGMENT DWORD USE32 PUBLIC 'BSS'
_BSS ENDS
$$SYMBOLS SEGMENT BYTE USE32 'DEBSYM'
$$SYMBOLS ENDS
$$TYPES SEGMENT BYTE USE32 'DEBTYP'
$$TYPES ENDS
_TLS SEGMENT DWORD USE32 PUBLIC 'TLS'
_TLS ENDS
; COMDAT _main
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
sxdata SEGMENT DWORD USE32 'SXDATA'
sxdata ENDS
FLAT GROUP _DATA, CONST, _BSS
ASSUME CS: FLAT, DS: FLAT, SS: FLAT
endif
INCLUDELIB LIBC
INCLUDELIB OLDNAMES
_DATA SEGMENT
COMM _i:DWORD
_DATA ENDS
PUBLIC _main
; Function compile flags: /Ogty
; File 01volatile.c
; COMDAT _main
_TEXT SEGMENT
_main PROC NEAR ; COMDAT
; 12 : i = 0;
mov DWORD PTR _i, 0
mov eax, 1
npad 1
$L74654:
; 13 :
; 14 : while( 1 )
; 15 : {
; 16 : if(i == 1)
cmp DWORD PTR _i, eax
jne SHORT $L74654
; 17 : break;
; 18 : }
; 19 :
; 20 : return 0;
xor eax, eax
; 21 : }
ret 0
_main ENDP
_TEXT ENDS
END
volatile02.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
#include <Windows.h>
int i; /* volatile修飾子を削除 */
int main( void )
{
i = 0;
while( 1 )
{
if(i == 1)
break;
}
return 0;
}
-------------------------------volatile02.asm
; Listing generated by Microsoft (R) Optimizing Compiler Version 13.10.3077
TITLE 02volatile.c
.386P
include listing.inc
if @Version gt 510
.model FLAT
else
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
_DATA SEGMENT DWORD USE32 PUBLIC 'DATA'
_DATA ENDS
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
_BSS SEGMENT DWORD USE32 PUBLIC 'BSS'
_BSS ENDS
$$SYMBOLS SEGMENT BYTE USE32 'DEBSYM'
$$SYMBOLS ENDS
$$TYPES SEGMENT BYTE USE32 'DEBTYP'
$$TYPES ENDS
_TLS SEGMENT DWORD USE32 PUBLIC 'TLS'
_TLS ENDS
; COMDAT _main
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
sxdata SEGMENT DWORD USE32 'SXDATA'
sxdata ENDS
FLAT GROUP _DATA, CONST, _BSS
ASSUME CS: FLAT, DS: FLAT, SS: FLAT
endif
INCLUDELIB LIBC
INCLUDELIB OLDNAMES
_DATA SEGMENT
COMM _i:DWORD
_DATA ENDS
PUBLIC _main
; Function compile flags: /Ogty
; File 02volatile.c
; COMDAT _main
_TEXT SEGMENT
_main PROC NEAR ; COMDAT
; 11 : {
$L74654:
; 12 : i = 0;
; 13 :
; 14 : while( 1 )
; 15 : {
; 16 : if(i == 1)
jmp SHORT $L74654
_main ENDP
_TEXT ENDS
END
volatile03.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
* Hiro KITAYAMA
*/
#include <stdio.h>
int i = 0;
int main( void )
{
int j;
j = (volatile int)i;
printf("volatile int i = %d\n", j);
return 0;
}
volatile04.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
int i = 0;
int main( void )
{
volatile int *pi;
int j;
pi = &i;
j = *pi;
printf("volatile int i = %d\n", j);
return 0;
}
volatile05.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
volatile int i = 0;
int main( void )
{
int *pi;
int j;
pi = (int *)&i;
j = *pi; /* 未定義の動作 */
printf("int i = %d\n", j);
return 0;
}
volatile06.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*
*/
#include <stdio.h>
volatile int i = 0;
int main( void )
{
int itemp;
/* クリティカルセクション開始 */
/* 何らかの手順で変数 i が外部から更新されないようにしなければいけない */
itemp = i;
/* itempに必要な操作を実行 */
i = itemp;
/* 変数 i の外部から更新を許可 */
/* クリティカルセクション終了 */
return 0;
}
もどる