anything wrong when using pop function? 
Author Message
 anything wrong when using pop function?

std::queue<char*> queStr;

char* pPushMsg = "test";
char* pPopMsg;
queStr.push(pPushMsg );
popItem(pPopMsg);

void popItem(char* msg)
{
    msg = queStr.front();
    queStr.pop();

Quote:
}

I ran the above code in debug mode, and found correct value in msg inside
function popItem, but when it jumped out of the function, the value stored
in pPopMsg became wrong. why?


Fri, 10 Oct 2003 16:42:21 GMT  
 anything wrong when using pop function?
Aviva

The std::queue.front is return a reference from the queue. That mean the
"msg" is pointed to the object reference. After the queue.pop executed, the
object is no longer exist. So the value of pPopMsg is became invalid.

Andre


Quote:
> std::queue<char*> queStr;

> char* pPushMsg = "test";
> char* pPopMsg;
> queStr.push(pPushMsg );
> popItem(pPopMsg);

> void popItem(char* msg)
> {
>     msg = queStr.front();
>     queStr.pop();
> }

> I ran the above code in debug mode, and found correct value in msg inside
> function popItem, but when it jumped out of the function, the value stored
> in pPopMsg became wrong. why?



Fri, 10 Oct 2003 18:06:22 GMT  
 anything wrong when using pop function?
pPopMsg is never initialized, so when it is used, the data is not put
anywhere that is a valid memory location for the data. You must either
provide memory for pPopMsg or use a pointer to a pointer for the popItem
parameter.


Quote:
> std::queue<char*> queStr;

> char* pPushMsg = "test";
> char* pPopMsg;
> queStr.push(pPushMsg );
> popItem(pPopMsg);

> void popItem(char* msg)
> {
>     msg = queStr.front();
>     queStr.pop();
> }

> I ran the above code in debug mode, and found correct value in msg inside
> function popItem, but when it jumped out of the function, the value stored
> in pPopMsg became wrong. why?



Fri, 10 Oct 2003 21:10:30 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Anything wrong with this function?

2. Is there anything wrong with this ?

3. Is there anything wrong with this?

4. Is anything wrong with this declaration?

5. Can you see anything wrong with this code?

6. Anything wrong with this calculation????

7. HELP: Anything Wrong With This?

8. Anything wrong with this code?

9. Is there anything obviously wrong...

10. Is there anything wrong with this code?

11. Anything wrong with the code snippet?

12. Anything similar to virtual for static functions?

 

 
Powered by phpBB® Forum Software