简体   繁体   中英

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. It prints nothing and when I am debbuging I have this <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 :)

You do not save the variable you created in push so they all get lost

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 now points to a newly created variable on the heap

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. variables[0] is pointer to a Variable somewhere. Initially it is nullptr meaning it does not point at anything. 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 ). 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]

Suggested Changes:

  • Make push a function that returns a Variable* to the caller. 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
  • (My preference) create a deque struct that holds a Variable* head memeber. 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?

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.

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. Prepare for memory allocation of a char[] and copy the name string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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