469,279 Members | 2,216 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,279 developers. It's quick & easy.

Help with passing integer poniter to function

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 '06 #1
12 2038
ma***********@gmail.com 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
==============
Mar 1 '06 #2
ma***********@gmail.com 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

Mar 1 '06 #3
Nick Keighley wrote:
ma***********@gmail.com 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
==============
Mar 1 '06 #4
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 '06 #5
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 '06 #6
ma***********@gmail.com 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
==============
Mar 1 '06 #7
ma***********@gmail.com 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
==============
Mar 1 '06 #8
pemo wrote:
ma***********@gmail.com 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
==============
Mar 1 '06 #9
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 '06 #10
> // 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 '06 #11

<ma***********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
// 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
==============

Mar 1 '06 #12
"ma***********@gmail.com" <ma***********@gmail.com> 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) ks***@mib.org <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.
Mar 1 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dr_Z2A | last post: by
12 posts views Thread by Dennis D. | last post: by
2 posts views Thread by Jacques Wentworth | last post: by
33 posts views Thread by Martin Jørgensen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.