Pointer to Pointer to Pointer.... 
Author Message
 Pointer to Pointer to Pointer....

Hi all,

I'm writing a shared library (a Netscape Messaging server
plugin to be exactly) and have a problem...

I need to write a function with the following prototype:

int OrcaPluginProc(pblock *Config,Message **ppInMsg,Message ***pppOutMsg)

so pppOutMsg is a pointer to a pointer to a pointer to a
Message struct.

I declare:

Message *pOrcaMessage;

and near the end of my function this structure has been
properly set up, filled with the proper data, and OrcaMessage
points to it.

Now I need to make sure that ***pppOutMsg points to that same
thingy. Assuming that pppOutMsg starts out NULL, how can I do
this?

**ppOutMessage = pOrcaMessage

turns out to be a sure way of getting the process to dump
core...

Any of you C gurus have a usefull hint for me?

TIA

Krist



Sun, 09 Nov 2003 01:58:08 GMT  
 Pointer to Pointer to Pointer....

Quote:

> Hi all,

> I'm writing a shared library (a Netscape Messaging server
> plugin to be exactly) and have a problem...

> I need to write a function with the following prototype:

> int OrcaPluginProc(pblock *Config,Message **ppInMsg,Message ***pppOutMsg)

> so pppOutMsg is a pointer to a pointer to a pointer to a

an object of type Message. This may or may not be typedefed to
a struct type.

Quote:
> Message struct.

> I declare:

> Message *pOrcaMessage;

> and near the end of my function this structure has been
> properly set up, filled with the proper data, and OrcaMessage
> points to it.

I take it you properly malloced it ?

Quote:
> Now I need to make sure that ***pppOutMsg points to that same
> thingy.

Is your intention to return a pointer to the struct through
pppOutMsg?

Quote:
> Assuming that pppOutMsg starts out NULL, how can I do
> this?

Then there is no way you can return anything through it.

Quote:
> **ppOutMessage = pOrcaMessage

This is clearly wrong if ppOutMessage is NULL. You're dereferencing
a null pointer.

Quote:
> turns out to be a sure way of getting the process to dump
> core...

> Any of you C gurus have a usefull hint for me?

I'm not sure what you want to do. But

if((pOutMessage=malloc(*pOutMessage)==NULL){
  /* handle error */

Quote:
}

if((*pOutMessage=malloc(**pOutMessage)==NULL){
  /* handle error */
Quote:
}

**pOutMessage=pOrcaMessage;

will allocate a bunch of pointers that end up pointing to where
you want.

Without knowing what exactly you're trying to do it's difficult
to give you more than these $0.02. Tobias



Sun, 09 Nov 2003 02:23:45 GMT  
 Pointer to Pointer to Pointer....

Quote:

>> Hi all,

>> I'm writing a shared library (a Netscape Messaging server
>> plugin to be exactly) and have a problem...

>> I need to write a function with the following prototype:

>> int OrcaPluginProc(pblock *Config,Message **ppInMsg,Message ***pppOutMsg)

>> so pppOutMsg is a pointer to a pointer to a pointer to a
> an object of type Message. This may or may not be typedefed to
> a struct type.
>> Message struct.

>> I declare:

>> Message *pOrcaMessage;

>> and near the end of my function this structure has been
>> properly set up, filled with the proper data, and OrcaMessage
>> points to it.
> I take it you properly malloced it ?
>> Now I need to make sure that ***pppOutMsg points to that same
>> thingy.
> Is your intention to return a pointer to the struct through
> pppOutMsg?
>> Assuming that pppOutMsg starts out NULL, how can I do
>> this?
> Then there is no way you can return anything through it.
>> **ppOutMessage = pOrcaMessage
> This is clearly wrong if ppOutMessage is NULL. You're dereferencing
> a null pointer.
>> turns out to be a sure way of getting the process to dump
>> core...

>> Any of you C gurus have a usefull hint for me?
> I'm not sure what you want to do. But
> if((pOutMessage=malloc(*pOutMessage)==NULL){
>   /* handle error */
> }
> if((*pOutMessage=malloc(**pOutMessage)==NULL){
>   /* handle error */
> }
> **pOutMessage=pOrcaMessage;
> will allocate a bunch of pointers that end up pointing to where
> you want.
> Without knowing what exactly you're trying to do it's difficult
> to give you more than these $0.02. Tobias

Surely you mean:
if ((pOutMessage=malloc(sizeof(*pOutMessage))==NULL) {
  /* handle error */
Quote:
}

if ((*pOutMessage=malloc(sizeof(**pOutMessage))==NULL) {
  /* handle error */
Quote:
}

**pOutMessage=pOrcaMessage;
?

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"'So called' means: 'There is a long explanation for this, but I have no
time to explain it here.'"
   - JIPsoft



Sun, 09 Nov 2003 02:29:20 GMT  
 Pointer to Pointer to Pointer....

Quote:


> > **ppOutMessage = pOrcaMessage
> > turns out to be a sure way of getting the process to dump
> > core...

> I'm not sure what you want to do. But

> if((pOutMessage=malloc(*pOutMessage)==NULL){
>   /* handle error */
> }
> if((*pOutMessage=malloc(**pOutMessage)==NULL){
>   /* handle error */

I'm not sure what you want to do either. Did the Standard change?
Last time I had a look malloc() allocated a bunch of bytes, the
number of which is specified by its argument ...

willem



Sun, 09 Nov 2003 03:45:39 GMT  
 Pointer to Pointer to Pointer....

Quote:



> > > **ppOutMessage = pOrcaMessage
> > > turns out to be a sure way of getting the process to dump
> > > core...

> > I'm not sure what you want to do. But

> > if((pOutMessage=malloc(*pOutMessage)==NULL){
> >   /* handle error */
> > }
> > if((*pOutMessage=malloc(**pOutMessage)==NULL){
> >   /* handle error */

as Joona pointed out i forgot the sizeof operator in the
malloc calls.
Tobias.


Sun, 09 Nov 2003 04:14:02 GMT  
 Pointer to Pointer to Pointer....

Quote:

>I need to write a function with the following prototype:

>int OrcaPluginProc(pblock *Config,Message **ppInMsg,Message ***pppOutMsg)

>so pppOutMsg is a pointer to a pointer to a pointer to a
>Message struct.

It is not really clear what all this is for, but I can suggest one
place where someone might want such a thing.  Imagine you have a
function looking like this:

    int main(int argc, char **argv) {
        ... code that uses argv[i] for some i ...
    }

Of course such an interface is terribly rare :-) so no one will ever
have seen it before or be familiar with it. :-)

Silliness aside, most C programmers should be quite familiar with
the above.  But suppose that, from your main() function, you decide
to call a new function that can supply a new "argv" and "argc" pair.
How will you change argc and argv in the target function?  The answer,
of course, is to receive pointers to them:

    /* who knows what this returns */
    double maybe_change_them(int *pargc, char ***pargv) {
        ... code that maybe changes argc and argv ...
    }

In main(), you simply call this as:

    val = maybe_change_them(&argc, &argv);

and upon its return you may have new values in argc and argv.

What about "maybe_change_them" itself?  Well, suppose it decides
not to change argc and argv.  This is easy: just return without
changing them.  But what if you do want to change argc and argv?
Well, suppose the new argc is always exactly 3, and the new argv
is always the set {"prog", "-x", "42", NULL}.  Then:

    double maybe_change_them(int *pargc, char ***pargv) {
        static char *newav[4] = { "prog", "-x", "4", NULL };
        ...
        *pargc = 4;
        *pargv = newav;
        ...
        return 3.14159;
    }

What have you allocated and produced here?

The answer is: you allocated the array "newav" of size 4 of "char
*"s, and for each newav[i], you provided a value.  The value you
provided points to the first char in an an array of "char"s, where
each of those arrays has a different size: one is size 5, one size
3, and one size 2.  The last element, newav[3], you set to NULL.
The "array 5 of char" is initialized with {'p', 'r', 'o', 'g', '\0'};
the "array 3 of char" is initialized with {'-', 'x', '\0'}; and the
"array 2 of char" is initialized with {'4', '\0'}.

Each of your "allocations" is static and is actually performed by
the compiler.  You could have spelled out the three "array N of
char"s explicitly:

    static char av0[] = {'p', 'r', 'o', 'g', '\0'};
    static char av1[] = "-x"; /* same as {'-', 'x', '\0' } */
    static char av2[] = "4";
    static char *newav[4] = { av0, av1, av2, NULL };

Or, you could use malloc() or equivalent to allocate "newav" and
each "avX" dynamically:

    char **newav;
    ...
    newav = malloc(N * sizeof *newav);
    if (newav == NULL) ... handle "out of memory" error ...
    for (i = 0; i < N; i++)
        newav[i] = some_string(); /* might need another malloc() */

Thus, given the type signature above (repeated here):

Quote:
>int OrcaPluginProc(pblock *Config,Message **ppInMsg,Message ***pppOutMsg)

I suspect that "OrcaPluginProc" is really supposed to *set* a
variable of type "Message **", just like maybe_change_them() *sets*
argv, a variable of type "char **".  In order to set it, it needs
that variable's address, which is a value of type "Message ***".

If the "incoming message" (ppInMsg) happens to be valid as the
"outgoing message", the assignment:

    *pppOutMsg = InMsg;

would do the trick.  Otherwise you may need to create an array
dynamically, or use a statically-allocated single array:

    static Message *fixedmsg[] = { some, values };

or:

    Message **newmsg;
    ...
    newmsg = malloc(N * sizeof *newmsg);
    if (newmsg == NULL) ... handle error ...
    for (i = 0; i < N; i++)
        newmsg[i] = some_msg();
--
In-Real-Life: Chris Torek, Wind River Systems




Sun, 09 Nov 2003 07:40:26 GMT  
 Pointer to Pointer to Pointer....

Quote:
> I'm not sure what you want to do. But

> if((pOutMessage=malloc(*pOutMessage)==NULL){

Syntax error. You forgot the sizeof and you are missing a ).

Quote:
>   /* handle error */
> }
> if((*pOutMessage=malloc(**pOutMessage)==NULL){

Again, syntax error.

Quote:
>   /* handle error */
> }
> **pOutMessage=pOrcaMessage;

Bart.


Sun, 09 Nov 2003 10:32:42 GMT  
 Pointer to Pointer to Pointer....
Thanks, Chris, Your explanation is very enlightening!


Quote:

> In article


>>int OrcaPluginProc(pblock *Config,Message **ppInMsg,Message
>>***pppOutMsg)

> I suspect that "OrcaPluginProc" is really supposed to *set*
> a variable of type "Message **", just like
> maybe_change_them() *sets* argv, a variable of type "char
> **".  In order to set it, it needs that variable's address,
> which is a value of type "Message ***".

> If the "incoming message" (ppInMsg) happens to be valid as
> the "outgoing message", the assignment:

>     *pppOutMsg = InMsg;

Ok, that the first thing I'll try.
The final goal is to split mesages though. The proc gets one
"Message" from whoever calls it. Which I can't see because of
not having the source to Netscape Messaging Server :-(
Based on what is found in the message other messages are
created and it is indeed the intention that all these
messages get passed back.
I do allocate them dynamically. The API docs are explicit in
that the caller will free any memory the pointers in
pppOutMsg point to.

But all this will have to wait till monday. I'm of sailing in
a few hours :-)

Krist



Sun, 09 Nov 2003 17:37:21 GMT  
 Pointer to Pointer to Pointer....
I think a simple

  *pppOutMsg = pOrcaMessage;

should do the trick.

In the iPlanet Web Server (previously Netscape Web Server) all functions in a
plugin that will be invoked by the web server should  have the following
interface

    int func(pblock *, Session *, Request *)

All these are initialized by the web server and passed on to the function. It
could be similar in the messaging server too. May be pppOutMsg is the address
of a (Message **). Check the docs.

HTH

DK

Quote:

> Hi all,

> I'm writing a shared library (a Netscape Messaging server
> plugin to be exactly) and have a problem...

> I need to write a function with the following prototype:

> int OrcaPluginProc(pblock *Config,Message **ppInMsg,Message ***pppOutMsg)

> so pppOutMsg is a pointer to a pointer to a pointer to a
> Message struct.

> I declare:

> Message    *pOrcaMessage;

> and near the end of my function this structure has been
> properly set up, filled with the proper data, and OrcaMessage
> points to it.

> Now I need to make sure that ***pppOutMsg points to that same
> thingy. Assuming that pppOutMsg starts out NULL, how can I do
> this?

> **ppOutMessage = pOrcaMessage

> turns out to be a sure way of getting the process to dump
> core...

> Any of you C gurus have a usefull hint for me?

> TIA

> Krist



Sun, 09 Nov 2003 20:17:29 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. memory leak: pointer->pointer->pointer->struct

2. Pointer of Pointers was Pointer of arrays...

3. memory block containing pointers, aka pointer to pointer i believe

4. memory Leak: pointer->pointer->pointer->struct

5. Question on pointer-to-pointer-to-pointer

6. double pointer (or pointer to pointer)

7. Why Wont This Compile Right--Pointer to Pointer to Pointer to Float

8. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

9. pointers pointers pointers!!!

10. Pointer Functions and Pointers to Pointer Functions

11. How to use (pointer to function), and function and pointer to (pointer to function)

12. Pointers: return of pointer to array of pointers to main

 

 
Powered by phpBB® Forum Software