473,395 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Really odd pointer behavior

Hello all,

Please advise...

Consider the following...

void init_sockaddr (struct sockaddr_in *name, const char *hostname,int port)
{
struct hostent *hostinfo;
name->sin_family = AF_INET;
name->sin_port = htons (port);
hostinfo = gethostbyname (hostname);

#if ONEWAY
name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
#endif

#if ANOTHERWAY
bcopy(hostinfo->h_addr, (char*)&(name->sin_addr), hostinfo->h_length);
#endif
}

As I understand, there will be problems with the first way.
Because once the CPU leaves the function, hostinfo might be lost.
For now, it works, but I imagine it might one day fail.

My bigger concern is why it even compiles?
What is the "*" to the left of the "(" in that line?
What is it doing?
Admittedly, it was a mistake that worked, but I am more interested
in
Nov 14 '05 #1
3 1365
<snip>
void init_sockaddr (struct sockaddr_in *name, const char *hostname,int port) {
struct hostent *hostinfo;
name->sin_family = AF_INET;
name->sin_port = htons (port);
hostinfo = gethostbyname (hostname);

#if ONEWAY
name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
#endif

#if ANOTHERWAY
bcopy(hostinfo->h_addr, (char*)&(name->sin_addr), hostinfo->h_length);
#endif
}

As I understand, there will be problems with the first way.
Because once the CPU leaves the function, hostinfo might be lost.
For now, it works, but I imagine it might one day fail.
You mean once the function reaches its end / returns, in which case Yes
information
in the 'hostinfo' is lost.
My bigger concern is why it even compiles?
Whats wrong in compiling ? Its common to declare local variables only to use
it locally inside
the function and once you return from the function you forget about those..
What is the "*" to the left of the "(" in that line?
What is it doing? * -> dereferencing pointer.
It dereferences the value in hostinfo->h_addr and assigns to name->sin_addr
(input parameter)
Admittedly, it was a mistake that worked, but I am more interested
in

Nov 14 '05 #2
In article <cKhOc.1241$wz.1020@fed1read01>,
"Mr. X" <gr**********@cox.net> wrote:
Hello all,

Please advise...

Consider the following...

void init_sockaddr (struct sockaddr_in *name, const char *hostname,int port)
{
struct hostent *hostinfo;
name->sin_family = AF_INET;
name->sin_port = htons (port);
hostinfo = gethostbyname (hostname);

#if ONEWAY
name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
#endif

#if ANOTHERWAY
bcopy(hostinfo->h_addr, (char*)&(name->sin_addr), hostinfo->h_length);
#endif
}

As I understand, there will be problems with the first way.
Because once the CPU leaves the function, hostinfo might be lost.
So? You've copied the data out of it into the structure that name
points to, so you don't care about hostinfo any more.
For now, it works, but I imagine it might one day fail.

My bigger concern is why it even compiles?
What is the "*" to the left of the "(" in that line?
What is it doing?


It's indirecting through the pointer. hostinfo->h_addr is an array,
which is automatically converted to a pointer in this context. (struct
in_addr *) casts that pointer to a pointer to an in_addr structure. The
* at the beginning means to access the contents of the structure that it
points to, so this structure is copied into name->sin_addr.

--
Barry Margolin, ba****@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Nov 14 '05 #3
On Thu, 29 Jul 2004 18:44:55 -0700, "Mr. X" <gr**********@cox.net>
wrote:
Hello all,

Please advise...

Consider the following...

void init_sockaddr (struct sockaddr_in *name, const char *hostname,int port)
In standard C int may be as little as 16-bits signed, which -max or -0
excluded, hence unable to correctly represent all 16-bit port numbers
used in IP. The correct type for a port number, and for an argument to
htons, is unsigned short, or more formally uint16_t which almost
always will be typedef'ed to unsigned short.
{
struct hostent *hostinfo;
name->sin_family = AF_INET;
name->sin_port = htons (port);
hostinfo = gethostbyname (hostname);
Should check hostinfo is non-null befure using it; there are lots of
reasons gethostbyname can fail, in which cases it returns null.
#if ONEWAY
name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
#endif
This could fail if hostinfo->h_addr points to memory that is not
correctly aligned for a struct in_addr; what alignment is required, if
any, is platform dependent. In practice it likely will be, given the
variety of ways the provider may need to construct it, but I don't
believe anything (in particular SUS/POSIX) guarantees it.
#if ANOTHERWAY
bcopy(hostinfo->h_addr, (char*)&(name->sin_addr), hostinfo->h_length);
#endif
bcopy is not standard C, although it is likely to be available on
(most? all?) systems that have (BSDish) sockets. memcpy is, and takes
pointers to void which are converted automatically without casting.
That is the approach guaranteed to work always. Well, as long as the
implementation has a correct <netdb.h>, <arpa/inet.h>, gethostbyname,
etc. which as far as the C standard is concerned could so something
completely different from their normal Unix/sockets meaning.
}

As I understand, there will be problems with the first way.
Because once the CPU leaves the function, hostinfo might be lost.
For now, it works, but I imagine it might one day fail.
Not for that reason. Yes, the pointer hostent is local to this
function and lost when it returns, and more to the point, as it were,
what it points to, i.e. the value returned from successful gethostY,
plus the data pointed to by h_addr_list and h_addr (which is
h_addr_list[0] on modern systems) may be overwritten by other getX et
al. But you have already copied the data to the sockaddr_in provided
by the caller when this function returns so those don't matter, except
for thread-unsafety if in a multithread environment.
My bigger concern is why it even compiles?
What is the "*" to the left of the "(" in that line?
What is it doing?
That is taking h_addr (which as noted is really h_addr_list[0]), which
is a pointer to void, which means effectively "unspecified" type, and
casting it to a pointer to in_addr, which is what the data provided by
gethostbyname actually is, and *(cast)ptr fetches that data, which is
then stored in (assigned to a field of) the caller-provided struct.
Admittedly, it was a mistake that worked, but I am more interested
in


- David.Thompson1 at worldnet.att.net
Nov 14 '05 #4

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

Similar topics

18
by: Joe Seigh | last post by:
Is there a good write on this. The textbooks I have fluff over on this? Specifically, I trying to dereference with 2 levels of type conversion not 1, i.e. X<T> -> z => Y<T> -> z => T* ->...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
2
by: jalkadir | last post by:
I am trying to get character string from the user, to do that I use getline(char_type*, streamsize), but I get a segmentation fault??!! Can anyone give me a hand, what am I doing wrong? --snip...
38
by: Martin Marcher | last post by:
Hi, I've read several questions and often the answer was 'C knows nothing about .' So if C knows that little as some people say, what are the benefits, I mean do other languages know more...
11
by: Mantorok Redgormor | last post by:
Is const really constant? And on an OT note: how can I post with a modified e-mail address so I don't get so much spam?
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
23
by: Kenneth Brody | last post by:
Given the following: char *ptr1, *ptr2; size_t n; ptr2 = ptr1 + n; Assuming ptr1 is a valid pointer, is the following guaranteed to be true? (ptr2 - ptr1) == n
25
by: dis_is_eagle | last post by:
Hi.I have a question on the following statement. char* a="hello"; The question is where "hello" gets stored.Is it in some static area ,stack or heap.I have observed that attempting to modify...
15
by: Steven T. Hatton | last post by:
I found the example code below, listed in the book described here: http://cartan.cas.suffolk.edu/moin/OopDocbookWiki The result was a bit surprising. I guess it falls into the category of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.