336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
C 코딩을 하다보면,
메모리 접근 실수 등으로 'Segmentation Fault'가 자주 발생한다.
이 때, 보통 커널에서 임의로 core dump 생성을 막아 놓지 않았다면,
보통은 디버깅을 하게 된다. 
(core dump가 발생하여도 컴파일시에 '-g' 옵션을 주지 않았다면 디버깅은 힘들어진다.)

그러나 'Segmentation Fault'가 발생 할 때, 임의로 시그널을 잡아서
프로그래밍을 하기도 하는데, 이럴 때는 core dump가 생성 되지 않으므로,
임의로 생성을 시켜줘야 한다.

아래는 그 예제이다.

-------------------------------------------------------------------------------------

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

void sighandler(int signum)
{
    printf("Process %d got signal %d\n", getpid(), signum); // Segmentation Fault 발생 시 처리 할 문구
    signal(signum, SIG_DFL); // 발생한 시그널의 handler를 Default handler로 변경
    kill(getpid(), signum); // 발생한 시그널을 다시 발생 시킴
}

void main()
{
    int *p = (int*)112233; // 임의의 메모리 번지
    int x;
    signal(SIGSEGV, sighandler); // SIGSEGV 시그널에 sighandler 등록
    x = *p; // 잘못 된 메모리 접근으로 SIGSEGV 발생
    printf("got to here...");
}


-------------------------------------------------------------------------------------

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
벌써 C 코딩만 만 5년이 넘어 가면서...
제 개인 적인 생각을 담아봤습니다.
본래 이 글은 이번에 새로 들어온 신입에게
레포트를 하나 맞겨 봤는데, 
이런 저런 충고를 한 내용입니다.

 1.될수 있으면 main or task 외에는
   function 내에서 큰 size의 값은 선언 하지 않을 것!!(특히 자주 호출 해야 하는 function같은 경우)
   - 필요하다면 상위에서 pointer만 받아 오도록 함
 2.return 할때, int, short, long, char, void, or pointer만 사용!
   - pointer return은 전역 변수의 pointer 또는
     상위 function에서 할당해서 넘겨온 인자의 memory size 이내의 값만 사용
   - function내부에서 선언된 값의 pointer return은 하면 안됨 (예외, static으로 선언된 값)
 3.인자 값으로 인자값들의 유효범위는 function 초기에 항상 체크
   - 예를 들면 pointer의 NULL 체크
 4.function 내에서 인자 선언 할때 초기값 설정
   - 예를 들면 pointer를 선언 할때 NULL로 초기화를 해야,
     그 point로 또 다른 function 호출시에 호출된 function에서 잘못된 pointer가 넘어온 것을 체크 할수 있음.

 5.가장 중요한것은 function을 만드는 사람이 '갑'이라는 것!!!
   - 내부적으로 필요하면 당연히 인터페이스를 바꿔야죠 ^^
   - 약간의 고집과 우김이 필요함.

 범외, for 또는 if 뒤에 명령 문이 하나 올때 괄호를 빼도 상관이 없지만,
       아주 단순한 명령 외에는 붙이는 습관이 좋습니다.
       처음 한번에 수정일때는 보기 좋고 깔끔하고 좋은데, 나중에 디버깅을 위해
       수정 코드, 추가 코드 또는 프린트등을 넣을 때, 다시 괄호를 넣어야 하는데 실수로 빠트리는 경우가 생겨요 ^^


Mathematics : C (R)eference 2008. 11. 6. 14:29
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Mathematics: <math.h>

Mathematics is relatively straightforward library to use again. You must #include <math.h> and must remember to link in the math library at compilation:

   cc mathprog.c -o mathprog -lm

A common source of error is in forgetting to include the <math.h> file (and yes experienced programmers make this error also). Unfortunately the C compiler does not help much. Consider:

double x;
x = sqrt(63.9);

Having not seen the prototype for sqrt the compiler (by default) assumes that the function returns an int and converts the value to a double with meaningless results.

Math Functions

Below we list some common math functions. Apart from the note above they should be easy to use and we have already used some in previous examples. We give no further examples here:

double acos(double x) -- Compute arc cosine of x. 
double asin(double x) -- Compute arc sine of x. 
double atan(double x) -- Compute arc tangent of x. 
double atan2(double y, double x) -- Compute arc tangent of y/x, using the signs of both arguments to determine the quadrant of the return value. 
double ceil(double x) -- Get smallest integral value that exceeds x. 
double cos(double x) -- Compute cosine of angle in radians. 
double cosh(double x) -- Compute the hyperbolic cosine of x. 
div_t div(int number, int denom) -- Divide one integer by another. 
double exp(double x -- Compute exponential of x 
double fabs (double x ) -- Compute absolute value of x. 
double floor(double x) -- Get largest integral value less than x. 
double fmod(double x, double y) -- Divide x by y with integral quotient and return remainder. 
double frexp(double x, int *expptr) -- Breaks down x into mantissa and exponent of no. 
labs(long n) -- Find absolute value of long integer n. 
double ldexp(double x, int exp) -- Reconstructs x out of mantissa and exponent of two. 
ldiv_t ldiv(long number, long denom) -- Divide one long integer by another. 
double log(double x) -- Compute log(x). 
double log10 (double x ) -- Compute log to the base 10 of x. 
double modf(double x, double *intptr) -- Breaks x into fractional and integer parts. 
double pow (double x, double y) -- Compute x raised to the power y. 
double sin(double x) -- Compute sine of angle in radians. 
double sinh(double x) - Compute the hyperbolic sine of x. 
double sqrt(double x) -- Compute the square root of x. 
void srand(unsigned seed) -- Set a new seed for the random number generator (rand). 
double tan(double x) -- Compute tangent of angle in radians. 
double tanh(double x) -- Compute the hyperbolic tangent of x.

Math Constants

The math.h library defines many (often neglected) constants. It is always advisable to use these definitions:

HUGE -- The maximum value of a single-precision floating-point number.
M_E -- The base of natural logarithms (e).

M_LOG2E -- The base-2 logarithm of e.

M_LOG10E - The base-10 logarithm of e.

M_LN2 -- The natural logarithm of 2.

M_LN10 -- The natural logarithm of 10.

M_PI -- $\pi$.

M_PI_2 -- $\pi$/2.

M_PI_4 -- $\pi$/4.
M_1_PI -- 1/$\pi$.

M_2_PI -- 2/$\pi$.

M_2_SQRTPI -- 2/$\sqrt{\pi}$.

M_SQRT2 -- The positive square root of 2.

M_SQRT1_2 -- The positive square root of 1/2.

MAXFLOAT -- The maximum value of a non-infinite single- precision floating point number.

HUGE_VAL -- positive infinity.

There are also a number a machine dependent values defined in #include <value.h> -- see man value or list value.h for further details.