简体   繁体   English

zsh:总线错误。/array_stack 关于使用指针访问堆栈元素。 欢迎任何线索

[英]zsh: bus error ./array_stack on using pointers to access the stack elements. Any leads are welcome

the code run fine when I used the dot operator for accessing the elements of the stack.当我使用点运算符访问堆栈的元素时,代码运行良好。 But on using pointers to access the stack structure, I was getting this error.但是在使用指针访问堆栈结构时,我收到了这个错误。 zsh: bus error./array_stack. zsh:总线错误。/array_stack。 Can anyone help me in resolving this.The code is as follows:谁能帮我解决这个问题。代码如下:

 #include<iostream>

 using namespace std;
//implementing the stack using an array

struct Stack{
int size;//size of the array
int top;//points to the last element of the array
int * arr;

};

int IsEmpty(struct Stack *ptr){
if(ptr->top == -1){
    return 1;
}
else{
    return 0;
}

 }

 int main(){
//one way of making the stack
// struct Stack S;
// S.size=43;
// S.top=-1;
// S.arr=(int *)malloc(S.size*sizeof(int));

//second way of making the stack
struct Stack *S;
S->size=54;
S->top=-1;
S->arr=(int *)malloc(S->size*sizeof(int));
if(IsEmpty(S)){
    cout<<"The array is empty.";
} 
return 0; 

}

EDIT: This question was originally tagged as C (not C++ ).编辑:这个问题最初被标记为C (不是C++ )。 I'm still not sure which way it will go.我仍然不确定它会以哪种方式 go。 I will modify this answer to use C++ when it's clear.我将修改此答案以在清楚时使用 C++ 。

The problem is you're defining S as a pointer to a Stack , but you never tell it to point to anything.问题是您将S定义为指向 Stack的指针,但您从未告诉它指向任何东西。 So S is pointing to somewhere in memory (you probably don't own), and the S->size = 54 is trying to modify the content of that memory by writing 54 .所以S指向 memory 中的某个地方(你可能不拥有),并且S->size = 54试图通过写入54来修改 memory 的内容。

The fact that the code is generating a " Bus Error " indicates that S actually contains a memory address that is outside of valid memory range.代码生成“ 总线错误”的事实表明S实际上包含一个超出有效 memory 范围的 memory 地址。

To fix it, S should be pointing at a stack:要修复它, S应该指向一个堆栈:

struct Stack myStack;         // myStack is an actual stack
struct Stack *S = &myStack;   // a pointer to myStack

Obviously this method creates a Stack that is uninitialised.显然,此方法创建了一个未初始化的Stack A nice way to do this sort of thing is to make a function that creates a new Stack , initialises it, then returns a pointer to it:做这种事情的一个好方法是创建一个 function 来创建一个新的Stack ,初始化它,然后返回一个指向它的指针:

struct Stack *createStack( int capacity )
{
    assert( capacity > 0 );

    struct Stack *new_stack = malloc( sizeof( struct Stack ) );
    if ( new_stack != NULL )
    {
        new_stack->size = capacity;
        new_stack->top  = -1;
        new_stack->arr  = malloc( capacity * sizeof( int ) );  

        // If we failed to allocate array memory: clean-up & return NULL
        if ( new_stack->arr == NULL )
        {
            free( new_stack );
            new_stack = NULL;
        }
    }

    return new_stack;
}

Then in your code, it can be called:然后在您的代码中,可以调用它:

struct Stack *s = createStack( 54 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM