2016-11-18 11 views
0

スタックが動作するようにスタックができないようです。それは正しくコンパイルされているようですが、プッシュとポップアップの機能は正しく書かれていると理解していますが、間違っている可能性があります。スタックが正しい値をポップしていない:C

スタックに2つの整数を押し込み、テストするためにそれらを再びポップしようとしましたが、整数形式のメモリアドレスと思われるものがポップアウトされますが、これは場合。いずれにしても、正しい値がポップされているわけではないので、コードでは何もわかりません。

ポップされた値はいくつかの反復で変化しないように見えることが重要ですが、私はmalloc呼び出しがこれを防ぐと思います。構造体がdefs.hにここに格納され

#include "defs.h" 
//Initialising the stack 
TopStack* initTOS() 
{ 
    TopStack* pTopStack; 
    pTopStack = (TopStack*)malloc(sizeof(TopStack)); 
    return (pTopStack); 
} 

//Pushing an element onto the stack 
void push(TopStack* ts, int val) 
{ 
    if (ts->num == 0) { 
     Stack* pNewNode; 
     pNewNode = (Stack*)malloc(sizeof(Stack)); 
     pNewNode->val = val; 
     pNewNode->next = NULL; 
     ts->top = pNewNode; 
     ts->num++; 
    } 
    else if (ts->num != 0) { 
     Stack* pNewNode; 
     pNewNode = (Stack*)malloc(sizeof(Stack)); 
     pNewNode->val = val; 
     pNewNode->next = ts->top; 
     ts->top = pNewNode; 
     ts->num++; 
    } 
} 

int pop(TopStack* ts) 
{ 
    if (ts->num == 0) { 
     printf("Can't pop, stack is empty!\n"); 
     exit(1); 
    } 
    else { 
     Stack* pTemp; 
     int RemovedValue; 
     RemovedValue = pTemp->val; 
     ts->top = pTemp->next; 
     ts->num--; 
     free(pTemp); 
     return (RemovedValue); 
    } 
} 

void testStack(TopStack* ts) 
{ 
    int RemovedValue; 
    push(ts, 1); 
    push(ts, 2); 
    printf("the popped value was %i\n", pop(ts)); 
    printf("the popped value was %i\n", pop(ts)); 
} 

#include <stdio.h> 
#include <stdlib.h> 
#include <ctype.h> 
#include <assert.h> 
#include <stdbool.h> 

#define MAX_EXPR 50 

//struct that contains stack's element 

typedef struct stack_elem { 
    int val; 
    struct stack_elem* next; 
} Stack; 

//struct that contains the pointer to the top of the stack 

typedef struct { 
    int num; //num of elements in stack 
    Stack* top; 
    ; //top of stack 
} TopStack; 

//ts=pointer to the top of stack, val=element to push 

void push(TopStack* ts, int val); //push element on the stack 

//prints the elements in the stack 

void printStack(TopStack* ts); 

// initialize the structure that will point to the top of the stack 

TopStack* initTOS(); 

// a simple test for the stack 

void testStack(TopStack* ts); 

// ts=pointer to the top of stack 

int pop(TopStack* ts); //returns element from top of stack 
// simple parser function for RPN expressions that assumes numbers have only one digit 

void parseRPN(char expr[], TopStack* st); 

// empties the stack using the pop operation 

void emptyStack(TopStack* ts); 

// performs the operation defined by character op on the elements on top of stack 

void performOp(TopStack* st, char op); 

、最終的にはmain.cのファイル:

#include "defs.h" 

int main() 
{ 
    TopStack* tp; 

    tp = initTOS(); // initialize the top of stack structure 
    testStack(tp); // this function tests your stack 
    return EXIT_SUCCESS; 
} 
+0

ここで、ts-> numを初期化していますか? – schil227

答えて

1

pTemp機能では、popは未初期化時に使用されます。自動保存期間を持つ初期化されていない変数の値を使用すると、の未定義の動作が呼び出されます。

ライン

Stack *pTemp; 

あなたはinitTOS()ゼロにpTopStack->numを初期化する必要があります、またはあなたが経由で割り当てられたバッファ内の値を使用するための未定義の動作を再起動します。また

Stack *ptemp = ts->top; 

する必要がありますmalloc()、初期化されていません。

もう1つの注意点は、you shouldn't cast the result of malloc() in Cです。

1
以下

はlib.cファイルです

A bigおよびメジャー問題mallocでメモリを割り当てると、メモリが何らかの形で初期化されないということです。メモリの内容は、不定です。あなたはinitTOS機能にTopStackためのメモリを割り当てる際、構造topポインタが最も可能性が高いNULLなどnumがゼロでないことがないことを意味します

。これらの値を使用すると、の定義されていない動作が発生します。

メモリをinitTOSに初期化する必要があります。代わりにcallocを使用するか、明示的に各メンバーを設定してください。

他の初期化されていない変数やデータにも問題があるようです。

関連する問題