Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Help with passing integer poniter to function

Reply
Thread Tools

Help with passing integer poniter to function

 
 
manochavishal@gmail.com
Guest
Posts: n/a
 
      03-01-2006
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

 
Reply With Quote
 
 
 
 
pemo
Guest
Posts: n/a
 
      03-01-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
==============


 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      03-01-2006
(E-Mail Removed) 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

 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      03-01-2006
Nick Keighley wrote:
> (E-Mail Removed) 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
==============


 
Reply With Quote
 
manochavishal@gmail.com
Guest
Posts: n/a
 
      03-01-2006
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

 
Reply With Quote
 
manochavishal@gmail.com
Guest
Posts: n/a
 
      03-01-2006
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

 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      03-01-2006
(E-Mail Removed) 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
==============


 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      03-01-2006
(E-Mail Removed) 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
==============


 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      03-01-2006
pemo wrote:
> (E-Mail Removed) 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
==============


 
Reply With Quote
 
manochavishal@gmail.com
Guest
Posts: n/a
 
      03-01-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing an integer to a function that accepts a void pointer pozz C Programming 45 05-14-2011 08:41 AM
CType(x,Integer) vs. Integer.Parse(x) =?Utf-8?B?Sm9l?= ASP .Net 7 02-07-2006 02:30 AM
How do I add an Integer to another Integer? Sebastian Stelzer Java 2 10-15-2004 01:17 PM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM
No Math.min(Integer, Integer)? =?ISO-8859-1?Q?Thomas_Gagn=E9?= Java 0 07-29-2003 07:46 PM



Advertisments