dereferencing problem

B

Bill Cunningham

My compiler is complaining about line 7 I am pretty sure and is saying
something about dereferencing. The only dereference I intend in my code is
being passed to the freeaddrinfo(). Beause it wants a struct addrinfo *. Now
I am intending to declare a pointer to pointer in that line 7. This compiled
the other day when I was working on something very similar. I know these
aren't standard functions but dereferencing is standard C. What am I doing
wrong. I forgot to save the compiler warnings to stderr but someone good
enough can probably see what the problem is.

Whew.

Bill
 
B

Bill Cunningham

Bill Cunningham wrote:
[...]

Oh Duh. I need to post code. Long day.

#include <stdio.h>
#include <sys/socket.h>

int main()
{
struct addrinfo *pa;
struct addrinfo **pp;
int v;

pa->ai_family = AF_INET;
pa->ai_socktype = SOCK_DGRAM;
pa->ai_protocol = 0;
pa->ai_next = NULL;
v = getaddrinfo(NULL, "80", pa, pp);
if (v == -1)
fprintf(stderr, "%s\n", gai_strerror(v));
else {
fprintf(stderr, "%s\n", gai_strerror(v));
}
freeaddrinfo(*pp);
return 0;
}
 
S

Stefan Ram

Bill Cunningham said:
struct addrinfo *pa; ....
pa->ai_family = AF_INET;

»pa«, in »pa->ai_family«, is assumed to point to writeable
memory, but »pa« was not initialized. »pa->« means »(*pa).«,
thus, dereferencing.
 
B

Bill Cunningham

Stefan said:
»pa«, in »pa->ai_family«, is assumed to point to writeable
memory, but »pa« was not initialized. »pa->« means »(*pa).«,
thus, dereferencing.

Ok. You say pa is uninitialized. Hum. All I know is the rest of the
elements pointed to work. Well ai_family element is full. I assigned it
properly or at least the way I meant to. I am glad to see the problem wasn't
with the freeddrinfo function. I did intend a dereference.
I meant initialization with the line...

struct addrinfo *pa;

A pointer to a struct addrinfo type. Is my syntax wrong?

Bill
 
I

Ian Collins

Bill said:
My compiler is complaining about line 7 I am pretty sure and is saying
something about dereferencing.

Pretty sure? Can't you read what it actually says?
 
G

glen herrmannsfeldt

(snip, and previously snipped code using pa)
struct addrinfo *pa;
A pointer to a struct addrinfo type. Is my syntax wrong?

Syntax is right. That declares pa as a pointer, but doesn't
point it to anything.

One way is to have a struct addrinfo, and use &.

struct addrinfo a;
struct addrinfo *pa=&a;
(or assign it on another line)

Since addrinfo is small, that is probably the best way,
but some will malloc() it.

struct addrinfo *pa;
pa=malloc(sizeof(*pa));

then you should remember to free() it later.

I think pp should also point to something before the call, or
be & of something, but you didn't (yet) ask about that.

-- glen
 
B

Barry Schwarz

Bill Cunningham wrote:
[...]

Oh Duh. I need to post code. Long day.

#include <stdio.h>
#include <sys/socket.h>

int main()
{
struct addrinfo *pa;
struct addrinfo **pp;
int v;

pa->ai_family = AF_INET;
pa->ai_socktype = SOCK_DGRAM;
pa->ai_protocol = 0;
pa->ai_next = NULL;
v = getaddrinfo(NULL, "80", pa, pp);
if (v == -1)
fprintf(stderr, "%s\n", gai_strerror(v));
else {
fprintf(stderr, "%s\n", gai_strerror(v));
}
freeaddrinfo(*pp);
return 0;
}

It only took two tries for you to post the code. How many before you
post the actual error message?
 
B

Bill Cunningham

glen said:
(snip, and previously snipped code using pa)



Syntax is right. That declares pa as a pointer, but doesn't
point it to anything.

One way is to have a struct addrinfo, and use &.

struct addrinfo a;
struct addrinfo *pa=&a;
(or assign it on another line)

Since addrinfo is small, that is probably the best way,
but some will malloc() it.

struct addrinfo *pa;
pa=malloc(sizeof(*pa));

then you should remember to free() it later.

I think pp should also point to something before the call, or
be & of something, but you didn't (yet) ask about that.

So that's why they're using that ampersand in the function calls that
require pointers. I thought it was because they just weren't wanting to use
pointers and they declare something like this...
struct addrinfo sa;
Which is an instance of that type and not a pointer. In the code I've been
reading getaddrinfo's third paramter is calling for a pointer. So I just
passed it pa. My understanding is somewhat off.

Bill
 
B

Bill Cunningham

glen said:
(snip, and previously snipped code using pa)



Syntax is right. That declares pa as a pointer, but doesn't
point it to anything.

One way is to have a struct addrinfo, and use &.

struct addrinfo a;
struct addrinfo *pa=&a;
(or assign it on another line)

Since addrinfo is small, that is probably the best way,
but some will malloc() it.

struct addrinfo *pa;
pa=malloc(sizeof(*pa));

then you should remember to free() it later.
[snip]

Or use memset! Like memset (pa,0,sizeof (struct addrinfo)); Since it takes a
pointer. Or maybe I should use memset(&pa,0,sizeof(struct addrinfo));
Which would I use and why?

Bill
 
B

Bill Cunningham

Barry said:
It only took two tries for you to post the code. How many before you
post the actual error message?

All right smart ass. As I explained in my first post it would've been proper
I admit to have posted what you want. But I forgot and didn't think it was
necessary. But since you asked here it is. Sorry I forgot. It happens.

p.c: In function 'main':
p.c:10:7: error: dereferencing pointer to incomplete type
p.c:11:7: error: dereferencing pointer to incomplete type
p.c:12:7: error: dereferencing pointer to incomplete type
p.c:13:7: error: dereferencing pointer to incomplete type

If that tells you something your better than me. If you need this then
you're not as good as some.

Bill
 
S

Stefan Ram

Bill Cunningham said:
p.c: In function 'main':
p.c:10:7: error: dereferencing pointer to incomplete type
p.c:11:7: error: dereferencing pointer to incomplete type
p.c:12:7: error: dereferencing pointer to incomplete type
p.c:13:7: error: dereferencing pointer to incomplete type

This might suggest to

#include <netdb.h>
 
B

Bill Cunningham

Stefan said:
This might suggest to

#include <netdb.h>

Are those first numbers line numbers? Why is 7 memntioned in every line.
I thought 7 was a line number. :shrug:

Bill
 
I

Ian Collins

Bill said:
Are those first numbers line numbers? Why is 7 memntioned in every line.
I thought 7 was a line number. :shrug:

After, what at decade or more, you still haven't worked this out?

There be trolls...
 
B

Bill Cunningham

[snip]

Ok I included that header and the code compiled but when it was run it seg
faulted. I compiled with debugging code the debugger said line 11 and that
was the line previously mentioned supra that is:

pa->ai_family=AF_INET;

Bill
 
B

Bill Cunningham

Richard said:
Which indicates that it doesn't have a definition for the type that pa
is pointing to, i.e. addrinfo.

Ok I checked and addrinfo is defined in netdb.h for some reason. I thought
that header was just for get* functions from files in the /etc directory
like protocols and service and so on. But the man page included it so I
should have. What does column mean? Thanks much.

Bill
 
L

Lew Pitcher

Bill Cunningham wrote:
[...]

Oh Duh. I need to post code. Long day.

#include <stdio.h>
#include <sys/socket.h>

int main()
{
struct addrinfo *pa;

Here, you define pa as an object that will contain a pointer to a struct
addrinfo structure.

struct addrinfo **pp;
int v;

pa->ai_family = AF_INET;
/
Here, you try to stuff some data into an object that is accessed through the
pa pointer.
pa->ai_socktype = SOCK_DGRAM;
/
And here
pa->ai_protocol = 0;
/
And here
pa->ai_next = NULL;
/
And here

But, you never actually populate pa with an address. So all of these
attempts to put data into objects pointed to by pa will result in errors,
because pa *doesn't point at anything*.
 
I

Ian Collins

Lew said:
Bill Cunningham wrote:
[...]

Oh Duh. I need to post code. Long day.

#include <stdio.h>
#include <sys/socket.h>

int main()
{
struct addrinfo *pa;

Here, you define pa as an object that will contain a pointer to a struct
addrinfo structure.

I guess there's always one who falls into the trap....
 
L

Lew Pitcher

[snip]

Ok I included that header and the code compiled but when it was run it seg
faulted. I compiled with debugging code the debugger said line 11 and that
was the line previously mentioned supra that is:

pa->ai_family=AF_INET;

See my previous reply.

Short version: you never put a pointer value into pa, so it doesn't point at
anything.
 
L

Lew Pitcher

Lew said:
Bill Cunningham wrote:
[...]

Oh Duh. I need to post code. Long day.

#include <stdio.h>
#include <sys/socket.h>

int main()
{
struct addrinfo *pa;

Here, you define pa as an object that will contain a pointer to a struct
addrinfo structure.

I guess there's always one who falls into the trap....

.... Meh ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top