473,799 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,listen fd);
.......
.......
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((clientsockf d = accept(listenfd ,(struct
sockaddr*)&sign addr,&addrlengt h))<0)
{
printf("\nSome Problem with accepting connection");
exit(EXIT_FAILU RE);
}

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 2293
ma***********@g mail.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,listen fd);
......
......
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((clientsockf d = accept(listenfd ,(struct
sockaddr*)&sign addr,&addrlengt h))<0)
{
printf("\nSome Problem with accepting connection");
exit(EXIT_FAILU RE);
}

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***********@g mail.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***********@g mail.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((clientsockf d = accept(listenfd ,&signaddr,&add rlength))<0)
{
printf("\nSome Problem with accepting connection");
exit(EXIT_FAILU RE);
}
***

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***********@g mail.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((clientsockf d = accept(listenfd ,&signaddr,&add rlength))<0)
{
printf("\nSome Problem with accepting connection");
exit(EXIT_FAILU RE);
}
***

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((clientsockf d = accept(listenfd , signaddr, &addrlength) ) < 0)

I.e., no *&* on signaddr - and remove the cast.
--
==============
Not a pedant
==============
Mar 1 '06 #7
ma***********@g mail.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***********@g mail.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((clientsockf d = accept(listenfd ,&signaddr,&add rlength))<0)
{
printf("\nSome Problem with accepting connection");
exit(EXIT_FAILU RE);
}
***

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((clientsockf d = 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((clientsockf d = accept(listenfd , &signaddr, &addrlength) ) < 0)

--
==============
Not a pedant
==============
Mar 1 '06 #9
I have tried running as you suggested

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

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

I have used a Wrapper function
if((clientsockf d = 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),strerro r(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(err no);
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10188
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
1
1986
by: Dr_Z2A | last post by:
Ok so a couple months ago I wrote a currency converter and never got it to compile (my memorization of syntax sucked then) and I just had the printed out copy lying in a stack of papers. So I found it the other day and decided to fix it up. I have one problem that I can't figure out in it still. When I run the program it prompts for the option number as it is supposed to, but when I enter in the number it outputs that I have entered an...
6
5001
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
12
6907
by: Dennis D. | last post by:
Hello: I want a function to return three variables to the calling procedure: Private Function CalcTimes(ByVal iAddDays as Integer, ByVal iAddHours as Integer, ByVal iAddMins as Integer) As Array Variable values are calculated in the function. Calling procedure receives the values preferably into variables of the same
2
1947
by: Jacques Wentworth | last post by:
Hi I'm trying to pass a collection from a .NET DLL to a VB6 app. I get a error 13 (type mismatch) when I do so. I passed back a boolean variable without any problems, but when I try the collection I get the error. As you can see the function does not do anything at the moment, so it must be some issue passing the collection. Thanks
33
2871
by: Martin Jørgensen | last post by:
Hi, In continuation of the thread I made "perhaps a stack problem? Long calculations - strange error?", I think I now got a "stable" error, meaning that the error always seem to come here now (tried: visual studio 2005 + linux/macintosh gcc)... That's a pretty good thing. I think the error still appears using both gcc and visual studio 2005. Everything is standard C (ANSI C ?? I don't know the difference) - but since so many functions...
3
5054
by: Geoff | last post by:
In this code I am trying to pass an image, as a parameter, out of the procedure PickCard. The code will not support this. How do I pass an image as a parameter? Thanks again Geoff
4
2517
by: Ram | last post by:
Hi All, Firstly i am a newbie and trying to learn C. The background of the problem is Program: Presently I am working on a program of numerology and the I/P will be the name and output will be a digit for which there are known characteristics which i will print.
14
2750
by: pgfdbug | last post by:
First I am learning this as I go so please forgive my ignorance, all is self taught. I was given this as program to run an LED signboard for my fire station. The program is supposed to transmit the calls to the signboard so we can see what piece is due to go out the door. The problem is I am getting several warnings and also the program is sending multi-colors instead of just one. Any help is greatly appreciated. betabrite.c In...
0
9544
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10259
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.