在 Windows 中的 vim 编辑器中运行时程序输出发生变化
下面的程序是用C语言返回的。我不知道为什么当程序相同但编辑器不同时输出会改变。
#include<stdio.h>
#include<stdlib.h>
struct Queue{
int tail,head,capacity,size;
int* array;
};
struct Queue* create_queue(int capacity){
struct Queue *queue = (struct Queue*)malloc(sizeof(struct Queue*));
queue->capacity = capacity;
queue->head = 0;
queue->tail = 0;
queue->size = 0;
queue->array = (int*)malloc(capacity * sizeof(int));
return queue;
}
int isFull(struct Queue* q){
return q->size == q->capacity;
}
int isEmpty(struct Queue* q){
return q->size == 0;
}
void enqueue(struct Queue* q,int data){
if (isFull(q)){
return;
}
else{
q->array[q->tail++] = data;
q->size = q->size + 1;
}
}
int dequeue(struct Queue* q){
if (isEmpty(q) || (q->tail == q->head) || q->tail < q->head){
return -1;
}
else{
return q->array[q->head++];
}
}
int main(){
struct Queue *qptr = create_queue(100);
printf("Queue of capacity %d is created.\n",qptr->capacity);\
enqueue(qptr,20);
enqueue(qptr,30);
enqueue(qptr,40);
printf("The dequeued element is: %d\n",dequeue(qptr));
return 0;
}
下面是在 Vim 中构建和运行 C 文件的键映射供您参考。是否需要更改此映射,我想知道是否有。
autocmd filetype c nnoremap <F2> :w <CR> :!gcc % -o %:r -Wl,--stack,268435456<CR>
autocmd filetype c nnoremap <F3> :!%:r<CR>
Vim 输出:
Queue of capacity -499734755 is created.
shell returned -1073741819
代码块输出:
Queue of capacity 100 is created.
The dequeued element is: 20
我该如何解决这个问题?
The program below is return in C. I have no idea why the output is changing when the program is same but the editor is different.
#include<stdio.h>
#include<stdlib.h>
struct Queue{
int tail,head,capacity,size;
int* array;
};
struct Queue* create_queue(int capacity){
struct Queue *queue = (struct Queue*)malloc(sizeof(struct Queue*));
queue->capacity = capacity;
queue->head = 0;
queue->tail = 0;
queue->size = 0;
queue->array = (int*)malloc(capacity * sizeof(int));
return queue;
}
int isFull(struct Queue* q){
return q->size == q->capacity;
}
int isEmpty(struct Queue* q){
return q->size == 0;
}
void enqueue(struct Queue* q,int data){
if (isFull(q)){
return;
}
else{
q->array[q->tail++] = data;
q->size = q->size + 1;
}
}
int dequeue(struct Queue* q){
if (isEmpty(q) || (q->tail == q->head) || q->tail < q->head){
return -1;
}
else{
return q->array[q->head++];
}
}
int main(){
struct Queue *qptr = create_queue(100);
printf("Queue of capacity %d is created.\n",qptr->capacity);\
enqueue(qptr,20);
enqueue(qptr,30);
enqueue(qptr,40);
printf("The dequeued element is: %d\n",dequeue(qptr));
return 0;
}
Below is the key mapping to build and to run a C file in Vim for your reference. Is there any need to change this mapping , I would like to know if any.
autocmd filetype c nnoremap <F2> :w <CR> :!gcc % -o %:r -Wl,--stack,268435456<CR>
autocmd filetype c nnoremap <F3> :!%:r<CR>
Vim output:
Queue of capacity -499734755 is created.
shell returned -1073741819
Codeblocks output:
Queue of capacity 100 is created.
The dequeued element is: 20
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(struct Queue*)malloc(sizeof(struct Queue*));
很可疑。我怀疑第二个*
。习惯用法是
struct Queue *queue = malloc(sizeof(*queue));
。即没有强制转换,也没有重复数据类型。您的 malloc 太小,这意味着您的代码正在访问超出合法分配的内存。
你只是“幸运”(或不幸,取决于你是否喜欢隐藏的错误),这种未定义的行为只会在不同的环境中导致不同的输出。
(struct Queue*)malloc(sizeof(struct Queue*));
is fishy. I doubt the second*
.The idiom would be
struct Queue *queue = malloc(sizeof(*queue));
.I.e. no casting and no repeating of datatype. What you malloc is too small, which means that your code is accessing beyond legally allocated memory.
You are only "lucky" (or unlucky, depening on whether you like hidden errors or not), that this undefined behaviour only causes different output in different environments.