dereferencing problem

Discussion in 'C Programming' started by Bill Cunningham, Apr 12, 2014.

  1. 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
     
    Bill Cunningham, Apr 12, 2014
    #1
    1. Advertisements

  2. 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;
    }
     
    Bill Cunningham, Apr 12, 2014
    #2
    1. Advertisements

  3. Bill Cunningham

    Stefan Ram Guest

    »pa«, in »pa->ai_family«, is assumed to point to writeable
    memory, but »pa« was not initialized. »pa->« means »(*pa).«,
    thus, dereferencing.
     
    Stefan Ram, Apr 12, 2014
    #3
  4. 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
     
    Bill Cunningham, Apr 12, 2014
    #4
  5. Bill Cunningham

    Ian Collins Guest

    Pretty sure? Can't you read what it actually says?
     
    Ian Collins, Apr 13, 2014
    #5
  6. (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.

    -- glen
     
    glen herrmannsfeldt, Apr 13, 2014
    #6
  7. It only took two tries for you to post the code. How many before you
    post the actual error message?
     
    Barry Schwarz, Apr 13, 2014
    #7
  8. 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
     
    Bill Cunningham, Apr 13, 2014
    #8
  9. [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
     
    Bill Cunningham, Apr 13, 2014
    #9
  10. 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
     
    Bill Cunningham, Apr 13, 2014
    #10
  11. Bill Cunningham

    Stefan Ram Guest

    This might suggest to

    #include <netdb.h>
     
    Stefan Ram, Apr 13, 2014
    #11
  12. Are those first numbers line numbers? Why is 7 memntioned in every line.
    I thought 7 was a line number. :shrug:

    Bill
     
    Bill Cunningham, Apr 13, 2014
    #12
  13. Bill Cunningham

    Ian Collins Guest

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

    There be trolls...
     
    Ian Collins, Apr 13, 2014
    #13
  14. [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
     
    Bill Cunningham, Apr 13, 2014
    #14
  15. 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
     
    Bill Cunningham, Apr 13, 2014
    #15
  16. Bill Cunningham

    Lew Pitcher Guest

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

    /
    Here, you try to stuff some data into an object that is accessed through the
    pa pointer.
    /
    And here
    /
    And here
    /
    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*.
     
    Lew Pitcher, Apr 13, 2014
    #16
  17. Bill Cunningham

    Ian Collins Guest

    Do you have access to a dictionary?
     
    Ian Collins, Apr 13, 2014
    #17
  18. Bill Cunningham

    Ian Collins Guest

    I guess there's always one who falls into the trap....
     
    Ian Collins, Apr 13, 2014
    #18
  19. Bill Cunningham

    Lew Pitcher Guest

    See my previous reply.

    Short version: you never put a pointer value into pa, so it doesn't point at
    anything.
     
    Lew Pitcher, Apr 13, 2014
    #19
  20. Bill Cunningham

    Lew Pitcher Guest

    .... Meh ...
     
    Lew Pitcher, Apr 13, 2014
    #20
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.