Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Update 2, refining question

How do you design a function to set two values which could start and end as null? I'm in another function where I'd like to have a couple of local pointers that would start as null, which a function would take as parameters, and set them to point to objects under certain conditions and leave them null in other conditions.

I've been trying to use pointers to pointers but I have not been able to make it work correctly. Is there a better approach? If not, what am I doing wrong?

Original Question with edits

I would like a function that takes two pointer parameters which begin as null and if the function finds the two objects it's looking for the, the two pointers would be set to the addresses of the found objects for use by the caller. I'm guessing the way to do this would be to use pointer to pointer params but I haven't gotten it to work.

void ConfigureStuff()
{
    CMyObject ** left = 0; // update, what should I be doing here?
    CMyObject ** right = 0;

    // update, recently tried this and it crashes here as well
    // so I must be doing something wrong above 
    *left = 0; // crashes
    *right = 0;

    FindLeftAndRight( left, right );


}

void FindLeftAndRight( CMyObject ** left, CMyObject ** right )
{
    CMyObject * searchObjectLeft = 0;
    CMyObject * searchObjectRight = 0;

    //...
    // code that finds left and right
    //...

    if ( bFoundObjects )
    {
        *left = searchObjectLeft;  // update, crashed here
        *right = searchObjectRight;
    }

}


What am I doing wrong? What's the simplest way to get what I want?

Thanks.
Posted
Updated 1-May-11 21:12pm
v9

C++
void ConfigureStuff()
{
    CMyObject * left = 0;
    CMyObject * right = 0;
    FindLeftAndRight( &left, &right );

}
 
Share this answer
 
Comments
Brian Bennett 2-May-11 3:20am    
You rock! Thanks. Thanks to others for their time, it is much appreciated.
Niklas L 2-May-11 4:06am    
You're welcome
Ajay Vijayvargiya 2-May-11 3:36am    
Looks like basic pointer level misunderstanding! :)
Edit: Not to answer, but to Brain!
Sergey Alexandrovich Kryukov 2-May-11 5:47am    
Certainly. Did you see "discussion" over my answer?

Niklas gets 5 for the answer!
--SA
What am I doing wrong?
Pretty much everything. It would not even compile.

What's the simplest way to get what I want?
You don't want the "simplest way". You want the right way. It depends on what you want. From this code, it is not clear.

You also did not report your problem. How come you supply the code which does not compile? You do not consider it a problem; "I haven't gotten it to work" cannot be uses as an issue report.

Yes, your double pointer is the way to modify the inner pointer and return it to the caller, nothing wrong with it. By some reason you return null pointer (zero) for both parameters, but I guess you should calculate them under "//...".

Nevertheless, I looks like you're not ready for C coding just yet. 1) Functions lack return type (even void); 2) function FindLeftAndRight is used before declaration, so it cannot compile, ";" after declaration should be removed, because after it the code block goes; 3) function ConfigureStuff needs "()" after the name; 4) you need to define CMyObject, if you think it's a class (as the MFC-style name suggests), remember there are no classes in C; 4) variable bFoundObjects if not declared/initialized; 5) it looks like you lack important input and return parameter needed for calculations… do I need to continue?..

—SA
 
Share this answer
 
v4
Comments
Brian Bennett 1-May-11 22:35pm    
My explanation makes it clear what I want. The code above is basically pseudo-code. My code crashes where the first assignment is made, "Access violation writing location 0x00000000" after it finds valid objects.
Sergey Alexandrovich Kryukov 1-May-11 23:10pm    
You think I does, I think it does not. Want to wait for the one who would understand it?
You're asking not about algorithm but about coding but present pseudo-code. It is not very productive.
If you present pseudo-code you need to warn about it.
It does not looks like you really trying to get help. Just think about it. Your exception does not show much. Not only you don't show in what line of code the exception is thrown but you don't show the code itself.
--SA
Brian Bennett 1-May-11 23:22pm    
You seem irritated
Sergey Alexandrovich Kryukov 2-May-11 0:36am    
It does not matter. But if you're so nice to take care about that, please formulate questions and reply correctly. Why did I spent time spotting your bugs? Just to hear from you that it was just a joke, pardon... pseudo-code?

Anyway, you got an answer:

"*left = 0;" crashes because "CMyObject ** left = 0;" You pointer to pointer points nowhere, so you don't have a single pointer to modify.

Now look, you have complained to the bug in the code you did not show. Do you realize that? You need to collaborate to get help; this is called cryptic.

--SA
Brian Bennett 2-May-11 1:04am    
There is a lot of negativity in the way you teach and so the collaboration is not fun. Try being nicer. I will try to better my questions.

Still you haven't given a solution, only showed why it's going wrong. I want to start with null local variables and have the called function fill them if the results are found.
Have you tried to Debug it? Where exactly it crashes?
What is the value of variable 'left' and 'right' when it enters the funtion?

How have you called this method?
 
Share this answer
 
Comments
Brian Bennett 1-May-11 23:50pm    
I've tried to debug this (with a break-point) at that first assignment (using .net) but when I step into it, I get the crash immediately. I call the function as shown inside ConfigureStuff(). The values of left and right are null (as is how I would like it to be) when the function is called.
Ajay Vijayvargiya 2-May-11 0:14am    
HOW are you calling the function? What exact parameters?
Brian Bennett 2-May-11 0:40am    
I changed the example above but I was calling it exactly like

CMyObj ** left = 0; // local
CMyObj ** right = 0;
FindLeftAndRight( left, right );

With the changes I made to the example, look where it crashes now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900