Tzu ng
Tzu ng

Reputation: 9234

Problem with Pointers in C++

char *str = NULL;
str = Operation(argv[1]);
cout << Cal(str);

Operation function return a pointer, but I really don't know why str in Cal is null. in line 2, it still has content. What am I missing?


char* Operation(char* name)
{
    fstream file(name,ios::in);
    char c;
    char stack[256]; int count_stack = -1;  
    char result[256]; int count_result = -1;
    while ( !file.eof() )
    {
        c = file.get();
        if ( c=='(')
        {
            count_stack++;
            stack[count_stack] = c ;
        }
        else if ( isOperand(c) )
        {
            count_result++;
            result[count_result] = c;
        }
        else if ( isOperator(c) )
            if ( isOperator( (stack[count_stack]) )>0 )
                if ( isOperator(c) > isOperator( stack[count_stack] ) )// dua ra so sanh khi trong stack co n>=1 ptu
                {
                    count_result++;
                    result[count_result] = c;
                }
                else
                {
                    count_result++;
                    result[count_result] = (stack[count_stack]);
                    stack[count_stack]= c;
                }
            else
            {
                count_stack++;
                stack[count_stack] = c ;
            }
        else if ( c==')') // Neu gap dau ngoac dong se lay het phan tu ra den khi gap ngoac mo
        {
            while( stack[count_stack] != '(')
            {
                count_result++; 
                result[count_result] = stack[count_stack];
                count_stack--;
            }
            count_stack--;
        }
    }
    while ( count_stack >=0 )
    {
        count_result++; 
        result[count_result] = stack[count_stack];
        count_stack--;
    }
    return &result[0];
}

This is the Operation Function . I'm really bad in pointer :P

Upvotes: 0

Views: 238

Answers (2)

Charles Salvia
Charles Salvia

Reputation: 53289

Well, firstly, in your Operation function you're returning a pointer to a temporary stack array. In Operation you declare char result[256] as a temporary stack variable. The 256 bytes you allocate here are ONLY valid for the duration of the Operation function. So using the pointer to result after Operation returns will cause undefined behavior.

To fix this, you have a few options:

1) You can allocate result on the heap, (using the new operator)

2) Even better, instead of returning a char* pointer, simply return an std::string object so you don't need to worry about allocating/deallocating memory.

3) Finally, if your program is single-threaded, you can also simply make char result[256] a static variable by declaring static char result[256]. This way, the pointer will be valid even after you return from Operation.

I would recommend option 2.

Upvotes: 2

dirkgently
dirkgently

Reputation: 111120

str is a pointer to a pointer. As you say, Operation returns a pointer (to what?). You ought to get a diagnostic (type mismatch).

Upvotes: 1

Related Questions