単語ソートプログラムについて

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
gray
記事: 6
登録日時: 12年前
住所: osaka

単語ソートプログラムについて

#1

投稿記事 by gray » 12年前

以下の英単語のソートプログラムをつくりました。
バブルソートを使いstrcmpで文字を比較して1を入力したら昇順に0を入力したら降順にソートされます。
自分で実行すると正常に動くのですがこれを大学のサイトに提出してみると
実行時エラー:メモリリーク, 領域アクセスが適切に行われているか確認して下さい
と返ってきます
いろいろと試したのですが一向に変わりません。
どうしたらいいでしょうか?

コード:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stddef.h>

#define N 128

typedef struct node{        
	char str[N];        
	struct node *next;       
}NODE;

int main(int argc, char *argv[]){
    NODE *topnode=NULL, *newnode=NULL, *thisnode, *nextnode;
    int order;
    char b[N];
    switch(atoi(argv[1])){
	 case 1: 
		 order=1;
		 break;
	 case 0:  
		 order=-1;
		 break;
	}
     while(1){
          if(fgets(b,N,stdin)==NULL) break;
        b[strlen(b)-1] = '\0';
          newnode=(NODE*)malloc(sizeof(NODE));
          newnode->next=NULL;
          strcpy(newnode->str,b);
		  
          if(topnode==NULL){
            topnode=newnode;        
          }
          else{
              thisnode=NULL;
              nextnode=topnode;

              while(nextnode!=NULL){
                   if(strcmp(b,nextnode->str)*order<0){
                     if(thisnode==NULL){
                        newnode->next=topnode;
                        topnode=newnode;
            	     }
                     else{
                         newnode->next=nextnode;
                         thisnode->next=newnode;                         
	                 }
	                  break;
                   }               
                    thisnode=nextnode;                    
	                nextnode=thisnode->next; 
	           }          
               if(nextnode==NULL){                
	             thisnode->next=newnode;      
               }              
          }
     }	  
      thisnode=topnode;
      while(thisnode!=NULL){
	  printf("%s\n",thisnode->str);  
	  thisnode=thisnode->next;
      }
       if(newnode !=NULL){  
           free(newnode);
           newnode=NULL;
     }
   return 0;          
}

よろしくお願いします。
最後に編集したユーザー gray on 2013年5月09日(木) 22:04 [ 編集 18 回目 ]

アバター
softya(ソフト屋)
副管理人
記事: 11677
登録日時: 15年前
住所: 東海地方
連絡を取る:

Re: 単語ソートプログラムについて

#2

投稿記事 by softya(ソフト屋) » 12年前

えーと、まずインデントが汚いので整理をお願いします。
それと問題は分かったのですが、問題が起きた時の入力値などを提示してください。
プログラムの簡単な仕様とか、使っているソートアリゴリズムも書いてもらったほうが良いでしょう。
コメントが皆無なので、解析の手間を減らすためにお願いします。
by softya(ソフト屋) 方針:私は仕組み・考え方を理解して欲しいので直接的なコードを回答することはまれですので、すぐコードがほしい方はその旨をご明記下さい。私以外の方と交代したいと思います(代わりの方がいる保証は出来かねます)。

usao

Re: 単語ソートプログラムについて

#3

投稿記事 by usao » 12年前

複数回mallocし得るのに対し,freeが一回しか行われないように見受けます.

box
記事: 2002
登録日時: 15年前

Re: 単語ソートプログラムについて

#4

投稿記事 by box » 12年前

別に私のコーディングスタイルを押しつけるつもりは毛頭ありませんが…

コード:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define N (128)
#define CRT SECURE NO WARNING   // 意味不明
 
typedef struct node {
    char str[N];
    struct node *next;
} NODE;

int main(int argc, char *argv[])
{
    NODE *topnode = NULL, *newnode = NULL, *thisnode, *nextnode;
    int order, i = 0;
    char b[N];
    
    switch(*(argv[i]+1)) {
    case '1' :
        order = 1;
        break;
    case '0' :
        order = -1;
        break;
  }
    
    while (1) {
        printf("文字列?=\n");
       if (fgets(b, N, stdin) == NULL) break;
        b[strlen(b)-1] = '\0';
        strtok(b, "\n");
        newnode = (NODE *)malloc(sizeof(NODE));
        newnode->next = NULL;
        strcpy(newnode->str, b);
        if (topnode == NULL) {
            topnode = newnode;
        }
        else {
            thisnode = NULL;
            nextnode = topnode;
            while (nextnode != NULL) {
                if (strcmp(b, nextnode->str) * order < 0) {
                    if (thisnode == NULL) {
                        newnode->next = topnode;
                        topnode = newnode;
                    }
                    else {
                        newnode->next  = nextnode;
                        thisnode->next = newnode;
                    }
                    break;
                }
                thisnode = nextnode;
                nextnode = thisnode->next;
            }
            if (nextnode == NULL) {
                thisnode->next = newnode;
            }
        }
    }
    
    thisnode = topnode;
    while (thisnode != NULL) {
        printf("%s\n", thisnode->str);
        thisnode = thisnode->next;
    }
    if (newnode) {
        free(newnode);
        newnode = NULL;
    }
    return 0;
}
せめてこのくらいには書いてほしいなぁ、と思います。ちなみにロジックには一切手を触れていませんので、
ちゃんと動くかどうかはわかりません。
もとのコードは、インデントがぐっちゃんぐっちゃんで、あまり読みたくないです。

ところで、「メモリーリークが発生している」ことをどのように検知したのでしょうか。
バグのないプログラムはない。
プログラムは思ったとおりには動かない。書いたとおりに動く。

閉鎖

“C言語何でも質問掲示板” へ戻る