Help with passing integer poniter to function

M

manochavishal

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
 
P

pemo

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.
 
N

Nick Keighley

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

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.
 
P

pemo

Nick said:
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

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?
 
M

manochavishal

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
 
M

manochavishal

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
 
P

pemo

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.
 
P

pemo

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.
 
P

pemo

pemo said:
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)
 
M

manochavishal

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
 
K

Keith Thompson

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.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top