简体   繁体   English

C栈使用数组

[英]C stack using array

This is only a part of the program but I don't understand these codes why did we declare int* array in structure what does it mean? 这只是程序的一部分,但我不理解这些代码,为什么我们在结构中声明int *数组是什么意思? Also we generally use int or void before function type why did we write struct Stack * before create function and also what is the use of unsigned here? 同样,我们通常在函数类型之前使用int或void,为什么我们在创建函数之前编写struct Stack *,以及unsigned的用途是什么? Click the link to view the actual program. 单击链接以查看实际程序。 a link 一条链接

struct Stack {
    int top;
    unsigned capacity;
    int* array;
};

struct Stack* createStack(unsigned capacity) {
    struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
    stack->capacity = capacity;
    stack->top = -1;
    stack->array = (int*) malloc(stack->capacity * sizeof(int));
    return stack;
}

int main() {
    struct Stack* stack = createStack(100);
}

1) int * is used because we dont know before hand how huge the array is going to be, so we declare it as a pointer and do malloc using the capacity of the stack. 1)使用int *是因为我们事先不知道数组的大小,因此我们将其声明为指针,并使用堆栈的容量进行malloc。

If we had used an array, we had to perform additonal checks to see if our initial declared size was overflown and would be a huge overhead across the program. 如果使用数组,则必须执行附加检查,以查看初始声明的大小是否溢出,并且在整个程序中都将是巨大的开销。

2) The function create stack is returning the structure ponter of type stack, according to the syntax for functions. 2)函数创建堆栈根据函数的语法返回类型堆栈的结构体。

Syntas: 语法:

<return type> function_name(<Parameters>){<Statements>}

3) We want the program to give us an error if some one tries to declare a negative capacity. 3)如果有人试图声明负容量,我们希望程序给我们一个错误。 Unsigned ensures that that is satisfied 未签名可确保满足要求

Hope this helps. 希望这可以帮助。

why did we declare int* array in structure what does it mean? 为什么我们在结构中声明int *数组是什么意思?

The array member is used to store the contents of the stack. array成员用于存储堆栈的内容。 However, we don't know ahead of time how many elements the stack will need to hold, so we can't declare it as a regular array (whose size must be known at compile time 1 ). 但是,我们无法提前知道堆栈需要容纳多少个元素,因此我们无法将其声明为常规数组(必须在编译时1知道其大小)。 Instead, we'll allocate the memory at runtime using the malloc library function, which returns a pointer to the first element of the dynamically allocated block; 相反,我们将在运行时使用malloc库函数分配内存,该函数将返回指向动态分配的块的第一个元素的指针。 this pointer will be stored to the array member. 该指针将存储到数组成员。

why did we write struct Stack * before create function 我们为什么要在创建函数之前编写struct Stack *

Because the createStack function is returning a pointer to a new struct Stack instance: 因为createStack函数正在返回一个指向新的struct Stack实例的指针:

struct Stack *createStack( ... )  ----------------+
{                                                 | The type of the expression in the 
  struct Stack *stack = ...; -----+               | `return` statement must match the  
  ...                             |               | return type of the function
  return stack; <-----------------+---------------+
}

what is the use of unsigned here 这里unsigned的用途是什么

unsigned is short for unsigned int ; unsignedunsigned int缩写; it guarantees that only non-negative values can be used for the stack size. 它保证仅非负值可用于堆栈大小。

Here's what memory looks like after the createStack function has been called: 这是调用createStack函数后的内存外观:

                   +---+
            stack: |   |----+  // The stack variable points to a struct Stack instance
                   +---+    |
                    ...     |
                   +---+    |
       stack->top: |   |<---+  // The actual struct Stack instance is created on the heap
                   +---+       // The -> operator allows us to refer to the members of the
  stack->capacity: |   |       // instance through the stack pointer variable.
                   +---+       
     stack->array: |   |----+  // The memory for the stack contents is allocated in a 
                   +---+    |  // separate malloc call, and the resulting pointer is
                    ...     |  // stored in the instance's array member.  
                   +---+    |
  stack->array[0]: |   |<---+
                   +---+
  stack->array[1]: |   |
                   +---+
  stack->array[2]: |   |
                   +---+
                    ...
                   +---+
stack->array[N-1]: |   |    // N == stack->capacity
                   +---+

The stack variable in main points to an instance of struct Stack ; mainstack变量指向struct Stack的实例; this instance is created by the line 该实例由以下行创建

struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));

in the createStack function. createStack函数中。 The memory for the stack instance is taken from the "heap" (a region of memory reserved for dynamic allocation). 堆栈实例的内存来自“堆”(为动态分配保留的内存区域)。 Within the stack instance, the array member also points to a region of dynamic memory reserved by another call to malloc : 在堆栈实例中, array成员还指向动态内存区域,该区域由另一个对malloc调用保留:

stack->array = (int*) malloc(stack->capacity * sizeof(int));

This function sets aside enough memory to store as many int objects as specified in the capacity member. 此函数预留了足够的内存来存储capacity成员中指定的多个int对象。

Edit 编辑

Note that both malloc calls can be cleaned up as follows: 请注意,两个malloc调用可以按以下方式清理:

struct Stack *stack = malloc( sizeof *stack );
...
stack->array = malloc( stack->capacity * sizeof *stack->array );

Unless you're using a C++ compiler or a C compiler that predates the 1989 standard, the cast is unnecessary (and on C89 implementations, dangerous). 除非您使用的C ++编译器或早于1989年标准的C编译器,否则强制类型转换是不必要的(在C89实现中很危险)。

The sizeof expressions use the dereferenced target expression, rather than a type name; sizeof表达式使用解引用的目标表达式,而不是类型名称。 this cleans up a little of the visual clutter, and it reduces maintenance if you ever decide to change the type of the target variable (from int * to double * , for example). 如果您决定更改目标变量的类型(例如,从int *变为double * ,那么这将消除一些视觉混乱,并减少维护工作。 The type of the expression *stack is struct Stack , so it follows that sizeof *stack == sizeof (struct Stack) . 表达式*stack的类型是struct Stack ,因此它遵循sizeof *stack == sizeof (struct Stack) Similarly, the type of the expression *stack->array is int , so sizeof *stack->array == sizeof (int) . 同样,表达式*stack->array的类型为int ,因此sizeof *stack->array == sizeof (int) Note that parentheses are only required if the operand of sizeof is a type name. 请注意,仅当sizeof操作数为类型名称时才需要括号。


1. C99 introduced variable-length arrays whose size can be specified at runtime, but they won't work in this context for several reasons. 1. C99引入了可变长度数组,其大小可以在运行时指定,但是由于多种原因,它们在这种情况下不起作用。

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

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