Hey everyone,

I'm trying to do something like this:

typedef void * socktag

int gensock_connect (char * hostname,
                     char * service,
                     socktag * pst)
{

   int retval;

   connection * conn = new connection;

   if ((retval = conn->get_connected (hostname, service)))
   {
      gensock_close(0);
      *pst = 0;
      return (0);
   }
   *pst = conn;

   return (retval);
}

So when it gets to the line *pst = conn;, *pst never gets assigned the value of conn's memory address. It stays at 0. But I have confirmed it does actually hit the line, and the address of conn is 0x...119de7610. Am I missing something?

Edited 5 Years Ago by Duki: n/a

Have a look at this:

if ((retval = conn->get_connected (hostname, service)))

You're performing an assignment within the condition of an if statement. That's almost always a no no. Was this your intent? Remember, a non-zero value in a conditional is considered true. Only a NULL/0 value is considered false by a conditional.

I have a feeling you're never getting to the line where you assign conn to pst. I think your flow of control keeps going in to the block controlled by the if and everything is getting reset before terminating the function.

Edited 5 Years Ago by Fbody: n/a

Yeah, this code is ancient. Not written by me, I promise. I'm just a debugger. :)

It's a tweaked version of BLAT for AIX systems. We're (I think) putting in an enhancement request to rewrite the whole component, but for now I just need to get it working.

I'm having an issue getting *pst to contain the memory address of conn.

I don't think so. I've stepped through each line - I'm pretty positive it's hitting.

@Fbody: I think that statement you posted is intentional. I have a feeling that it uses return values to signify success or not. So the if statement checks if the return value is a success or a fail. A fail probably returns 0.

@Fbody: I think that statement you posted is intentional. I have a feeling that it uses return values to signify success or not. So the if statement checks if the return value is a success or a fail. A fail probably returns 0.

I agree, but I suspect that it returns 0 on success not fail, because 0 is considered false. Otherwise it would never get to the line in question and the OP has indicated that it does get to that line. I really just want to make sure the OP knows what's going on there.

@Duki:
Which line(s) did you add to this and which are original? Is *pst = conn; yours? What about the typedef?

I notice that you are de-referencing pst. Why? There is most likely a type compatibility issue there. Have you tried making the assignment without de-referencing pst? As written, you are attempting to assign directly from a pointer to some object, not from a pointer to a pointer.

Edited 5 Years Ago by Fbody: n/a

The only modification I've made is the *pst = conn; - it was miswritten as *pst = retval, which is not at all what needs to be done.

You are correct - a 0 return signifies a successful connection. The typedef was originally typedef void FAR * socktag.

I want *pst to be the memory address of conn. Because later down the road, we do this:

int gensock_getchar (int st, int wait, char * ch)
{
   int retval = 0;
   connection *conn;

   conn = (connection *)st;   //<- st = pst

   if ((retval = conn->cc_getchar(wait, ch)))
      return (retval);
   else
      return (0);
}

Remember: I didn't author this.

Edited 5 Years Ago by Duki: n/a

>>I want *pst to be the memory address of conn.
Based on what I'm seeing, conn is a pointer to a connection, it's not actually a connection object. A pointer stores a memory address.

Based on that, I should pose the question back to you. Do you want the memory address of the connection object or the memory address of the pointer to the connection (conn)? I suspect you want the address of the actual connection object.

You can get the memory address of the connection object quite easily by using the assignment pst = conn (notice I didn't de-reference pst).

You can not assign the memory address of conn to pst legally. The pointer conn goes out of scope when your function ends, which means you are returning a reference to a local variable, a cardinal sin. Additionally, if it wasn't a local variable, you would have to declare pst as a double-pointer/pointer-to-pointer (i.e. void **pst).

Edited 5 Years Ago by Fbody: n/a

You're right in most of your assumptions. Though, I need the memory address of 'new connection' - the address that conn points to.

Later, in gensock_getchar, we then create a new pointer, conn, and point it to the value of *pst as it's memory location.

I hope that makes sense?

Edited 5 Years Ago by Duki: n/a

>>I need the memory address of 'new connection' - the address that conn points to.
That's what I thought you meant, but your wording said something different. When talking about pointers, you need to be very careful about your vocabulary, otherwise things can get confusing very quickly.

The "new connection" you refer to is the connection object. The 'new' operator returns the memory address of the allocated memory for the created object (a pointer to the object). The variable conn is a pointer. This means that the value stored in conn is the memory address that it points to. You're trying to get that memory address to another part of the program so that this other part of the program can use that connection object.

Both pst and conn are pointers. Thus, they are both capable of storing memory addresses. All that you need to do is assign conn to pst, you don't need to do any referencing or de-referencing.

pst = conn;

EDIT:
In fact, you could simply do this.

DISCLAIMER!!
This would produce the same net effect, but I don't recommend this because it's not very robust code.

int gensock_connect (char * hostname,
                     char * service,
                     socktag * pst)
{
  pst = new connection;
  return (0);
}

Edited 5 Years Ago by Fbody: n/a

You can't do pst = conn though:

gensock.cpp(763) : error C2440: '=' : cannot convert from 'connection *' to 'socktag *'

Edited 5 Years Ago by Duki: n/a

Oh nuts, I read something wrong. The declaration of pst is socktag *pst . The definition of the "socktag" type is typedef void* socktag . This results in pst having the actual declaration void **pst . It's a pointer-to-pointer...

I can see now why they're de-referencing pst in the assignment, but something seems wrong about the way things are being declared. Pointer operators are supposed to fire off before assignment operators, but it's possible that the operators are not firing off in the correct order, try adding parentheses for now:

(*pst) = conn;

I'll have to see the code surrounding the call to gensock_connect() and the declarations of the arguments. Something just isn't sitting right with me...

Edited 5 Years Ago by Fbody: n/a

I'm surprised Narue hasn't showed up to tell me how I should never consider using typedef void *.

NARUE: I didn't write the code. I just want it to work while we wait for approval to re-write the component.

This article has been dead for over six months. Start a new discussion instead.