我在代码块中编写的Stack实现代码是成功编译但在运行时显示错误

问题描述 投票:-1回答:2

我附上了运行时错误的屏幕截图。

此代码将创建一个堆栈

#include <stdio.h>
#include <stdlib.h>
#define SIZE 5         
// Declared a Structure with top,an array as its members

     struct Stack {  
     int items[SIZE],top;  

     };


//push method inserts an element in the stack

void push(struct Stack *s1,int x){   
    s1->items[++s1->top]=x;
}

// pop  method is for Removing the Elements

void pop(struct Stack* s1){  
    printf("Before pop the Top is %d ",s1->top);
    int a=s1->items[s1->top--];
    printf("After pop the Top is %d",s1->top);
}


//Main method


void main() {
    struct Stack s,*s1;
    struct Stack top = {-1};    //Initializing Top variable in Structure 
    int n;

    //loop to insert the elements

     for(int i=0;i<SIZE;i++){
     printf("Enter the elements to be pushed");
     scanf("%d",&n);
     push( s1, n);
      }
    pop(s1);
    pop(s1);

    printf("The Elements of the Stack after performing all the operations are   ");

    // Print the elements of the stack

    for(int i=0;i<SIZE;i++) {  
     printf("%d",s1->items[SIZE]); 

    }

}
c data-structures ide stack codeblocks
2个回答
1
投票

有很多问题:

这是您的程序的更正版本。所有更正都被评论。代码仍然很草率(例如,没有错误检查,变量名称没有意义),但至少它是有效的。

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

#define SIZE 5         

struct Stack {
  int items[SIZE];
  int top;             // better readability
};


// push method inserts an element in the stack

void push(struct Stack *s1, int x) {
  s1->items[++s1->top] = x;
}

// pop  method is for removing ONE Element  // COMMENT CORRECTED

int pop(struct Stack* s1) {   // we need to return the popped element
  printf("Before pop the Top is %d\n", s1->top);
  int a = s1->items[s1->top--];
  printf("After pop the Top is %d\n", s1->top);
  return a;
}


int main() {        // main must return an int
  struct Stack s;   // removed useless s1 ansd top variables
                    // BTW s1 was an uninitialized pointer so this was
                    // totally wrong
  int n;

  s.top = -1;       // initialize top correctly 
                    // struct Stack s = {-1}  will not initialize s.top !!

  for (int i = 0; i<SIZE; i++) {
    printf("Enter the elements to be pushed: ");
    scanf("%d", &n);
    push(&s, n);
  }

  printf("First popped value: %d\n", pop(&s));   // print popped elements
  printf("Second popped value: %d\n", pop(&s));

  printf("The Elements of the Stack after performing all the operations are: ");

  for (int i = 0; i <= s.top; i++) {   //i <= top instead of i < SIZE
    printf("%d\n", s.items[i]);        // s.items[i] instead of s.items[SIZE]
  }
}

0
投票

我将指出代码中的一些主要问题。编程时请注意这些。

push()和pop()方法似乎没问题。

您的程序成功符合,因为它没有任何语法错误。编译器无法识别运行时错误,例如由于内存管理,逻辑错误等引起的错误。因此,编译代码意味着您没有正确的答案。您需要使用打印件或其他内容调试每一行,以跟踪程序在崩溃之前的工作量。

在此程序中,由于内存管理问题,您会出现分段错误。因为在这里你使用指针s1作为push()和pop()函数的参数。但是你的指针s1没有用struct Stack变量初始化。因此,s1指向任意存储器位置并且不允许修改该存储器位置处的数据并导致分段错误。

您可以看到@Michael Walz在给出的代码中已经纠正了这个问题。

struct Stack s;     //This variable is statically allocated and has enough memory
struct Stack *s1;   //Pointer variable stores only address of the struct no memory
                    //for struct is allocated. Dynamic allocation is required

可以通过以下方式将内存分配给指针变量s1。

//Dyanmic memory allocation from heap
s1 = (struct Stack*)malloc(sizeof(struct Stack));

或者分配已分配变量的地址,如

struct Stack s, *s1;
s1=&s;

现在s1将指向有效的struct Stack对象,因此可以避免由于引用禁止的内存位置而导致的分段错误。

您编写的以下代码实际上并未初始化结构中的top变量

struct Stack top = {-1};

这将创建另一个名为top的Stack结构,并将数组item []中的第一个元素创建为-1。

要访问struct的元素,如下所示

struct Stack s;
struct Stack *s1; 
s.top = -1;      //Statically allocated. Referencing by '.'
s1->top = -1;    //Referencing by '->' for pointers

注意:一个很好的链接,建议如何进行struct初始化。这个链接提到了许多不同的可能性。很高兴看看。 http://en.cppreference.com/w/c/language/struct_initialization

© www.soinside.com 2019 - 2024. All rights reserved.