IT 취미생활.  
Front Page
Tag | Location | Media | Guestbook | Admin   
 
'Programming/C'에 해당하는 글(6)
2009.12.02   큰 실수 -_-;;;; 5
2009.07.01   Hello World가 실행 파일이 될때까지...
2008.08.18   Little-Endian to Big-Endian
2008.07.17   다시 들쳐 보는 문법 및 레퍼함수 - fseek 2
2008.06.30   복잡한 포인터(Pointer) 읽기
2008.05.23   포인터 읽기 9


큰 실수 -_-;;;;
초보중에서도 초보가 할 수 있는 copy and paste를 통해서
아래와 같은 코드를 만들어 냈다. ㅜㅜ

하늘을 보기가 부끄럽다.!!!!
(친구 : "인간은 실수하는 동물"라는 말에 조금은 위로하고 Code Review에 힘써야겠다.)




void *xx_mem_get(size)
{
    void *mem = (void *)malloc(size);
    if(mem == NULL) {
        error ( ~ ~~~ );
    }

    return malloc(sizeof(size));
}





Hello World가 실행 파일이 될때까지...
우선 간단하게 컴파일이 되는 아래 Hello World를 작성한다.

helloworld.c



컴파일러들을 이용하여 실행할 수 있는
실행 파일로 만드는 과정을 간단하게 적어 보면

Preprocessor --> Compiler -->Assembler  --> Linker & Relocation--> executableFile

1. 컴파일러는 helloworld.c 파일을 읽어들인다.
    (확장자에 따라 c / c++로 인식)

2. helloworld.c에 Code를 오브젝트 파일로 만든다.

3. 중간 목적 파일(기계어)로 변경한다 .

4. 중간 목적 파일을 링커 프로그램으로 연결한다.

5. exe  실행파일


Little-Endian to Big-Endian

MP4 Decoder에 Video Fream을 넘겨 줄때 앞에 4byte에 Length를 넣으라는
양키의 말에 급조해서 만들긴 했습니다.
더 좋은 코드가 있다면 언제든지 리플과 수정 부탁 드립니다.

UINT my_htonl( UINT nSource )
{
 if( 0 == nSource )
  return 0;

 UINT nResult = 0;
 nResult =  (nSource <<24);
 nResult  =  nResult  | ( 0x00ff0000 & (( nSource << 16) >>8) );
 nResult  =  nResult  | ( 0x0000ff00 & (( nSource << 8) >>16) );
 nResult  =  nResult | ( 0x000000ff & ( nSource >>24 ) );

 return nResult
}




자세히 설명이 나와 있습니다.
아래 링크를 참고 하세요.

http://ssulsamo.egloos.com/3150581


다시 들쳐 보는 문법 및 레퍼함수 - fseek

제목을 어떻게 붙일까하다가 "다시 들쳐 보는 문법 및 레퍼함수"이라 하였다.
특별히 fseek를 사용하는데 어려운 일이 있는 것도 아니다.

다만 가물 가물 해져가는 기억을 위해 실험하고 기록하여
이짓( IT )을 오래도록 해먹기 위한 발악쯤으로 생각 하면 될 듯...


fseek를 MSDN을 참조하면...

* 특정 위치로 파일의 포인터를 움직인다.
* 64bit를 위한 offset이 존재 한다는 것을 알 수 있다.

msdn에서 함수에 대한 설명을 볼때 가장 중요하게 볼 것은 역시,
인자 값과, 리턴 값일 것이다. 아래 내용을 잘 살펴보면

"If successful, fseek and _fseeki64 returns 0. Otherwise, it returns a nonzero value. "
성공과 실패에 대한 return 값을 정의하고 있다.

MSDN을 살펴보면 친절하게도 예제 코드도 제공 해준다.
굿이다.

Moves the file pointer to a specified location.

int fseek( 
   FILE *stream,
   long offset,
   int origin 
);
int _fseeki64( 
   FILE *stream,
   __int64 offset,
   int origin 
);

Parameters

stream

Pointer to FILE structure.

offset

Number of bytes from origin.

origin

Initial position.

If successful, fseek and _fseeki64 returns 0. Otherwise, it returns a nonzero value. On devices incapable of seeking, the return value is undefined. If stream is a null pointer, or if origin is not one of allowed values described below, fseek and _fseeki64 invoke the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, these functions set errno to EINVAL and return -1.

The fseek and _fseeki64 functions moves the file pointer (if any) associated with stream to a new location that is offset bytes from origin. The next operation on the stream takes place at the new location. On a stream open for update, the next operation can be either a read or a write. The argument origin must be one of the following constants, defined in STDIO.H:

SEEK_CUR

Current position of file pointer.

SEEK_END

End of file.

SEEK_SET

Beginning of file.

You can use fseek and _fseeki64 to reposition the pointer anywhere in a file. The pointer can also be positioned beyond the end of the file. fseek and _fseeki64 clears the end-of-file indicator and negates the effect of any prior ungetc calls against stream.

When a file is opened for appending data, the current file position is determined by the last I/O operation, not by where the next write would occur. If no I/O operation has yet occurred on a file opened for appending, the file position is the start of the file.

For streams opened in text mode, fseek and _fseeki64 have limited use, because carriage return–linefeed translations can cause fseek and _fseeki64 to produce unexpected results. The only fseek and _fseeki64 operations guaranteed to work on streams opened in text mode are:

  • Seeking with an offset of 0 relative to any of the origin values.
  • Seeking from the beginning of the file with an offset value returned from a call to ftell when using fseek or _ftelli64 when using _fseeki64.

Also in text mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing, fopen and all related routines check for a CTRL+Z at the end of the file and remove it if possible. This is done because using the combination of fseek and ftell or _fseeki64 and _ftelli64, to move within a file that ends with a CTRL+Z may cause fseek or _fseeki64 to behave improperly near the end of the file.

When the CRT opens a file that begins with a Byte Order Mark (BOM), the file pointer is positioned after the BOM (that is, at the start of the file's actual content). If you have to fseek to the beginning of the file, use ftell to get the initial position and fseek to it rather than to position 0.

This function locks out other threads during execution and is therefore thread-safe. For a non-locking version, see _fseek_nolock, _fseeki64_nolock.

Function Required header Compatibility

fseek

<stdio.h>

ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

_fseeki64

<stdio.h>

Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

For additional compatibility information, see Compatibility in the Introduction.

// crt_fseek.c
// This program opens the file FSEEK.OUT and
// moves the pointer to the file's beginning.
 
#include <stdio.h>

int main( void )
{
   FILE *stream;
   char line[81];
   int  result;

   if ( fopen_s( &stream, "fseek.out", "w+" ) != 0 )
   {
      printf( "The file fseek.out was not opened\n" );
      return -1;
   }
   fprintf( stream, "The fseek begins here: "
                    "This is the file 'fseek.out'.\n" );
   result = fseek( stream, 23L, SEEK_SET);
   if( result )
      perror( "Fseek failed" );
   else
   {
      printf( "File pointer is set to middle of first line.\n" );
      fgets( line, 80, stream );
      printf( "%s", line );
    }
   fclose( stream );
}

Output

File pointer is set to middle of first line. This is the file 'fseek.out'.


복잡한 포인터(Pointer) 읽기
제가 종종 들리는 신영진님의 "지니야 넷"에서 훔쳐 온 글입니다.
원문 그대로 옮겼습니다. 트랙백을 통해서 글을 연결하고 싶지만,,,,,;;,,,,

원문이 없어지는(?) 일이 종종 있어서 아쉬운 마음에 담아왔습니다.
널은 아량으로 용서를 빕니다. ~

복잡한 포인터를 읽는 법에 대해서 잘 설명을 해주셨습니다.
좌에서 우로~ 3가지의 표현을 영어로 해석하게 되면
아무리 복잡한 포인터라도 의미를 쉽게 알 수 있습니다.

팀내 친한 선배님(이의진 책임님)은 안에서 "원을 그리며 읽어라"라고 하셨습니다.
거의 일맥 상통한다 볼 수 있습니다.

저도 또한 포인터를 많이  사용합니다.
Callback Function을 주로 사용하거나, Struct에 Fucntion Pointer member를 이용하여
구조체 자체를 넘기기도 하고, 2차 배열 동적할당을 위해서 사용하기도 합니다.

포인터는 어려운 것이 아닙니다. 다만 프로그래밍을 싫어하는(?) 선배들에 의해서
포인터가 어렵데 어렵데라는 말에 벌써 벌벌 떨진 않았나 돌아 봅니다.

포인트를 이해 하기위해서는 약간의 자기만의 이해 방법이 있어야 하고 ~
도서관에서 얇은 책을 보고도 포인터를 알 수도 있습니다.
알려고 노력 했을 경우 입니다. ^^

혹시 포인터 말고도 더 많은 지식을 얻고자 한다면 아래 신영진 님의 싸이트를
방문 해보시면 ..^^ 살이 되고 피가 되는 좋은 내용들을 얻을 수 있을 것입니다.

그럼 고운하루 되세요~!



신입 개발자를 위한
복잡한 포인터 선언을 해석하는 방법
신영진 pop@jiniya.net http://www.jiniya.net

C언어 게시판에 자주 올라오는 질문 중에 하나가 복잡한 포인터 선언문을 해석하는 것들이다. 대부분의 학생들은 이것을 막연히 외우려고 한다. int *a[3]은 int 포인터를 저장하는 배열이고, int (*a)[3]은 int 배열에 대한 포인터라고 외우는 것이다. 하지만 이렇게 외운 지식은 그 형태가 조금만 달라져도 무용지물이 된다. 그렇다면 어떻게 해야 이러한 선언문을 손쉽게 해석할 수 있을까? 간단한 규칙만 이해하면 된다.

복잡한 선언을 정확하게 이해하기 위한 첫 단계는 선언문 내부에 나타나는 각 요소의 정확한 의미를 이해하는 것이다. <표 1>에는 선언문 내부에 나타나는 각 구성요소의 의미와 사용 예가 나와있다. 선언문을 해석할 때 영어를 이용하면 굉장히 편리하게 해석할 수 있다. 따라서 각 기호에 맞는 영어 표현도 같이 알아두는 것이 좋다.

표 1 표현식에 나타나는 기호들의 의미

기호

표현

의미

*

pointer to

특정 대상체를 가리키는 포인터

int *a;

a int 형을 가리키는 포인터다.

[]

array of

특정 대상체를 저장하는 배열

int a[3];

a int 3개 저장할 수 있는 배열이다.

()

function

인자를 받고 값을 리턴하는 함수

int a();

a는 인자가 없고 int를 반환하는 함수다.


다음으로 이해해야 하는 것은 선언문을 해석하는 순서다. 선언문은 선언 대상이 되는 변수 명에서 시작해서 오른쪽으로 가면서 해석한다. 선언문의 끝이나 오른쪽 괄호를 만나면 방향을 바꾸어 왼쪽으로 가면서 해석한다. 왼쪽으로 가면서 해석을 하다 왼쪽 괄호를 만나면 다시 오른쪽으로 가면서 해석한다. 이렇게 해서 선언문의 가장 왼쪽 끝에 도달하면 해석이 마무리 된다.

표 2 복잡한 표현식들의 해석 순서와 해석 결과

표현식

해석 순서

해석 결과

int **a;

 

a => a; => *a; => **a; => int **a;

a is a pointer to pointer to int

a int를 가리키는 포인터의 포인터다.

int *a[3];

 

a => a[3] => a[3]; => *a[3]; => int *a[3];

a is an array of 3 pointer to int

a int를 가리키는 포인터를 세 개 저장하는 배열이다.

int (*a)[3];

 

a => a) => (*a) => (*a)[3] => int (*a)[3]

a is a pointer to array of 3 int

a int 세 개를 저장하고 있는 배열을 가리키는 포인터다.

int *(*(*fp1)(int))[10];

fp1 => fp1) => (*fp1) => (*fp1)(int) => (*fp1)(int)) => (*(*fp1)(int)) => (*(*fp1)(int))[10] => *(*(*fp1)(int))[10]; = > int *(*(*fp1)(int))[10];

fp1 is a pointer to function that takes an int as an argument and returns a pointer to an array of 10 pointers to ints.

fp1 int를 인자로 받고 int에 대한 포인터를 열 개 저장하는 배열을 가리키는 포인터를 리턴하는 함수에 대한 포인터다.

char *(*(**foo[][8])())[];

foo[] => foo[][8] => *foo[][8] => (**foo[][8]) => (**foo[][8])() => *(**foo[][8])() => (*(**foo[][8])())[] => char *(*(**foo[][8])())[]

foo is an array of array of 8 pointers to pointer to function that returns a pointer to array of pointer to char.

foo char를 가리키는 포인터를 저장하는 배열을 가리키는 포인터를 리턴하는 함수에 대한 포인터를 가리키는 포인터를 8개 저장할 수 있는 배열에 대한 배열이다.




포인터 읽기

일전에 비슷한 글을 읽은 적이 있습니다.
다중 포인터를 쉽게 읽는 방법이라는 글이었는데..
도통 기억이 나지 않는군여. 여튼 각설하고
제가 종종 들려 이것 저것 훔쳐 오는
http://www.troot.co.kr/trbbs/zboard.php?id=newgal&no=1427
에서 담아 옵니다.

트랙백을 찾다 못 찾았습니다.

최초 작성자는
http://blog.cnrocks.net/   라는 분입니다.


고운하루 되세요.!


BLOG main image
취미생활
 Notice
 Category
분류 전체보기 (191)
매일매일갱생 (83)
서버개발 (1)
임베디드개발 (12)
Programming (80)
개발잡념 (10)
C (6)
C++ (5)
Java (4)
알고리즘 (5)
VC++ (11)
DirectShow (5)
OS (1)
Error Case (13)
Personal Projects (6)
유용한 프로그램 (0)
 TAGS
벨소리 변경 DVB-T 서태지 퇴사 Debug 군대 1seg 알고리즘 Algorithm 개발자 영어 이메일 출장 english email Java MP3 Brazil 미라지폰 spam mail C++ 음식 project isdbt English DirectShow Wince5.0 warning M480 Linux It ISDB-T C Dshow debugging VC++ 티스토리 초대장 Error Case DVB 티스토리초대 Windows Mobile6.0
 Calendar
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
 Recent Entries
 Recent Comments
 Recent Trackbacks
 Archive
 Link Site
zextor
괴짜 프로그래머의 일상사~@@
Gag & Peace, and more..
Kazakhstan Almaty.......
Min-A
Sadgarret
Steve Yoon's log
가슴 뛰는 삶을 살아라
오스틴 파워
GUI sin
melanie parker_Lady
제레미의 TV 2.0 이야기..
 Visitor Statistics
Total :
Today :
Yesterday :
rss