[완료] C언어로 코딩을 했는데.. 실행시 세그먼트 폴트 오류가 뜨는데 해결이 안됩니다..

mewestside의 이미지

C언어로 코딩을 했습니다...

컴파일은 문제 없이 됩니다..

근데 실행시 세그먼트 폴트 오류가 뜨는데 해결이 안됩니다..

어느 부분이 잘못 되었는지 지적 좀 부탁드립니다...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "sacdata.h"
 
long int read_sac(char *fname, SAC_DATA *sdata, float *beg, float *dt);
 
//SAC_DATA ew, ns;
SAC_DATA e1, n1, z1, e2, n2, z2;
 
float XCOR(long int n1, float *dat1, float *dat2);
float CRMS(float *dat, long int start, long int end);
 
int main(int argc, char *argv[])
{
	int i, j, k;
	long int a, b;
	float bege, begn, dt;
	float *coee, *coen, *coez, *coesum, *coeavg;
	float *coerms, *ti;
	FILE *f;
 
	if( argc < 7 )
	{
		fprintf(stderr,"\nUsage: %s e1.sac n1.sac z1.sac e2.sac n2.sac z2.sac\n",argv[0]);
		return -1;
	}
 
	read_sac(argv[1],&e1,&bege,&dt);
	read_sac(argv[2],&n1,&begn,&dt);
	read_sac(argv[3],&z1,&begn,&dt);
	read_sac(argv[4],&e2,&begn,&dt);
	read_sac(argv[5],&n2,&begn,&dt);
	read_sac(argv[6],&z2,&begn,&dt);
 
	if( e1.npts != n1.npts || e1.npts != z1.npts || n1.npts != z1.npts )
	{
		fprintf(stderr,"\nError:  e1, n1, z1 NPTS not same\n");
		return -1;
	}
 
	if( e2.npts != n2.npts || e2.npts != z2.npts || n2.npts != z2.npts )
	{
		fprintf(stderr,"\nError:  e2, n2, z2 NPTS not same\n");
		return -1;
	}
 
	/////////////// Phase info. ///////////////
 
	f=fopen("coe_data.txt", "w");
 
	for (i=0; i<e2.npts-e1.npts; i++)
	{
		coee[i] = XCOR(e1.npts, e1.fdat, e2.fdat+i);
		coen[i] = XCOR(n1.npts, n1.fdat, n2.fdat+i);
		coez[i] = XCOR(z1.npts, z1.fdat, z2.fdat+i);
		coesum[i] = coee[i] + coen[i] + coez[i];
		coeavg[i] = coesum[i] / 3.;
//		fprintf(f, "%d  %.2f  %.2f  %.2f  %.2f  %.2f  %.2f  %.2f\n", i, i*dt, coeavg, coee, coen, coez, coesum, coeavg);
	}
 
	a = 100;
	b = 250;
 
	for (i=0; i<e2.npts; i++)
	{
		coerms[i] = CRMS(coeavg, a+i, b+i);
		ti[i] = i*dt;
	}
 
	fprintf(f, "%.2f  %.2f  %.2f  %.2f  %.2f  %.2f  %.2f\n", ti, coee, coen, coez, coesum, coeavg, coerms);
	fclose(f);
 
	return 0;
}
 
float XCOR(long int n1, float *dat1, float *dat2)
{
	int i, j, k;
	float bege, begn, dt;
	float ewsum, nssum, ewavg, nsavg;
	float ewsq, nssq, ewd, nsd;
	float ewns, r;
 
	ewsum = 0.,   nssum = 0.,   ewavg = 0.,   nsavg = 0.;
	ewsq = 0.,   nssq = 0.,   ewd = 0.,   nsd = 0.;
	ewns = 0.,   r = 0.;
 
	////////// Average //////////
	// mean //
 
	for (i=0; i<n1; i++)
	{
		ewsum += dat1[i];
	}
 
	for (i=0; i<n1; i++)
	{
		nssum += dat2[i];
	}
 
	ewavg = ewsum / n1;
	nsavg = nssum / n1;
 
	////////// Standard Deviation ///////////
	// amplitude //
 
	for (i=0; i<n1; i++)
	{
		ewsq += (dat1[i] - ewavg) * (dat1[i] - ewavg);
	}
 
	for (i=0; i<n1; i++)
	{
		nssq += (dat2[i] - nsavg) * (dat2[i] - nsavg);
	}
 
	ewd = sqrt (ewsq / n1);
	nsd = sqrt (nssq / n1);
 
 
	///////// crosscorrelation coefficient /////////
 
	for (i=0; i<n1; i++)
	{
		ewns += ((dat1[i] - ewavg) / ewd) * ((dat2[i] - nsavg) / nsd);
//		ewns += dat1[i]*dat2[i];
	}
 
	r = ewns / (float)n1;
 
	return r;
}
 
float CRMS(float *dat, long int start, long int end)
{
	int i;
	float point;
	float n, m, l;
 
	point = 0.;
	n = 0., m = 0., l = 0.;
 
	point = (float)end - (float)start;
 
	for (i=start; i<end; i++)
	{
		n += dat[i] * dat[i];
	}
 
	for (i=-end; i<-start; i++)
	{
		m += dat[i] * dat[i];
	}
 
	l = sqrt((n + m) / (point * 2.));
 
	return l;
}
익명 사용자의 이미지

오류가 유형별로 너무 다양하게 존재해서, 일일이 지적하기에는 한이 없군요. 미안합니다만, 총체적 난국이라고 할 수 있습니다.

gcc에 -Wall 옵션을 주어서 컴파일해 보시기 바랍니다. Warning나는 것부터 시작해서 하나 하나 잡아가시기 바랍니다.

질문하신 세그먼트 폴트나는 이유는 coee, coen, ..., coerms, ti 등 포인터가 메모리 할당되지 않은 상태에서 쓰였기 때문입니다. e1.npts나 e2.npts 으로부터 알맞은 크기를 계산해서 malloc()으로 메모리를 할당하시기 바랍니다.

그런데 이렇게 메모리 할당하도록 수정했다고 해도, 또 다른 오류가 많기 때문에 수정사항이 많습니다. 문법오류는 물론이고, 정확히 무얼 하는 것인지는 모르는 상태에서도 프로그램이 논리적으로 오류가 있게 짜여져 있습니다.

이걸 일일이 지적해가면서 수정해 주실 분이 과연 계실지 의문입니다. 그럴 시간도 없거니와, 호기심을 자극할만한 요소도 없기 때문에...
주변에 프로그램 좀 하는 친구가 있다면, 식사같은 걸 미끼로 도움을 요청해 보시기 바랍니다. 솔직히 난이도가 높은 프로그램은 아니기 때문에, 같히 협력하면 잠깐 시간을 투자하면 오류도 수정하고, 프로그램 지식도 얻고 하실 수 있을 겁니다.

물론 시간을 들여서 계속해서 살펴보고 고민한다만, 스스로 문제를 해결할 수도 있을거라고 생각합니다. 그런데, 기초가 너무 안되있는거 같아 암담해 보입니다. 너무 죄송한 말만 늘어놓아서 죄송합니다.

mewestside의 이미지

지적 감사합니다... 문제는 해결 했습니다..
기초가 없는 상태에서 급하게 코딩을 하다보니 이런 문제가 생기네요..ㅠ
더 공부를 해야겠습니다...

댓글 달기

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