세그멘테이션 오류가 떠요..

liliyse의 이미지

제가 보기엔 어디가 문제인지 잘 모르겠는데...
세그멘테이션 오류가 뜨네요..
어디가 잘 못된걸까요? ㅠㅠ

파일 첨부해 놓을게요..

#include
#include
void func_print();
void SJF();
void func_sort();

struct process{

char name[10];
int arrive;
int running;
int run;
int order;
int processNo;
int turnaround;
int waiting;

};

struct process* queue;

int n=0;
int total=0;
int quantum;
struct process tmp;

void func_print(){

int* gantt = (int*)malloc(sizeof(int)*total);
int i=0, turnaround=0, waiting=0;

for (i=0,turnaround=0,waiting=0; i turnaround+=queue[i].turnaround;
waiting+=queue[i].waiting;
}

printf("\n 1. Gantt Chart\n");
printf (" ");
for (i=0; i printf ("%d",gantt[i]);

printf("\n 2. Average turnaround time : %d/%d = %f", turnaround,n,(float)turnaround/n);
printf("\n 3. Average waiting time : %d/%d = %f", waiting,n,(float)waiting/n);
printf("\n\n");

}

void SJF(){
//SJF
int* gantt=(int*)malloc(sizeof(int)*total);
int b, i=0, a=0;

while (a while ( queue[a].running >0 ){
gantt[i]=queue[a].processNo;
queue[a].running--;
i++;
queue[a].turnaround = i-queue[a].arrive;
queue[a].waiting = queue[a].turnaround - queue[a].run;
}
a++;
for (b=a+1; b if (queue[b].arrive <= i && queue[a].running > queue[b].running){
tmp=queue[b];
queue[b]=queue[a];
queue[a]=tmp;
}
}

func_print();
}

void func_sort(){

int a, b;
for (a=0; a for (b=a+1; b if (queue[a].arrive > queue[b].arrive){
tmp=queue[a];
queue[a]=queue[b];
queue[b]=tmp;
}
}

int main(){

int i;

printf(" 몇개의 프로세스를 입력 하시겠습니까? : ");
scanf("%d",&n);

struct process* queue = (struct process*)malloc(sizeof(struct process)*n);

printf(" 프로세스 이름, 도착시간, 실행시간, 우선순위를 순서대로 입력하세요\n");

for (i=0; i

queue[i].processNo=i+1;
printf(" %d 번째 프로세스 정보 입력 : ",queue[i].processNo);
scanf("%s %d %d %d",
queue[i].name, &queue[i].arrive, &queue[i].running,&queue[i].order);
queue[i].run = queue[i].running;
}

printf(" Time Quantum을 입력하세요 : ");
scanf("%d", &quantum);

printf("\n 다음이 프로세스 넘버와 프로세스 이름입니다.\n");
printf(" 간트차트는 프로세스 넘버가 출력됩니다.\n");

for (i=0; i printf(" %d : %s ",queue[i].processNo, queue[i].name);

for (i=0; i total+=queue[i].running;

printf("\n********** Shortest Job First **********\n"); //SJF

func_sort();
SJF();

return 0;

}

File attachments: 
첨부파일 크기
Package icon what.zip1.09 KB
biiiiid의 이미지

여러가지 문제가 있을 것으로 보입니다만, 일단 당장 오류가 나는 근본적인 문제는

전역변수로 선언한 queue와 main에서 다시 선언한 queue가 있는데,

값이 할당된 queue는 main에서 선언한 것이고,

sort함수에서 보는 queue는 전역에서 선언된 queue이기때문에,

sort함수에서 할당되지 않은 메모리를 건드리고 있습니다..

전역변수 사용을 줄이고, 함수를 잘 설계하시길..

댓글 달기

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