Help with passing integer poniter to function

Discussion in 'C Programming' started by manochavishal@gmail.com, Mar 1, 2006.

  1. Guest

    Hi,

    I am having strange problem in my Program.

    I cannot paste the whole program as it is huge so just pasting the
    lines i think are necessary.

    I am passing a integer array pointer to a function.

    **********

    int sockets[MAX_TCPCLIENTS]; /* an array of connected sockets*/
    ......
    ......
    fprintf(stderr,"The value is %d",sockets);

    accept_new_tcp_client(sockets,&readset,listenfd);
    .......
    .......
    void accept_new_tcp_client(int *sockets, fd_set *readset, int listenfd)
    {
    struct sockaddr *signaddr;
    int addrlength;
    int i;
    int clientsockfd;
    addrlength = sizeof(signaddr);

    fprintf(stderr,"\nThe value is %d",sockets);

    /*== First accept the new connection ==*/

    if((clientsockfd = accept(listenfd,(struct
    sockaddr*)&signaddr,&addrlength))<0)
    {
    printf("\nSome Problem with accepting connection");
    exit(EXIT_FAILURE);
    }

    fprintf(stderr,"\nAccepted a new Client connection");

    fprintf(stderr,"\nThe value is %d",sockets);

    ********
    The print output is

    .....
    The value is 12643936
    The value is 12643936
    Accepted a new Client connection
    The value is 0Segmentation fault (core dumped)


    Can anyone explians whats happening???

    Cheers
    Vishal
     
    , Mar 1, 2006
    #1
    1. Advertising

  2. pemo Guest

    wrote:
    > Hi,
    >
    > I am having strange problem in my Program.
    >
    > I cannot paste the whole program as it is huge so just pasting the
    > lines i think are necessary.
    >
    > I am passing a integer array pointer to a function.
    >
    > **********
    >
    > int sockets[MAX_TCPCLIENTS]; /* an array of connected sockets*/
    > .....
    > .....
    > fprintf(stderr,"The value is %d",sockets);
    >
    > accept_new_tcp_client(sockets,&readset,listenfd);
    > ......
    > ......
    > void accept_new_tcp_client(int *sockets, fd_set *readset, int
    > listenfd) {
    > struct sockaddr *signaddr;
    > int addrlength;
    > int i;
    > int clientsockfd;
    > addrlength = sizeof(signaddr);






    > fprintf(stderr,"\nThe value is %d",sockets);


    This is the second 'The value is 12643936' in your list?


    > /*== First accept the new connection ==*/
    >
    > if((clientsockfd = accept(listenfd,(struct
    > sockaddr*)&signaddr,&addrlength))<0)
    > {
    > printf("\nSome Problem with accepting connection");
    > exit(EXIT_FAILURE);
    > }
    >
    > fprintf(stderr,"\nAccepted a new Client connection");



    > fprintf(stderr,"\nThe value is %d",sockets);



    And this one produces the error?


    > ********
    > The print output is
    >
    > ....
    > The value is 12643936
    > The value is 12643936
    > Accepted a new Client connection
    > The value is 0Segmentation fault (core dumped)
    >
    >
    > Can anyone explians whats happening???



    If my assumptions are correct [above], then I would *guess* that something's
    screwing up the stack in your call to 'accept()' - if you comment that out,
    the fprintf works ok?

    In the call to accept(), you're casting signaddr to a 'struct sockaddr *' -
    which it already is ...

    struct sockaddr *signaddr

    But, you're passing a struct sockaddr ** by using &signaddr.


    --
    ==============
    Not a pedant
    ==============
     
    pemo, Mar 1, 2006
    #2
    1. Advertising

  3. wrote:

    > I am having strange problem in my Program.
    >
    > I cannot paste the whole program as it is huge so just pasting the
    > lines i think are necessary.
    >
    > I am passing a integer array pointer to a function.


    no you arn't. You are passing an int* or ptr-to-int to a function.

    > **********
    >
    > int sockets[MAX_TCPCLIENTS]; /* an array of connected sockets*/
    > .....
    > .....
    > fprintf(stderr,"The value is %d",sockets);


    this is wrong. fpritnf() expects an int for %d you've passed an
    array name which "decays" (is automatically converted) into
    an int*. You've got undefined behaviour (Very Bad) your program's
    behaviour cannot be predicted.

    What do you think this statement does? Did you mean to
    print sockets[0]?

    Note you use the same broken constuct in several places

    <snip>

    > void accept_new_tcp_client(int *sockets, fd_set *readset, int listenfd)


    as I noted above sockets is an int*

    <snip>

    fix the Undefined Behaviour and try again.


    --
    Nick keighley
     
    Nick Keighley, Mar 1, 2006
    #3
  4. pemo Guest

    Nick Keighley wrote:
    > wrote:
    >
    >> I am having strange problem in my Program.
    >>
    >> I cannot paste the whole program as it is huge so just pasting the
    >> lines i think are necessary.
    >>
    >> I am passing a integer array pointer to a function.

    >
    > no you arn't. You are passing an int* or ptr-to-int to a function.
    >
    >> **********
    >>
    >> int sockets[MAX_TCPCLIENTS]; /* an array of connected sockets*/
    >> .....
    >> .....
    >> fprintf(stderr,"The value is %d",sockets);

    >
    > this is wrong. fpritnf() expects an int for %d you've passed an
    > array name which "decays" (is automatically converted) into
    > an int*. You've got undefined behaviour (Very Bad) your program's
    > behaviour cannot be predicted.
    >
    > What do you think this statement does? Did you mean to
    > print sockets[0]?
    >
    > Note you use the same broken constuct in several places
    >
    > <snip>
    >
    >> void accept_new_tcp_client(int *sockets, fd_set *readset, int
    >> listenfd)

    >
    > as I noted above sockets is an int*
    >
    > <snip>
    >
    > fix the Undefined Behaviour and try again.


    I *think* he just wanted the address - to /see/ that it's valid - possibly
    the stuff that outputs the address of the array was put in place as a
    diagnostic aid to trace the original problem?

    In my experience, *most* people seem to use %d vs. %p with the necessary
    cast to check addresses, and as long as an sizeof(int) == sizeof(? *)
    there's not a lot that can go wrong with it is there?


    --
    ==============
    Not a pedant
    ==============
     
    pemo, Mar 1, 2006
    #4
  5. Guest

    HI,


    > If my assumptions are correct [above], then I would *guess* that something's
    > screwing up the stack in your call to 'accept()' - if you comment that out,
    > the fprintf works ok?


    Yes you are right if i comment the accept() function it works fine.
    So i really cant figure out whats happeing to the stack here.
    Why the address of sockets is being lost.


    > In the call to accept(), you're casting signaddr to a 'struct sockaddr *' -
    > which it already is ... struct sockaddr *signaddr


    I have tried passing signaddr without casting , but still giving the
    same problem.
    ****
    if((clientsockfd = accept(listenfd,&signaddr,&addrlength))<0)
    {
    printf("\nSome Problem with accepting connection");
    exit(EXIT_FAILURE);
    }
    ***


    >But, you're passing a struct sockaddr ** by using &signaddr.


    I am working on Cygwin here. Can that be a problem. As when i used to
    work on Solaris as far as i remember this did not
    gave any problem.


    Cheers
    Vishal
     
    , Mar 1, 2006
    #5
  6. Guest

    Yes, you are right.

    Even if i use %p instead it gives

    The value is 0xc0ee60
    The value is 0xc0ee60
    Accepted a new Client connection
    The value is 0x0Segmentation fault (core dumped)

    So %d was merely for diagnostic purpose to trace the problem (to check
    if sockets hold the same thing after accept is called).

    You can read my Mind!!!
    Great!!

    Cheers
    Vishal
     
    , Mar 1, 2006
    #6
  7. pemo Guest

    wrote:
    > HI,
    >
    >
    >> If my assumptions are correct [above], then I would *guess* that
    >> something's screwing up the stack in your call to 'accept()' - if
    >> you comment that out, the fprintf works ok?

    >
    > Yes you are right if i comment the accept() function it works fine.
    > So i really cant figure out whats happeing to the stack here.
    > Why the address of sockets is being lost.
    >
    >
    >> In the call to accept(), you're casting signaddr to a 'struct
    >> sockaddr *' - which it already is ... struct sockaddr *signaddr

    >
    > I have tried passing signaddr without casting , but still giving the
    > same problem.
    > ****
    > if((clientsockfd = accept(listenfd,&signaddr,&addrlength))<0)
    > {
    > printf("\nSome Problem with accepting connection");
    > exit(EXIT_FAILURE);
    > }
    > ***
    >
    >
    >> But, you're passing a struct sockaddr ** by using &signaddr.

    >
    > I am working on Cygwin here. Can that be a problem. As when i used to
    > work on Solaris as far as i remember this did not
    > gave any problem.


    Well, you're now getting into the 'off topic slap' area here, so best be
    brief ...

    From the docs I've found ...
    int accept(int s, struct sockaddr *addr, socklen_t *addrlen);

    You shouldn't be passing a ** but a *, so change your code to:

    if((clientsockfd = accept(listenfd, signaddr, &addrlength)) < 0)

    I.e., no *&* on signaddr - and remove the cast.


    --
    ==============
    Not a pedant
    ==============
     
    pemo, Mar 1, 2006
    #7
  8. pemo Guest

    wrote:
    > Yes, you are right.
    >
    > Even if i use %p instead it gives
    >
    > The value is 0xc0ee60
    > The value is 0xc0ee60
    > Accepted a new Client connection
    > The value is 0x0Segmentation fault (core dumped)
    >
    > So %d was merely for diagnostic purpose to trace the problem (to check
    > if sockets hold the same thing after accept is called).
    >
    > You can read my Mind!!!
    > Great!!


    Oh, now you're getting into the 'what %p slap' area!

    Slap: Please post whatever it is that you're replying to.

    --
    ==============
    Not a pedant
    ==============
     
    pemo, Mar 1, 2006
    #8
  9. pemo Guest

    pemo wrote:
    > wrote:
    >> HI,
    >>
    >>
    >>> If my assumptions are correct [above], then I would *guess* that
    >>> something's screwing up the stack in your call to 'accept()' - if
    >>> you comment that out, the fprintf works ok?

    >>
    >> Yes you are right if i comment the accept() function it works fine.
    >> So i really cant figure out whats happeing to the stack here.
    >> Why the address of sockets is being lost.
    >>
    >>
    >>> In the call to accept(), you're casting signaddr to a 'struct
    >>> sockaddr *' - which it already is ... struct sockaddr *signaddr

    >>
    >> I have tried passing signaddr without casting , but still giving the
    >> same problem.
    >> ****
    >> if((clientsockfd = accept(listenfd,&signaddr,&addrlength))<0)
    >> {
    >> printf("\nSome Problem with accepting connection");
    >> exit(EXIT_FAILURE);
    >> }
    >> ***
    >>
    >>
    >>> But, you're passing a struct sockaddr ** by using &signaddr.

    >>
    >> I am working on Cygwin here. Can that be a problem. As when i used to
    >> work on Solaris as far as i remember this did not
    >> gave any problem.

    >
    > Well, you're now getting into the 'off topic slap' area here, so best
    > be brief ...
    >
    > From the docs I've found ...
    > int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
    >
    > You shouldn't be passing a ** but a *, so change your code to:
    >
    > if((clientsockfd = accept(listenfd, signaddr, &addrlength)) < 0)
    >
    > I.e., no *&* on signaddr - and remove the cast.



    Oh!

    *Ahem* - /and/, um, ensure that signaddr actually points to something!

    void accept_new_tcp_client(int *sockets, fd_set *readset, int listenfd)
    {
    // Nope!
    // struct sockaddr *signaddr;

    // Better!
    struct sockaddr signaddr;

    ...

    // And then ...
    if((clientsockfd = accept(listenfd, &signaddr, &addrlength)) < 0)

    --
    ==============
    Not a pedant
    ==============
     
    pemo, Mar 1, 2006
    #9
  10. Guest

    I have tried running as you suggested

    if((clientsockfd = accept(listenfd, signaddr, &addrlength)) < 0)

    but the accept function is returning -1 and its failing here.

    I have used a Wrapper function
    if((clientsockfd = Accept(listenfd, signaddr, &addrlength)) < 0)

    to see the diagnostics The function is:

    int Accept(int s, struct sockaddr *addr, int *addrlen)
    { int sockfd;
    int addrlen_value;

    if ( addrlen != NULL )
    addrlen_value = *addrlen;

    if ( (sockfd=accept(s,addr,addrlen)) < 0 ) {
    char *msg=0;
    fprintf(stderr,"In function Accept() :\n");
    fprintf(stderr,"accept() : returned %d, errno=%s (%s)\n",
    sockfd,strerrno(errno),strerror(errno));
    fprintf(stderr,"parameters : s = %d\n",s);
    fprintf(stderr," addr = %p\n",addr);
    fprintf(stderr," addrlen = %p\n",addrlen);
    if ( addrlen != NULL ) {
    fprintf(stderr," (*addrlen = %d before call, %d after
    call)\n",
    addrlen_value,*addrlen);
    }
    msg = diagnostic_info_for_accept(errno);
    if ( msg ) fprintf(stderr,"%s\n",msg);
    }
    return sockfd;
    }

    It Prints:
    *******
    accept() : returned -1, errno=EFAULT (Bad address)
    parameters : s = 4
    addr = 0x15
    addrlen = 0xc0d8d0
    (*addrlen = 4 before call, 16 after call)
    ******

    Cant figure out why???

    Cheers
    Vishal
     
    , Mar 1, 2006
    #10
  11. Guest

    > // Better!
    > struct sockaddr signaddr;
    > ...


    > // And then ...
    > if((clientsockfd = accept(listenfd, &signaddr, &addrlength)) < 0)


    Thanks that solved my Problem. I couldn't understand what made it work
    earlier.


    Cheers
    Vishal
     
    , Mar 1, 2006
    #11
  12. pemo Guest

    <> wrote in message
    news:...
    >> // Better!
    >> struct sockaddr signaddr;
    >> ...

    >
    >> // And then ...
    >> if((clientsockfd = accept(listenfd, &signaddr, &addrlength)) < 0)

    >
    > Thanks that solved my Problem.


    You're welcome.

    Please see http://cfaj.freeshell.org/google/ if you're using Google - or
    just *do* make sure that you include context when replying if you're using
    some other news group reader - otherwise you'll find most folks here won't
    be overly keen to help you out!

    --
    ==============
    Not a pedant
    ==============
     
    pemo, Mar 1, 2006
    #12
  13. "" <> writes:
    > I am having strange problem in my Program.
    >
    > I cannot paste the whole program as it is huge so just pasting the
    > lines i think are necessary.
    >
    > I am passing a integer array pointer to a function.
    >
    > **********
    >
    > int sockets[MAX_TCPCLIENTS]; /* an array of connected sockets*/
    > .....
    > .....
    > fprintf(stderr,"The value is %d",sockets);
    >
    > accept_new_tcp_client(sockets,&readset,listenfd);
    > ......
    > ......
    > void accept_new_tcp_client(int *sockets, fd_set *readset, int listenfd)
    > {
    > struct sockaddr *signaddr;
    > int addrlength;
    > int i;
    > int clientsockfd;
    > addrlength = sizeof(signaddr);
    >
    > fprintf(stderr,"\nThe value is %d",sockets);
    >
    > /*== First accept the new connection ==*/
    >
    > if((clientsockfd = accept(listenfd,(struct
    > sockaddr*)&signaddr,&addrlength))<0)
    > {
    > printf("\nSome Problem with accepting connection");
    > exit(EXIT_FAILURE);
    > }
    >
    > fprintf(stderr,"\nAccepted a new Client connection");
    >
    > fprintf(stderr,"\nThe value is %d",sockets);


    I think your problem was already solved elsewhere in the thread, but
    I'm going to comment on your code.

    Never use "%d" to print pointer values. Use "%p", and convert the
    pointer to void* (it's not guaranteed that all pointers have the same
    representation).

    Print a '\n' at the end of each message, not at the beginning.

    printf or fprintf statements can be useful for debugging, but they're
    more useful if you can tell them apart. You use "The value is" twice,
    making it difficult to tell which one generated a given message.

    You use the same name, "sockets", for a global array and for a pointer
    parameter; this is likely to cause confusion.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 1, 2006
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    CType(x,Integer) vs. Integer.Parse(x)

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,966
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Feb 7, 2006
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    No Math.min(Integer, Integer)?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 29, 2003, in forum: Java
    Replies:
    0
    Views:
    521
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Jul 29, 2003
  3. James Vanns
    Replies:
    7
    Views:
    7,088
    Evan Carew
    Jan 21, 2004
  4. Replies:
    4
    Views:
    1,121
  5. pozz
    Replies:
    45
    Views:
    2,841
    Dr Nick
    May 14, 2011
Loading...

Share This Page