简体   繁体   English

char *中的C ++错误的PTR(无法评估表达式)

[英]C++ bad PTR in char* (Expression cannot be evaluated)

I've been searching for quite a time for an answer, although there were similar problems I still couldn't improve my code so it would work. 我一直在寻找答案的时间已经很长了,尽管存在类似的问题,但我仍然无法改善我的代码,因此它可以正常工作。 I have a simple lifo structure to which I am trying to add one element and print the structure. 我有一个简单的lifo结构,尝试向其中添加一个元素并打印该结构。 It prints nothing and when I am debbuging I have this <bad ptr> in char * nameOfVariable . 它不打印任何内容,当我调试时, <bad ptr> in char * nameOfVariable有这个<bad ptr> in char * nameOfVariable

I would appreciate any help! 我将不胜感激任何帮助! Here is my source code: 这是我的源代码:

#include<stdio.h>

struct Variable 
{ 
    double value; 
    char *name;
    struct Variable *next; 
} *variables[80000];

void pop(Variable * head);
void push(Variable * head, char *name, double value);
void show(Variable * head);


int main(){


for(int i = 0; i <80000; i++){
    variables[i] = nullptr;

}

char *nameOfVariable = "aaab";
double value = 5;
push(variables[0], nameOfVariable, value );
show(variables[0]);


system("pause");
return 0;
}
void push(Variable *  head, char *name, double value)
{
    Variable * p ;

    p = head;

    head = new Variable;
    head -> name = name;
    head -> value = value;
    head -> next = p;

}


void pop(Variable *  head)
{
    Variable *p; 

    if (head != NULL) 
    { 
       p = head; 
       head = head -> next; 
       free(p); 
    } 
}
void show(Variable * head)
{
Variable *p; 

p = head; 
    while (p!=NULL){
         printf("%c %f ", p->name, p->value);
         p=p->next;
}
printf("\n");
}

PS - I cant use STL so string is not an option :) PS-我不能使用STL,所以不能使用字符串:)

You do not save the variable you created in push so they all get lost 您不会保存在push中创建的变量,因此它们都会丢失

void push(Variable *  head, char *name, double value) {
  Variable * p ;
  p = head;

  head = new Variable;
  head -> name = name;
  head -> value = value;
  head -> next = p;

}

When the function enters head points to null. 函数输入时,头指向空。

in head = new Variable; head = new Variable; head now points to a newly created variable on the heap head现在指向堆上新创建的变量

when the function exits no one keeps track of the newly created variable on the heap. 当函数退出时,没有人跟踪堆上新创建的变量。 The memory is leaked and there is no way to access that element. 内存泄漏,无法访问该元素。

NOTE: You should be aware that Changes you write to head in the function push do not affect variables[0] you passed to the function. 注意:您应该意识到,在函数推入中写入head更改不会影响传递给函数的variables[0] variables[0] is pointer to a Variable somewhere. variables [0]是某个地方的变量的指针。 Initially it is nullptr meaning it does not point at anything. 最初它是nullptr表示它什么都没有指向。 head is a copy of variables[0] that means a different pointer that happens to point at the same place in memory (in your case nullptr ). head是variables[0]的副本,表示variables[0]的另一个指针恰好指向内存中的同一位置(在您的情况下为nullptr )。 That means though that if you change head it points at something else and is no longer pointing to the same object as variables[0] 这意味着尽管您改变了head但它指向了别的东西,并且不再指向与variables[0]相同的对象。

Suggested Changes: 建议的更改:

  • Make push a function that returns a Variable* to the caller. 强制执行一个将Variable *返回给调用方的函数。 Which is the new head. 这是新头。
  • Make push a function that accepts a Variable*& as an in/out parameter and returns the new head in that 将push设为接受Variable *&作为输入/输出参数并在其中返回新头的函数
  • (My preference) create a deque struct that holds a Variable* head memeber. (我的喜好)创建一个拥有Variable *头部成员的双端队列结构。 pass a deque* to all these functions (push/pop) and in these functions manage the memory 向所有这些功能(推/弹出)传递双端队列*,并在这些功能中管理内存

You are storing a pointer into a parameter location: 您正在将指针存储到参数位置:

void push(Variable *  head, char *name, double value)
{
Variable * p ;
p = head;
head = new Variable;

But the parameter location is local to the function and discarded upon return. 但是参数位置对于函数而言是本地的,并且在返回时将其丢弃。

Why do you allocate an array of 80000 elements? 为什么要分配80000个元素的数组?

In order to change a location by a function you must either pass the address of that location (a Variable** head in your case) or use a reference. 为了通过功能更改位置,您必须传递该位置的地址(在您的情况下为Variable** head )或使用引用。

Much better would be the definition of a class for your stack... 更好的方法是为您的堆栈定义一个类...

And another one: storing a variable's name as a char* will almost certainly cause trouble later on. 还有一个:将变量名存储为char*几乎肯定会在以后引起麻烦。 Prepare for memory allocation of a char[] and copy the name string. 准备分配char[]内存并复制名称字符串。

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

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