This is a question about the create function in the code that implements the C++ stack

Asked 1 years ago, Updated 1 years ago, 224 views

> struct _stack {
    //fill in here
    char* ele;
    int size;
    int top;
};


Stack create(int maxStackSize) {
    //Create an empty stack whose maximum size is maxStackSize
    Stack mathstack;
    mathstack->size = maxStackSize;
    mathstack->top = -1;
    mathstack->ele = (char*)malloc(maxStackSize * sizeof(char));
    return mathstack;
}

When you write the code like this, you receive a warning that you are using memory 'mathstack' that has not been initialized at runtime. So if you initialize 'mathstack=0', it doesn't work with the warning that you're referencing the NULL pointer 'mathstack'. Can you tell me what the problem is? It's not working because it's not created.ㅠ<

c

2022-10-03 01:00

1 Answers

Errors (errors) and warnings are different.

The warning does not necessarily need to be fixed in the learning code. An execution file is created even if there is a warning.

However, if there is an error, you need to fix the error. If there is an error, the executable file is not generated.

When you simply declare and use a variable (including a class variable), a warning appears that it uses uninitialized memory.

If there is no processing code for the Mallock function failing to operate when memory is allocated to the Mallock function, a warning message appears warning that it is referencing back.

Neither of the above alerts is a problem for execution. I think there is an error in another code.

It is separate from the above, but there is a problem with the code below.

Stack mathstack;
mathstack->size = maxStackSize;

In the code above, mathstack was declared a general variable The arrow operator (->) approaches the internal variable, which is used to access the internal variable of the pointer variable. The second line should use a point operator, not a pointer operator, as shown below.

mathstack.size = maxStackSize;


2022-10-03 01:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.