質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

関数型プログラミング

関数型プログラミングとは、関数を用いて演算子を構築し、算出し、コンピュータプログラムを構成する枠組みです。

Q&A

解決済

1回答

1014閲覧

malloc関数を使うとプログラムが強制終了する

hama1185

総合スコア18

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

関数型プログラミング

関数型プログラミングとは、関数を用いて演算子を構築し、算出し、コンピュータプログラムを構成する枠組みです。

0グッド

0クリップ

投稿2020/05/18 13:26

前提・実現したいこと

C言語でスタックを用いて中置記法から逆ポーランド記法へ変換するプログラムを作成していました。

発生している問題・エラーメッセージ

initNode関数でのmalloc関数が4回目の使用時プログラムを強制終了してしまう。

該当のソースコード

C

1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4 5struct node{ 6 char data; 7 int priority; 8 struct node* next; 9}; 10 11struct list{ 12 struct node* head; 13}; 14 15struct node* initNode(char data); 16struct list* initList(char data); 17void push(struct list* plist, char data); 18void pop(struct list* plist); 19int returnStackPriority(struct list* plist); 20int returnInputCharPriority(char data); 21 22void main(){ 23 struct list* plist = NULL; 24 char inputFormula[30]; 25 int i; 26 27 printf("input your formula\n"); 28 scanf("%s", inputFormula); 29 for(i = 0;i < strlen(inputFormula); i++){ 30 printf("for %c\n", inputFormula[i]); 31 if(i == 0){ 32 plist = initList(inputFormula[i]); 33 } 34 else{ 35 while((plist -> head != NULL)&& 36 (returnStackPriority(plist) != 4)&& 37 (returnInputCharPriority(inputFormula[i]) <= returnStackPriority(plist))){ 38 printf("while pop start\n"); 39 pop(plist); 40 printf("while pop end\n"); 41 } 42 if(returnInputCharPriority(inputFormula[i]) == 1){ 43 //出力せずにポップ 44 printf("if pop start\n"); 45 pop(plist); 46 printf("if pop end\n"); 47 } 48 else{ 49 //プッシュ 50 printf("push start\n"); 51 push(plist, inputFormula[i]); 52 printf("push end\n"); 53 } 54 } 55 } 56 while(plist -> head != NULL){ 57 pop(plist); 58 } 59} 60 61struct node* initNode(char data){ 62 struct node* p = NULL; 63 printf("init node start\n"); 64 printf("p data %c\n",data); 65 p = (struct node*)malloc(sizeof(struct node*)); 66 if(p == NULL){ 67 printf("init not found\n"); 68 } 69 p -> data = data; 70 p -> priority = returnInputCharPriority(p-> data); 71 p -> next = NULL; 72 return p; 73} 74 75struct list* initList(char data){ 76 struct list* plist = NULL; 77 plist = (struct list*)malloc(sizeof(struct list*)); 78 plist -> head = initNode(data); 79 return plist; 80} 81 82void push(struct list* plist, char data){ 83 struct node* pNode = NULL; 84 printf("func push start\n"); 85 if(plist -> head == NULL){ 86 plist -> head = initNode(data); 87 pNode = plist -> head; 88 printf("push is %c\n",pNode->data); 89 } 90 else{ 91 pNode = plist -> head; 92 while(pNode -> next != NULL){ 93 pNode = pNode -> next; 94 } 95 pNode ->next = initNode(data);//なんかうまくいかない 96 printf("push is %c\n",pNode->next->data); 97 } 98} 99 100void pop(struct list* plist){ 101 struct node* previousNode = NULL; 102 struct node* pNode = NULL; 103 previousNode = plist -> head; 104 pNode = plist -> head; 105 while(pNode -> next != NULL){ 106 previousNode = pNode; 107 pNode = pNode -> next; 108 } 109 previousNode -> next = NULL; 110 if(pNode == plist -> head){ 111 plist -> head = NULL; 112 } 113 printf("pop is %c\n",pNode->data); 114 if((pNode->data != '(') && (pNode->data != ')')){ 115 printf("%c",pNode->data); 116 } 117 free(pNode); 118} 119 120int returnStackPriority(struct list* plist){ 121 struct node* pNode = NULL; 122 pNode = plist -> head; 123 while(pNode -> next != NULL){ 124 pNode = pNode -> next; 125 } 126 return pNode -> priority; 127} 128 129int returnInputCharPriority(char data){ 130 int priority; 131 if(data == '*' || data == '/'){ 132 priority = 3; 133 } 134 else if(data == '+' || data == '-'){ 135 priority = 2; 136 } 137 else if(data == '('){ 138 priority = 4; 139 } 140 else if(data == ')'){ 141 priority = 1; 142 } 143 else if(data == '='){ 144 priority = 0; 145 } 146 else{ 147 priority = 5; 148 } 149 return priority; 150}

試したこと

上記のように処理の要所要所にprint文を入れどこで停止しているか確認しました。
実行例を以下に示します。
A=(B-C)/D+E*Fを入力しました。
Bのところで動作が停止してしまいます。

input your formula A=(B-C)/D+E*F for A init node start p data A for = while pop start pop is A Awhile pop end push start func push start init node start p data = push is = push end for ( push start func push start init node start p data ( push is ( push end for B push start func push start init node start

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

気がついたところだけ

p = (struct node*)malloc(sizeof(struct node*));

ポインタのサイズだけ確保してますが、これではまずいのでは。

plist = (struct list*)malloc(sizeof(struct list*));

なんか全てポインタサイズだけですね

投稿2020/05/18 13:44

編集2020/05/18 13:46
y_waiwai

総合スコア87784

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hama1185

2020/05/18 13:51

ありがとうございます ポインタサイズだけ確保してましたね...無事変更したら想定した出力になりました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問