c程序在调试时出错-ntdll!
我试图通过使用函数覆盖无效指针来实现C中的整数。该代码在大多数情况下正常工作,但是当我释放分配的内存(包括堆栈对象和整数数组)时,会在调试时给出错误。我从c:\ windows \ system32 \ ntdll.dll.dll 获得此错误: - ntdll!我在Windows和Vscode上使用GCC。
stack.h: -
#ifndef STACK_H
#define STACK_H
void stack_init(void**);
void stack_push(void**,int);
void stack_pop(void**);
int stack_top(void*);
int stack_empty(void*);
int stack_size(void*);
void stack_delete(void**);
#endif
stack.c: -
#include"stack.h"
#include<stdlib.h>
typedef struct
{
int*arr;
int size;
int capacity;
int empty;
}stack;
void stack_init(void**self)
{
if(!*self)
{
*self=malloc(sizeof(stack));
((stack*)(*self))->size=0;
((stack*)(*self))->capacity=1;
((stack*)(*self))->arr=(int*)malloc(sizeof(int)*((stack*)(*self))->capacity);
((stack*)(*self))->empty=1;
}
return;
}
void _realloc(stack**self,int Buffer_size)
{
(*self)->capacity=Buffer_size;
int*new_array=(int*)malloc(sizeof(int)*Buffer_size);
for(int i=0;i<(*self)->size;i++)
new_array[i]=(*self)->arr[i];
free((*self)->arr);
(*self)->arr=new_array;
return;
}
void stack_push(void**self,int data)
{
if(*self)
{
if(((stack*)(*self))->size==((stack*)(*self))->capacity)
_realloc((stack**)self,((stack*)(*self))->capacity+((stack*)(*self))->capacity/2);
if(((stack*)(*self))->empty)
((stack*)(*self))->empty=0;
((stack*)(*self))->arr[((stack*)(*self))->size]=data;
((stack*)(*self))->size++;
}
return;
}
void stack_pop(void**self)
{
if(*self)
{
((stack*)(*self))->size--;
if(((stack*)(*self))->size==0)
((stack*)(*self))->empty=1;
}
return;
}
int stack_top(void*self)
{
if(self)
return ((stack*)self)->arr[((stack*)self)->size-1];
exit(0);
}
int stack_empty(void*self)
{
if(self)
return ((stack*)self)->empty;
exit(0);
}
int stack_size(void*self)
{
if(self)
return ((stack*)self)->size;
exit(0);
}
void stack_delete(void**self)
{
free((((stack*)(*self))->arr)); // The code works fine when this part is commented
free(*self);
return;
}
main.c: -
#include<stdio.h>
#include<stdlib.h>
#include"stack.h"
int main(int argc,char**argv)
{
void*stack=NULL;
stack_init(&stack);
stack_push(&stack,10);
stack_push(&stack,20);
stack_push(&stack,30);
stack_push(&stack,40);
printf("%d\n",stack_size(stack));
while(!stack_empty(stack))
{
printf("%d ",stack_top(stack));
stack_pop(&stack);
}
printf("\n");
stack_delete(&stack); // error
getc(stdin);
return 0;
}
更新:我在Visual Studio IDE中测试了此代码,并且该代码被检测到的 HEAP损坏崩溃错误,但我无法弄清楚出去。
I tried to implement a stack of integers in C by overriding a void pointer using functions. The code works fine for the most part, but gives the error on debugging when I free the allocated memory including the stack object and the integer array. I get this error :- ntdll!RtlRegisterSecureMemoryCacheCallback () from C:\WINDOWS\SYSTEM32\ntdll.dll. I use gcc on windows and vscode.
stack.h :-
#ifndef STACK_H
#define STACK_H
void stack_init(void**);
void stack_push(void**,int);
void stack_pop(void**);
int stack_top(void*);
int stack_empty(void*);
int stack_size(void*);
void stack_delete(void**);
#endif
stack.c :-
#include"stack.h"
#include<stdlib.h>
typedef struct
{
int*arr;
int size;
int capacity;
int empty;
}stack;
void stack_init(void**self)
{
if(!*self)
{
*self=malloc(sizeof(stack));
((stack*)(*self))->size=0;
((stack*)(*self))->capacity=1;
((stack*)(*self))->arr=(int*)malloc(sizeof(int)*((stack*)(*self))->capacity);
((stack*)(*self))->empty=1;
}
return;
}
void _realloc(stack**self,int Buffer_size)
{
(*self)->capacity=Buffer_size;
int*new_array=(int*)malloc(sizeof(int)*Buffer_size);
for(int i=0;i<(*self)->size;i++)
new_array[i]=(*self)->arr[i];
free((*self)->arr);
(*self)->arr=new_array;
return;
}
void stack_push(void**self,int data)
{
if(*self)
{
if(((stack*)(*self))->size==((stack*)(*self))->capacity)
_realloc((stack**)self,((stack*)(*self))->capacity+((stack*)(*self))->capacity/2);
if(((stack*)(*self))->empty)
((stack*)(*self))->empty=0;
((stack*)(*self))->arr[((stack*)(*self))->size]=data;
((stack*)(*self))->size++;
}
return;
}
void stack_pop(void**self)
{
if(*self)
{
((stack*)(*self))->size--;
if(((stack*)(*self))->size==0)
((stack*)(*self))->empty=1;
}
return;
}
int stack_top(void*self)
{
if(self)
return ((stack*)self)->arr[((stack*)self)->size-1];
exit(0);
}
int stack_empty(void*self)
{
if(self)
return ((stack*)self)->empty;
exit(0);
}
int stack_size(void*self)
{
if(self)
return ((stack*)self)->size;
exit(0);
}
void stack_delete(void**self)
{
free((((stack*)(*self))->arr)); // The code works fine when this part is commented
free(*self);
return;
}
main.c :-
#include<stdio.h>
#include<stdlib.h>
#include"stack.h"
int main(int argc,char**argv)
{
void*stack=NULL;
stack_init(&stack);
stack_push(&stack,10);
stack_push(&stack,20);
stack_push(&stack,30);
stack_push(&stack,40);
printf("%d\n",stack_size(stack));
while(!stack_empty(stack))
{
printf("%d ",stack_top(stack));
stack_pop(&stack);
}
printf("\n");
stack_delete(&stack); // error
getc(stdin);
return 0;
}
Update: I tested this code in Visual Studio IDE, and the code crashed with HEAP CORRUPTION DETECTED error, but I am not able to figure out how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题是由于
stack_push()使用时使用的表达式容量 + 容量/2的值
_realloc()
始终等于1。请注意,由于 apcatigt 是
int
,并且具有初始值1,因此表达式<的值< em>容量/2为0。即使调用
_RealLoc()
,初始堆栈容量也永远不会增加,因此functionstack_push()
访问尚未正确分配的内存区域。I think the problem is due to the fact that the value of the expression capacity + capacity/2, used by
stack_push()
when it calls_realloc()
, is always equal to 1.Notice that, since capacity is
int
and has initial value 1, the value of the expression capacity/2 is 0.The initial stack capacity never increases, even after calling
_realloc()
, so functionstack_push()
accesses areas of memory that have not been properly allocated.