Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 指针未指向NULL时的访问冲突_C++_Pointers_Error Handling_Runtime_Access Violation - Fatal编程技术网

C++ 指针未指向NULL时的访问冲突

C++ 指针未指向NULL时的访问冲突,c++,pointers,error-handling,runtime,access-violation,C++,Pointers,Error Handling,Runtime,Access Violation,这段小代码面临rum时间错误,称为“访问冲突”。 我在网上读到过一篇文章,上面说当指针指向NULL时,它的值是不能改变的。 但这并没有解决问题 #include<iostream> using namespace std; int max=3; struct node { char *ptr; }*start, *current = start; void init() { current->ptr = (char*)malloc(max*sizeof(cha

这段小代码面临rum时间错误,称为“访问冲突”。 我在网上读到过一篇文章,上面说当指针指向NULL时,它的值是不能改变的。 但这并没有解决问题

#include<iostream>
using namespace std;
int max=3;
struct node
{
    char *ptr;
}*start, *current = start;

void init()
{
    current->ptr = (char*)malloc(max*sizeof(char)); //ERROR IN THIS LINE. 
}
void add(char page)
{
    char *c;
    c = current->ptr;
        if (page == *c)
            cout << "Yes";
        else
            cout << "No";
}
void main()
{
    init();
    add('a');
    cin >> max;
}
#包括
使用名称空间std;
int max=3;
结构节点
{
char*ptr;
}*开始,*电流=开始;
void init()
{
当前->ptr=(char*)malloc(max*sizeof(char));//此行出错。
}
无效添加(字符页)
{
char*c;
c=当前->ptr;
如果(第==*c页)
cout-max;
}
这个

不创建节点结构,仅创建指针。因此“当前”未初始化。 改为

struct node
{
    char *ptr;
}start, *current = &start;
或在main()或init()中添加正确的内存分配

current = (node*)malloc(sizeof(node));

发生代码转储是因为您正在访问未初始化的指针。 “current”应该在尝试取消引用之前初始化为有效的内存位置。 更干净的代码将是

#include<iostream>
using namespace std;
int max=3;
struct node
{
    char *ptr;
};

struct node* start = new node();
struct node* current = start;

void init()
{
    current->ptr = (char*)malloc(max*sizeof(char)); //ERROR IN THIS LINE. 
}
void add(char page)
{
    char *c;
    c = current->ptr;
        if (page == *c)
            cout << "Yes";
        else
            cout << "No";
}
void main()
{
    init();
    add('a');
    cin >> max;
}
#包括
使用名称空间std;
int max=3;
结构节点
{
char*ptr;
};
结构节点*开始=新节点();
结构节点*当前=开始;
void init()
{
当前->ptr=(char*)malloc(max*sizeof(char));//此行出错。
}
无效添加(字符页)
{
char*c;
c=当前->ptr;
如果(第==*c页)
cout-max;
}

您需要使指针指向可以写入的内容。目前,无论出于何种目的,它们都指向随机位置。@juanchopanza当我让指针指向动态分配的空间时,它不会指向该位置的垃圾值吗?不会,它会指向可以写入的动态分配内存段。
current
在这里不为空。它有一个未确定的值。我不确定,但我认为有一个GCC选项可以设置全局内存是否用零填充。哎呀,我没有意识到变量是全局的。对不起!指针是根据C++标准初始化的,所以你的第一个答案是正确的。My bad.current=(node*)malloc(sizeof(node));可以,但我需要一个char指针指向节点中malloc分配的内存的第一个字节(第一个char)或
char*char\u pointer=(char*)current
你能帮我澄清这个概念吗:指针总是指向一个值。如果我们使用malloc,它将分配一个空间并使指针引用该空间。否则,指针可以指向其他地方。那么node*start=newnode()的意义是什么;特别是NEW NODE()。未初始化的指针指向可以是任何内容的位置。当您试图读取或写入您无权访问的内存时,访问该内存将导致程序coredump。通过new node(),我们从堆中分配内存并将其分配给ptr。
#include<iostream>
using namespace std;
int max=3;
struct node
{
    char *ptr;
};

struct node* start = new node();
struct node* current = start;

void init()
{
    current->ptr = (char*)malloc(max*sizeof(char)); //ERROR IN THIS LINE. 
}
void add(char page)
{
    char *c;
    c = current->ptr;
        if (page == *c)
            cout << "Yes";
        else
            cout << "No";
}
void main()
{
    init();
    add('a');
    cin >> max;
}