Wednesday, August 31, 2016

Digest for comp.programming.threads@googlegroups.com - 5 updates in 2 topics

Sibin Thomas <sibinpthomas@gmail.com>: Aug 30 10:39PM -0700

Situation:
 
1. Thread 1 currently owns the mutex.
2. While Thread 1 retains ownership of the mutex, Thread 2 makes a request for the same lock.
3. Thread 1 unlocks the lock.
 
At this juncture can another thread (say, Thread 3) swoop in and make a request for the lock and acquire it (as shown in the image below)? Or does POSIX guarantee that a mutex shall be acquired by a thread that is already waiting for the said mutex at the moment of unlocking a mutex?
 
http://i.stack.imgur.com/yy937.png
 
The man page for `pthread_mutex_unlock()` states that -
 
> when pthread_mutex_unlock() is called, resulting in the mutex becoming
> available, the scheduling policy shall determine which thread shall
> acquire the mutex.
 
This seems to say that Thread 3 can not swoop in and acquire the mutex, though I am not fully assured.
Casper H.S. Dik <Casper.Dik@OrSPaMcle.COM>: Aug 31 08:24AM

>request for the lock and acquire it (as shown in the image below)? Or does
>POSIX guarantee that a mutex shall be acquired by a thread that is already
>waiting for the said mutex at the moment of unlocking a mutex?
 
Yes. Thread 3 can win.
 
>> acquire the mutex.
 
>This seems to say that Thread 3 can not swoop in and acquire the mutex,
>though I am not fully assured.
 
Why do you think the scheduling policy favors thread 2 over thread 3?
 
Thread 2 can be woken up but it may need to fetch its stack from
swap or at least its caches might be call; a running thread 3
has a lot of advantage over thread 2 and may swoop in and win.
 
Casper
Sibin Thomas <sibinpthomas@gmail.com>: Aug 31 04:08AM -0700

On Wednesday, 31 August 2016 13:54:36 UTC+5:30, Casper H.S. Dik wrote:
> swap or at least its caches might be call; a running thread 3
> has a lot of advantage over thread 2 and may swoop in and win.
 
> Casper
 
I was influenced by the discussion here - http://austingroupbugs.net/view.php?id=609
I thought similar to pthread_signal() pthread_mutex_unlock() too does an "atomic unblock" of threads already in its 'Wait queue'
Casper H.S. Dik <Casper.Dik@OrSPaMcle.COM>: Aug 31 11:41AM


>I was influenced by the discussion here - http://austingroupbugs.net/view.php?id=609
>I thought similar to pthread_signal() pthread_mutex_unlock() too does an "atomic unblock" of threads already in its 'Wait queue'
 
"atomic unblocking" does not mean that it will win the from other running threads.
 
But that doesn't actually happen in the case outside of a mutex; the mutex is unlocked
and of that point the it can be locked by other threads. The blocked threads, or
at least one of them, will be awoken. The lock is not handed to that thread.
 
Casper
bleachbot <bleachbot@httrack.com>: Aug 25 11:02PM +0200

You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.programming.threads+unsubscribe@googlegroups.com.

Tuesday, August 30, 2016

Digest for comp.lang.c++@googlegroups.com - 25 updates in 3 topics

Jerry Stuckle <jstucklex@attglobal.net>: Aug 30 08:47AM -0400

On 8/30/2016 8:01 AM, R.Wieser wrote:
 
> Any other ideas ?
 
> Regards,
> Rudy Wieser
 
Look at his update again. He isn't using the address of the first
member - he isn't referencing any member of the struct. You can take
the address of it with & and dereference it with *. For instance:
 
struct hostent server; // the actual structure
 
struct hostent* pserver = &server; // A pointer to the structure
 
At this level, it's really no different than other variables, i.e.
 
int i; // an int
int *ip = &i; // A pointer to an int
 
And you can use them just like other pointers, i.e.
 
void f(struct hostent * ps);
 
f(&server); // Use the address of the structure
f(pserver); // Use a pointer to the structure
 
Does this help?
 
And I agree with the others - you really need to find a good basic book
on C++ (or here, even C). It will help you much more than trying to
fumble your way through.
 
--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Aug 30 01:59PM +0100

On Tue, 30 Aug 2016 11:33:23 +0200
 
> Odd: several replies, but none that actually told me how its done.
> Is it some deep-dark secret that only masters of the trade are
> allowed to know ?
 
You have been told the answer (see the response of 29 August at
19:16:59 UTC). 'server', as in:
 
struct hostent *server;
 
is declared as a pointer and can be passed as a pointer. It _is_ a
pointer. You only need to dereference it if you want to obtain the
structure's members. There is no secret here. It is the most basic C.
 
You really do need to get a decent textbook on the C language. (Or
the C++ language if that is what you want to learn.)
 
> not appreciate further hints in the same direction though!)
 
> Do I really need to waste hours googeling hoping I will stumble over
> the (most likely rather simple) answer ?
 
Don't google. When learning a language get a textbook to learn the
basics.
 
Part of the confusion may be that when you said you were "a novice in
regard to using C++ (GCC) *and* using linux" people thought that it was
the specific gcc and linux issues you were unfamiliar with, not the
basics of C and C++ itself. You gave the impression that you had
done some windows programming.
 
The other difficulty is that you have an attitude problem which is
considerably larger than your state of knowledge; this is going to get
you into trouble, both in your code and in your interactions on this
news group.
 
Chris
"R.Wieser" <address@not.available>: Aug 30 03:14PM +0200

Paavo,
 
> I express my opinions, you express yours.
 
Nope. You use your "opinions" as an excuse not to post anything in regard
to an answer to the problem as I posted it. "Go away. Go learn for
yourself. If you do not you will never discover what its all about".
Really ? *Thats* how you help people ? In that case, I wonder what you
would do if you would *not* be willing to help ...
 
> Yes, that was concrete enough, thank you.
 
Good.
 
> 2) foobar(reinterpret_cast<unsigned char*>(server2));
 
Why are you writing it like that, when my posted code uses a different
make-up (syntax?) *AND* I've indicated I'm a beginner ?
 
But, lets assume you ment:
 
foobar( (unsigned char*) server2 );
 
Than you're mistaken (adding brackets around "server2" does not help
either).
 
Oh, it *compiles* well enough, but the argument in the foobar function will
than be pointing at the the value stored into that "server2" variable, not
the data in the "hostent" structure.
 
Also explain to me how currently
 
foobar( (unsigned char*) server2-> h_addr );
 
works for me, but I than can just remove the "->h_addr" part and get the
same (compile-time!) result ...
 
> A proper code sample is preferably a complete source
> file including all the #includes as well as int main().
 
Forget it. The only thing it would lead to is that you (and others like
you) would making suggestions about *everything* in there, but not the
problem. Heck, even now you have troubles with concentrating yourself on
the problem.
 
Over the years I've posted enough questions (few, but enough) to have
encountered the above many times. Shucks, why do you think I named the
function "foobar" instead of its actual name ? Thats because what it, and
the rest of the code does has got *nothing* to do with the *compile-time*
problem.
 
But, if you are amadant about only being able to glean the problem (and its
answer) from such full code I could waste some time to gut everything
irrelevant outof my current code (while ofcourse still causing the
compile-time error mind you). Apart from the includes for the standard IO,
the structure, the gethostbyname function, a minimal "main" declaration and
an as minimal "foobar" one (being fully gutted) you would see nothing more
than what I already posted.
 
By the way: have you already tried to put those three lines of code I posted
in my initial message into some test program and seen if you could get it to
compile ? Why not ? I think it would probably have been quite the
eye-opener for you.
 
Regards,
Rudy Wieser
 
 
-- Origional message:
Paavo Helde <myfirstname@osa.pri.ee> schreef in berichtnieuws
M6idnSfk5aQA6VjKnZ2dnUU78dvNnZ2d@giganews.com...
 
> >> I'm just saying that it is not productive to learn language
> >> basics from a newsgroup, it's too tedious and chaotic.
 
> > Please, let *me* be the judge of that. This is a tiny bit of
knowledge,
> > which, if solved, allows me to progress quite a bit.
 
> Why are not judging here anything, I express my opinions, you express
yours.
 
> > 2) struct hostent *server2;
 
> > For *both* of the above I want to give a pointer to the actual hostent
> > structure to a function called "foobar", which expects the argument to
be of
> using it in the same meaning.)
 
> >> and preferably with a real code sample, not a vague description ...
 
> > Thats what I posted and am using in my code. I'm sorry, but I have no
idea
Paavo Helde <myfirstname@osa.pri.ee>: Aug 30 04:22PM +0300

On 30.08.2016 16:14, R.Wieser wrote:
> yourself. If you do not you will never discover what its all about".
> Really ? *Thats* how you help people ? In that case, I wonder what you
> would do if you would *not* be willing to help ...
 
(plonk)
"R.Wieser" <address@not.available>: Aug 30 03:28PM +0200

Scott,
 
> The man pages are your friend.
 
:-) I was, just as under Windows, trying to go the opposite way -- from
function to documentation -- (Windows does not really have any decent "man"
page support), but yes, "man" seems to work rather well under Linux.
 
And thanks for the info-selection methods too.
 
Regards,
Rudy Wieser
 
 
-- Origional mesage:
Scott Lurndal <scott@slp53.sl.home> schreef in berichtnieuws
Gafxz.1214$LR.855@fx17.iad...
> >> to include stdio.h, use
 
> >> #include <stdio.h>
 
> >Thats exactly the problem. How do I know that I need to include stdio.h
and
> ...
 
> $ man 3 printf | head -20
> PRINTF(3) Linux Programmer's Manual
PRINTF(3)
 
> NAME
> printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf,
vsnprintf - formatted output conversion
"R.Wieser" <address@not.available>: Aug 30 03:22PM +0200

Scott,
 
> $ nm -D /usr/lib64/libzip.so.2 | grep " T " |more
 
Looks like its very usable, thanks. :-)
 
Regards,
Rudy Wieser
 
 
-- Origional message:
Scott Lurndal <scott@slp53.sl.home> schreef in berichtnieuws
s5fxz.1213$LR.467@fx17.iad...
 
> >As someone wo has spend quite some time on Windows I'm rather accustomed
> >being able to pick a DLL and look inside it to see which functions it
> >exposes that I can use (yeah, I still have to generate a library from it
and
"R.Wieser" <address@not.available>: Aug 30 03:30PM +0200

Scott,
 
 
> $ man 3 fprintf
 
> Section 1 is commands, 2 is system calls, 3 is library functions,
> 4 is device special files, etc.
 
Thanks again. Thats rather usefull (to make an understatement).
 
Regards,
Rudy Wieser
 
 
-- Origional message:
Scott Lurndal <scott@slp53.sl.home> schreef in berichtnieuws
Cdfxz.1215$LR.777@fx17.iad...
 
> >:-\ I had not thought of that.
> >...
 
> >Just tested it, and I had ofcourse (among a few others) to choose a
function
> >which clashes with a console command : write. :-( :-) Any idea what to
do
> 5 File formats and conventions eg /etc/passwd
> 6 Games
> 7 Miscellaneous (including macro packages and conventions), e.g.
man(7), groff(7)
Ben Bacarisse <ben.usenet@bsb.me.uk>: Aug 30 02:34PM +0100


> and
 
> struct hostent *server;
 
> I regard your above statement true for the first, not the latter.
 
No, the first is a variable (C and C++ call it an object) that holds the
structure. The second is an object that holds the address of a struct.
 
> I can tell for the latter "server" is the addres of the variable which holds
> the address to the "hostent" structure (hence the need for
> dereferencing).
 
If you just want the address of the hostent strict you just write server
because that variable already holds the address you want. But the code
you showed suggested a different intention:
 
foobar( (unsigned char*) server->h_addr, ... )
 
This suggest you might want to pass the address of the first member of
the list of discovered host addresses: h_addr is a legacy macro that
expands to h_addr_list[0]. If that's what you want, then you've got the
right code. You don't even need the cast if your function can take a
char * argument.
 
But, to repeat, if you just want the address of the whole hostent
struct, you already have it in server.
 
<snip>
--
Ben.
"R.Wieser" <address@not.available>: Aug 30 03:43PM +0200

Jerry,
 
> Does this help?
 
I'm sorry, no. I read the same, and thought I understood.
 
You seem to say that what he wrote works, while the compiler on my machine
still throws an compile-time error.
 
I can see that "server->h_addr" and "*server" *should* return the same
address. The problem is that I can't get the latter to be accepted (and, as
I'm a beginner, assumed I did something wrong), and I have *no* idea why.
 
Do you ?
 
Regards,
Rudy Wieser
 
 
Jerry Stuckle <jstucklex@attglobal.net> schreef in berichtnieuws
nq3v9u$sm2$1@jstuckle.eternal-september.org...
 
> >> For dereferencing you need to type '*server' (or 'server->' if you
> >> want access particular member of it).
 
> > In my first post I indicated I used the latter using the first member of
the
> > structure, but I consider that to be a hack. I also indicated that I
tried
"R.Wieser" <address@not.available>: Aug 30 03:51PM +0200

Scott,
 
> >Odd: several replies, but none that actually told me how its done. Is
it
> >some deep-dark secret that only masters of the trade are allowed to know
?
 
> Pointers are the most basic of concepts in C, and C++.
 
I should have added the "</sarcasm>" tag, but felt like I would be overdoing
it.
 
> Casting your 'server' variable to an unsigned char pointer
> is incorrect in either language.
 
Seems to be untrue. The compiler accepts "(unsigned char*) server" without
even a warning. Using that result does not seem to cause any problems
either.
 
Regards,
Rudy Wieser
 
 
-- Origional message:
Scott Lurndal <scott@slp53.sl.home> schreef in berichtnieuws
Fgfxz.1216$LR.419@fx17.iad...
 
> >> In short: How do I dereference that "server" variable so
> >> the function receives a pointer to the structure ?
 
> >Odd: several replies, but none that actually told me how its done. Is
it
> >some deep-dark secret that only masters of the trade are allowed to know
?
 
> Pointers are the most basic of concepts in C, and C++.
 
> >And for some of you guys here: don't try to second-guess what I'm after.
> >I'm in the habit of trying to ask my questions as exact as I can, needing
it
> >to have *it* answered. (that does not mean that I do not appreciate
further
 
> >Do I really need to waste hours googeling hoping I will stumble over the
> >(most likely rather simple) answer ?
 
> If you don't craft your question carefully, you won't get an useful
answer.
 
> The question, as originally posted, didn't really make sense in the
context
Reinhardt Behm <rbehm@hushmail.com>: Aug 30 10:01PM +0800

R.Wieser wrote:
 
 
> I can see that "server->h_addr" and "*server" should return the same
> address. The problem is that I can't get the latter to be accepted (and,
> as I'm a beginner, assumed I did something wrong), and I have no idea why.
 
Since we do not know what you function foo accepts as its parameter, we can
not really explain it. From what you write it seems it expect a pointer to a
char (char*). That would explain that the first form "server->h_addr" is
accepted by the compiler. But neither "*server" not "server" is a char
pointer. "*server" is a "struct hostent", assuming it has been set correctly
and "server" is a pointer to struct hostent.
 
To me it looks as if you do not know that in C (or C++) a pointer is not
just an address. It also has the property to point to a certain type.
 
So if your function foo expects a char pointer, meaning it is defined as
void foo(char *ptr);
You have to call it with a char pointer, not with a pointer to struct
anything.
This is true even if your variable "server" contains the address (as a bit
pattern) of the first byte of some struct hostent which by chance is also
the address of char or char array and the address of this char (server-
>h_addr) has the same bit pattern it is syntactically some thing different.
That willlead to your compiler complaining.
 
One tip: If you think it is to much effort to show something more of your
code,then just show us at least the definition of such functions because it
not irrelevant. Or show us the compiler message.

--
Reinhardt
scott@slp53.sl.home (Scott Lurndal): Aug 30 02:48PM


>Seems to be untrue. The compiler accepts "(unsigned char*) server" without
>even a warning. Using that result does not seem to cause any problems
>either.
 
Syntactically, perhaps. Semantically, it's catch as catch can - in this
case yours works by luck because the first field in the structure is
an array of characters.
 
If you have to cast something explicitly, you're generally doing the wrong thing.
 
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses */
}
 
struct hostent server;
 
void do_something_with_hostname(char *hostname);
 
 
do_something_with_hostname(server->h_name);
 
 
or
 
void do_something_with_hostent(struct hostent *); /* C definition */
void do_something_with_hostent(hostent *); /* C++ definition */
 
 
Note that the first field of struct hostent is a pointer to a
signed character. For historical reasons all strings in C and
C++ are considered to be arrays of signed characters, and the
compilers will complain if you attempt to pass server->h_name as
the argument of a function that takes 'unsigned char *' as an
argument.
 
From the GCC man page (only applies to the C compiler, not the C++ compiler)
 
Ideally, a portable program should always use "signed char" or "unsigned char" when it depends on
the signedness of an object. But many programs have been written to use plain "char" and expect it
to be signed, or expect it to be unsigned, depending on the machines they were written for. This
option, and its inverse, let you make such a program work with the opposite default.
Jerry Stuckle <jstucklex@attglobal.net>: Aug 30 11:00AM -0400

On 8/30/2016 9:43 AM, R.Wieser wrote:
 
> I can see that "server->h_addr" and "*server" *should* return the same
> address. The problem is that I can't get the latter to be accepted (and, as
> I'm a beginner, assumed I did something wrong), and I have *no* idea why.
 
Server->h_addr is a member of the structure. If it is an int, the code
will return an int. If it is a double, the code will return a double.
 
If it is an array, it will return the address of the first element in
the struct. This will be the same as the address of the struct - but
*only if this is the first element in the struct*. Should the struct
change at a later time, this will not be true (and will probably cause a
very difficult to find bug).
 
 
> Do you ?
 
> Regards,
> Rudy Wieser
 
No, because you haven't shown enough code. If the function takes a type
server*, then it should work as I explained. If it takes something
else, you shouldn't be passing a server* to it.
 
Function prototypes have a purpose - to indicate what type of parameters
the function accepts (and what it returns). Don't try to pass a B when
it expects an A. It doesn't work.
 
--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================
legalize+jeeves@mail.xmission.com (Richard): Aug 30 04:31PM

[Please do not mail me a copy of your followup]
 
"R.Wieser" <address@not.available> spake the secret code
 
>> Here 'server' is a variable holding the address of a
>> 'hostent' structure. server *is* the address.
 
>I'm sorry, but I disagree with you. [...]
 
Then you don't yet understand the fundamentals of the language. Take
some on-line courses on C or C++ and learn the fundamentals of
pointer types first.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
legalize+jeeves@mail.xmission.com (Richard): Aug 30 04:33PM

[Please do not mail me a copy of your followup]
 
"R.Wieser" <address@not.available> spake the secret code
>I'm in the habit of trying to ask my questions as exact as I can, needing it
>to have *it* answered. (that does not mean that I do not appreciate further
>hints in the same direction though!)
 
Please read "How to Ask Questions the Smart Way":
<http://www.catb.org/esr/faqs/smart-questions.html>
 
Your approach is directly at odds with getting the best way of
getting help from a programmer oriented newsgroup.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
jacobnavia <jacob@jacob.remcomp.fr>: Aug 30 07:36PM +0200

Le 30/08/2016 à 08:05, Gareth Owen a écrit :
 
> Actually, none of Jacob's "answers" are the most likely - what he knows
> about linux couldn't fill a hat. Doesn't stop him having opinions, of
> course.
 
I could say the same thing about you.
 
You do not know anything about me. I have been using linux since at
least 1995 or so.
 
But keep your opinion.
Manfred <noname@invalid.add>: Aug 30 08:10PM +0200

On 8/30/2016 11:33 AM, R.Wieser wrote:
>> the function receives a pointer to the structure ?
 
> Odd: several replies, but none that actually told me how its done. Is it
> some deep-dark secret that only masters of the trade are allowed to know ?
You had the correct answer, but you appeared to be too much busy
attacking the guy who gave you such correct answer in order to get it.
 
> hints in the same direction though!)
 
> Do I really need to waste hours googeling hoping I will stumble over the
> (most likely rather simple) answer ?
You can't hope to learn C and/or C++ by googleing around. Get a good
textbook instead.
Besides, many have already already given you the information you asked for.
But it looks as if your attitude got you convinced that they were all wrong.
 
Regards,
Manfred
"R.Wieser" <address@not.available>: Aug 30 07:32PM +0200

Jerry,
 
> No, because you haven't shown enough code. If the
> function takes a type server*, then it should work as
> I explained
 
Ehrmm .... In my first post in this thread I did show how I call the foobar
function, indicating I'm attempting to coerce the provided variable into an
unsigned char* before passing it on as the functions argument.
 
If the argument defined in the foobar function itself would be of a
different type I would (as far as I know) get a compile-time error because
of a mismatch. Hence, the function must look like this:
 
{undefined} foobar(unsigned char* argument , ....)
 
And it does. :-)
 
> Don't try to pass a B when it expects an A. It doesn't work.
 
Agreed.
 
Regards,
Rudy Wieser
 
 
-- Origional message:
 
Jerry Stuckle <jstucklex@attglobal.net> schreef in berichtnieuws
nq472u$mg3$1@jstuckle.eternal-september.org...
 
> >> Does this help?
 
> > I'm sorry, no. I read the same, and thought I understood.
 
> > You seem to say that what he wrote works, while the compiler on my
machine
> > still throws an compile-time error.
 
> > I can see that "server->h_addr" and "*server" *should* return the same
> > address. The problem is that I can't get the latter to be accepted
(and, as
> > I'm a beginner, assumed I did something wrong), and I have *no* idea
why.
"R.Wieser" <address@not.available>: Aug 30 08:34PM +0200

Richard,
 
> Then you don't yet understand the fundamentals of the language.
 
It could also be that I have a better understanding of what goes on below
than you do.
 
You see, you do not seem to understand the difference between a structures
address, and a variable *used as* a pointer *to hold* some other variables
address.
 
When you say "server *is* the address" you are right. But it is the address
of the "server" variable itself, not whatever it addres is stored as its
contents.
 
Granted, the language itself makes it easy for you to just forget such
fundamental things about it, and just throw something at it to get it
silently dereferenced to get at the actual structure.
 
Heck, why do you think both "." and "->" exist to access a strucures fields
?
 
Regards,
Rudy Wieser
 
 
-- Origional message:
Richard <legalize+jeeves@mail.xmission.com> schreef in berichtnieuws
nq4cdc$c2b$1@news.xmission.com...
> pointer types first.
> --
> "The Direct3D Graphics Pipeline" free book
<http://tinyurl.com/d3d-pipeline>
"R.Wieser" <address@not.available>: Aug 30 07:14PM +0200

Scott,
 
> Syntactically, perhaps. Semantically, it's catch as catch
> can - in this case yours works by luck because the first
> field in the structure is an array of characters.
 
Are you sure about that ?
 
Wait, two things:
 
One: if what you say is true than I should not need to call the function
including coercing the "server" variable with "(unsigned char*)"
 
Two: the percieved type of a structure is the same as that of its first
field ?
 
... testing ...
 
Nope on both. The compiler complains about "hostent*" not being compatible
to "unsigned char*". And that sound logical to me.
 
> If you have to cast something explicitly, you're generally
> doing the wrong thing.
 
Agreed. In this case though its intentional.
 
> struct hostent {
[snip]
 
Thanks for the examples.
 
Well, thats something else: The "foobar" routine is not only for that one
structure, or for structures only for that matter. Comparable to the
"write" function I guess. You can write *any* data with it, as long as you
coerce the data's type into the one "write"'s argument expects.
 
And yes, If I would have wanted to use a fields data as its stored in there
it stands to reason to receive that field in an argument of the exact same
type (ignoring signed/unsigned differences here).
 
Regards,
Rudy Wieser
 
 
-- Origional message:
Scott Lurndal <scott@slp53.sl.home> schreef in berichtnieuws
s1hxz.19182$IH3.2168@fx40.iad...
> "R.Wieser" <address@not.available> writes:
> >Scott,
 
> >> >Odd: several replies, but none that actually told me how its done.
Is
> >it
> >> >some deep-dark secret that only masters of the trade are allowed to
know
> >?
 
> >> Pointers are the most basic of concepts in C, and C++.
 
> >I should have added the "</sarcasm>" tag, but felt like I would be
overdoing
 
> >> Casting your 'server' variable to an unsigned char pointer
> >> is incorrect in either language.
 
> >Seems to be untrue. The compiler accepts "(unsigned char*) server"
without
> case yours works by luck because the first field in the structure is
> an array of characters.
 
> If you have to cast something explicitly, you're generally doing the wrong
thing.
> the argument of a function that takes 'unsigned char *' as an
> argument.
 
> From the GCC man page (only applies to the C compiler, not the C++
compiler)
 
> Ideally, a portable program should always use "signed char" or
"unsigned char" when it depends on
> the signedness of an object. But many programs have been written to
use plain "char" and expect it
> to be signed, or expect it to be unsigned, depending on the machines
they were written for. This
> option, and its inverse, let you make such a program work with the
opposite default.
"R.Wieser" <address@not.available>: Aug 30 08:53PM +0200

Manfred,
 
> You had the correct answer, but you appeared to be too
> much busy attacking the guy who gave you such correct
> answer in order to get it.
 
If the answer was correct, why didn't it work for me ?
 
And pardon me, but I think I have mentioned that at least the first few
times. After that ? Yeah, how many times do you think I should be
repeating the same answer (to the same suggestions) ?
 
> Besides, many have already already given you the information
> you asked for. But it looks as if your attitude got you convinced
> that they were all wrong.
 
You mean you didn't notice that I've mentioned a number of times I got a
compile-time error when I tried the suggestions ? Are you really trying
to shoot the *messenger* here ?
 
I tried *everything* that was suggested. I even retried a number of things
I had already done, because maybe I did something wrong the last time (on my
own) but *this* time (doing it exactly as suggested) it would work.
 
But there is one thing I simply cannot stand: And that is when I say that
something doesn't work the other party keeps insisting that it does <full
stop>.
 
Next to that, at the same time not getting any suggestions to what I could
try to find the problem (in my understanding or in the code) is not funny
either (And no, all that was needed for that was present in the first
message).
 
Regards,
Rudy Wieser
 
 
Manfred <noname@invalid.add> schreef in berichtnieuws
nq4i5q$ofh$1@gioia.aioe.org...
 
> >> In short: How do I dereference that "server" variable so
> >> the function receives a pointer to the structure ?
 
> > Odd: several replies, but none that actually told me how its done. Is
it
> > some deep-dark secret that only masters of the trade are allowed to know
?
> attacking the guy who gave you such correct answer in order to get it.
 
> > And for some of you guys here: don't try to second-guess what I'm after.
> > I'm in the habit of trying to ask my questions as exact as I can,
needing it
> > to have *it* answered. (that does not mean that I do not appreciate
further
 
> You can't hope to learn C and/or C++ by googleing around. Get a good
> textbook instead.
> Besides, many have already already given you the information you asked
for.
> But it looks as if your attitude got you convinced that they were all
wrong.
"R.Wieser" <address@not.available>: Aug 30 05:01PM +0200

Chris,
 
> 19:16:59 UTC). 'server', as in:
 
> struct hostent *server;
 
> is declared as a pointer and can be passed as a pointer.
 
And I've told this group several times that IT DOESN'T WORK.
 
You can keep giving me the same "this should work" as many times as you
like, but my compiler is rather uninpressed by this against-the-facts
insistance, laughs about it a bit and just keeps throwing the compile-time
error. :-\
 
I do wish you guys could convince him otherwise though. :-)
 
> _is_ a pointer. You only need to dereference it if you want
> to obtain the structure's members. There is no secret here.
> It is the most basic C.
 
Absolutily.
 
So, why don't you tell me why "server->h_addr" works, but "*server" won't ?
 
> "a novice in regard to using C++ (GCC) *and* using linux"
> people thought that it was the specific gcc and linux issues
> you were unfamiliar with, not the basics of C and C++ itself.
 
Irrelevant. I think the question is clear enough. Regardless of if its a
high-level problem, or one as basic as this one. Heck, as its a problem
that has to do with "the basics of C and C++ itself" *everyone* here (above
the novice level) should know what to do about it, don't you think ?
 
> You gave the impression that you had done some
> windows programming.
 
I have. I didn't mention which language though. :-)
 
I mentioned to indicate that I'm no stranger to the logic of programming, or
its syntax. That does not mean I know everything, or cannot run into
stupid "you have to do it exactly *this* way" problems when encountering a
new language.
 
> The other difficulty is that you have an attitude problem
> which is considerably larger than your state of knowledge;
 
You think *I* have an attitude problem, when being confronted with a slew of
people who do not read (advising me to try stuff I wrote already I did), and
do not take "No, it doesn't work" for an answer ? You must be joking. :-|
 
> this [attitude] is going to get you into trouble, both in your code
 
That "in your code" I do not quite understand. My code has no problem with
who I am, what I look like or how many keyboards I go thru in an average
session. :-)
 
> and in your interactions on this news group.
 
True. But I don't mind to get rid of people who only know one answer, and
keep repeating that same answer over-and-over again because they simply
refuse to accept that it doesn't work. *That* attitude is simply
infuriating.
 
... and I guess that my getting "rather annoyed" shows in my answers. :-)
:-\
 
 
But please do explain why "server->h_addr" works, but "*server" not. I
could really use it. If it simply doesn't (for whatever reason) I will be
content too (though you stand a chance that I will say "Ha! Told you so!"
to several of this threads contributors. Sorry.)
 
Regards,
Rudy Wieser
 
P.s.
I think I gave enough code in my first post to reproduce the problem.
Currently I have not heard of anyone trying and either having a pro or con
experience. Odd.
 
 
-- Origional message:
Chris Vine <chris@cvine--nospam--.freeserve.co.uk> schreef in berichtnieuws
20160830135918.3d0861d6@dell.homenet...
Lynn McGuire <lynnmcguire5@gmail.com>: Aug 30 12:24PM -0500

On 8/29/2016 1:52 PM, Daniel wrote:
 
> I think the Unicode people have vastly simplified many people's lives.
 
> Admittedly, C++ has badly screwed this up.
 
> Daniel
 
Microsoft has a big hand in the screw up with their decision to go with wide characters. But the Unicode consortium is getting wacky
here lately with adding emoticons to Unicode.
http://blog.unicode.org/2016/06/unicode-90-emoji-available-for-adoption.html
 
Lynn
legalize+jeeves@mail.xmission.com (Richard): Aug 30 06:50PM

[Please do not mail me a copy of your followup]
 
>On 8/29/2016 1:52 PM, Daniel wrote:
>> Admittedly, C++ has badly screwed this up.
 
I think you have your history backwards. C and C++ predate unicode.
 
The general consensus seems to be that you should use std::string with
UTF-8 encoding to add basic unicode support. You can always get
fancier if you need it with Qt, wxWidgets, UCS, etc.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
mark <mark@invalid.invalid>: Aug 30 05:23PM +0200

On 2016-08-30 13:28, Chris Vine wrote:
> T t2 = c->t; // this is all you can do with C
 
> But there may be something else in the standard which precludes even
> this.
 
My reading of the standard is that this works if C (and all its members)
has a trivial default constructor. If not, it's undefined behavior. If C
has a trivial default constructor, storage allocation is enough to start
the lifetime, otherwise a constructor has to run.
 
§3.08/5 Object lifetime:
<<<
Before the lifetime of an object has started but after the storage which
the object will occupy has been allocated or, after the lifetime of an
object has ended and before the storage which the object occupied is
reused or released, any pointer that refers to the storage location
where the object will be or was located may be used but only in limited
ways. [...] Indirection through such a pointer is permitted but the
resulting lvalue may only be used in limited ways, as described below.
The program has undefined behavior if:
[...]
- the pointer is used to access a non-static data member or call a
non-static member function of the object, or
[...]
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

Digest for comp.lang.c++@googlegroups.com - 25 updates in 3 topics

Gareth Owen <gwowen@gmail.com>: Aug 30 07:05AM +0100

>> find multiple (sometimes over 10) same-named header files , most often of
>> different sizes. Whats the idea behind that, and how should I know which
>> one of those to include ?
 
The compiler knows, so you don't have to. If you want to include
stdio.h, use
 
#include <stdio.h>
 
> Ahhh welcome to linux. Actually there is no way to know.
 
There are lots of ways to know. The package manager will know,
 
> You can have different headers if you have installed different
> compilers or different versions of the same compiler, or just some
> hacks that didn't go well and left some headers... who knows?
 
Ask the package manager. The package manager knows.
 
Actually, none of Jacob's "answers" are the most likely - what he knows
about linux couldn't fill a hat. Doesn't stop him having opinions, of
course.
 
The most likely answer is that they all belong to one compiler.
On my system I have
 
/usr/include/stdio.h
/usr/include/c++/4.8/tr1/stdio.h
/usr/include/x86_64-linux-gnu/bits/stdio.h
 
all of which belong to the gcc version I have installed. (I also have
the ARM-versions for the ARM compiler, in folders with names like
arm-xilinx-eabi).
 
When I do
#include <stdio.h>
the compiler does the right thing.
"R.Wieser" <address@not.available>: Aug 30 11:12AM +0200

jacobnavia,
 
 
>Ahhh welcome to linux. Actually there is no way to know.
...
> Now, you can ask the compiler which headers is it using, if
> memory serves gcc had a command line option to show the
> paths its using.
 
Ah, that makes some sense. I seem to remember having seen something like
that (a reference to a cache of some sort).
 
Thanks for the hint.
 
Regards,
Rudy Wieser
 
 
-- Origional message:
jacobnavia <jacob@jacob.remcomp.fr> schreef in berichtnieuws
nq28u0$al3$1@dont-email.me...
> Le 29/08/2016 à 21:16, R.Wieser a écrit :
> > And now I'm on a roll asking questions, one thing I did not expect was
to
> > find multiple (sometimes over 10) same-named header files , most often
of
> > different sizes. Whats the idea behind that, and how should I know
which
"R.Wieser" <address@not.available>: Aug 30 10:57AM +0200

Paavo,
 
> Anyway, if you have troubles with basic syntax like this,
> then I suggest to start with a proper textbook or tutorial.
 
Thanks, but al you seem to be saying is "go away". Thanks bub.
 
Is it really so hard to dereference something that it can't be described in
few lines ?
 
> But anyway, first google search shows there are things
> like objdump and readelf which might be useful for
> examining so-s.
 
Using 20-20 vision, sure. Mind you, I gave you a fair warning that I'm a
*beginner* (at least in respect to Linux and C{something} ). I *assume*
that library files are the .o files in the "lib" branch, but am not even
sure about that.
 
Also, how do you think I found that "nm" I spoke of ?
 
Regards,
Rudy Wieser
 
 
-- Origional message:
Paavo Helde <myfirstname@osa.pri.ee> schreef in berichtnieuws
vvidnecFA7cvB1nKnZ2dnUU78QXNnZ2d@giganews.com...
> On 29.08.2016 22:16, R.Wieser wrote:
> > Hello all,
 
> > Fair warning: I'm a novice in regard to using C++ (GCC) *and* using
linux
> > (lubuntu to be exact). If this is not the newsgroup to be asking
questions
 
> > - - - - - -
> > First problem, C++ related:
 
> > I need to transfer the address of a structure to a function, and have
only
> > be able to find a hackish solution for it. The problem is that the
variable
> > I've got is actually a pointer, and I have no idea how to dereference
it.
> > server = gethostbyname(argv[1]);
 
> > Now I want to transfer the address stored in the "server" variable to a
> > function. The only way I was able to do that was to refer to the first
(and
 
> > As someone wo has spend quite some time on Windows I'm rather accustomed
> > being able to pick a DLL and look inside it to see which functions it
> > exposes that I can use (yeah, I still have to generate a library from it
and
 
> > To be honest, I have no idea if the above is even feasible for C++ (GCC)
> > under linux.
 
> > I did find an example using "nm", but that only showed the contents of
the
 
> man fprintf
 
> for example.
 
> > And now I'm on a roll asking questions, one thing I did not expect was
to
> > find multiple (sometimes over 10) same-named header files , most often
of
> > different sizes. Whats the idea behind that, and how should I know
which
"R.Wieser" <address@not.available>: Aug 30 10:43AM +0200

嘱 Tiib,
 
> Yes, but it seemed that he wanted host addresses as strings
> from that hostent structure.
 
Nope, I just want the address of the structure itself. Thats all.
 
> it is not clear what programming language
 
I'm not sure myself, but I assume that its C++, seeing that let G++ compile
the sourcecode.
 
> and in what way he used on Windows.
 
I didn't. I'm doing it on a Linux variant.
 
Regards,
Rudy Wieser
 
 
-- Origional message
嘱 Tiib <ootiib@hot.ee> schreef in berichtnieuws
242b3c44-f5a2-4bed-b019-447827e5f856@googlegroups.com...
 
> > "R.Wieser" <address@not.available> spake the secret code
> > <57c4895a$0$871$e4fe514c@news.xs4all.nl> thusly:
 
> > >I need to transfer the address of a structure to a function, and have
only
> > >be able to find a hackish solution for it. The problem is that the
variable
> > >I've got is actually a pointer, and I have no idea how to dereference
it.
"R.Wieser" <address@not.available>: Aug 30 10:31AM +0200

Andy,
 
> As someone who started using Linux not many years ago
> after many years of other systems, which included a _lot_
> of Windows - I've almost never had to do that on Windows
 
:-) Who said I was using C{something} on Windows :-p
 
But consider my request both as an attempt to get a bit more knowledge about
how stuff works, as well as creating independance from the "they" that
should make the header and library files for me.
 
But, in the first place its only an attempt to generate a list with all
available functions that I can use in a program. Currently the only way to
know if a certain function (that I know exists in Windows) is available in
gcc/linux is by stumbling over it in an example program. And that isn't
acceptable to me.
 
Regards,
Rudy Wieser
 
 
-- Origional message:
Vir Campestris <vir.campestris@invalid.invalid> schreef in berichtnieuws
4vSdnVhKxMTWCFnKnZ2dnUU78afNnZ2d@brightview.co.uk...
> > As someone wo has spend quite some time on Windows I'm rather accustomed
> > being able to pick a DLL and look inside it to see which functions it
> > exposes that I can use (yeah, I still have to generate a library from it
and
"R.Wieser" <address@not.available>: Aug 30 10:09AM +0200

Richard,
 
> Here 'server' is a variable holding the address of a
> 'hostent' structure. server *is* the address.
 
I'm sorry, but I disagree with you. Just consider the difference between
 
struct hostent server;
 
and
 
struct hostent *server;
 
I regard your above statement true for the first, not the latter. As far as
I can tell for the latter "server" is the addres of the variable which holds
the address to the "hostent" structure (hence the need for dereferencing).
 
Regards,
Rudy Wieser
 
 
-- Origional message:
Richard <legalize+jeeves@mail.xmission.com> schreef in berichtnieuws
nq21nb$ead$1@news.xmission.com...
 
> "R.Wieser" <address@not.available> spake the secret code
> <57c4895a$0$871$e4fe514c@news.xs4all.nl> thusly:
 
> >I need to transfer the address of a structure to a function, and have
only
> >be able to find a hackish solution for it. The problem is that the
variable
 
> "server is a pointer to a hostent structure"
 
> --
> "The Direct3D Graphics Pipeline" free book
<http://tinyurl.com/d3d-pipeline>
"R.Wieser" <address@not.available>: Aug 30 11:17AM +0200

Gareth,
 
> The compiler knows, so you don't have to. If you want
> to include stdio.h, use
 
> #include <stdio.h>
 
Thats exactly the problem. How do I know that I need to include stdio.h and
not foobar.h to be able to use a certain function ?
 
> There are lots of ways to know. The package manager will know,
 
What has the packet manager to do with anything ? *I'm* writing a program,
and *I* need to add the correct includes.
 
Regards,
Rudy Wieser
 
 
- Origional message:
Gareth Owen <gwowen@gmail.com> schreef in berichtnieuws
87wpiyokq8.fsf@gmail.com...
> jacobnavia <jacob@jacob.remcomp.fr> writes:
 
> > Le 29/08/2016 à 21:16, R.Wieser a écrit :
> >> And now I'm on a roll asking questions, one thing I did not expect was
to
> >> find multiple (sometimes over 10) same-named header files , most often
of
> >> different sizes. Whats the idea behind that, and how should I know
which
Ian Collins <ian-news@hotmail.com>: Aug 30 09:19PM +1200

On 08/30/16 09:17 PM, R.Wieser wrote:
 
>> #include <stdio.h>
 
> Thats exactly the problem. How do I know that I need to include stdio.h and
> not foobar.h to be able to use a certain function ?
 
man <certain function>
 
--
Ian
"R.Wieser" <address@not.available>: Aug 30 11:33AM +0200

Funny:
 
> In short: How do I dereference that "server" variable so
> the function receives a pointer to the structure ?
 
Odd: several replies, but none that actually told me how its done. Is it
some deep-dark secret that only masters of the trade are allowed to know ?
 
And for some of you guys here: don't try to second-guess what I'm after.
I'm in the habit of trying to ask my questions as exact as I can, needing it
to have *it* answered. (that does not mean that I do not appreciate further
hints in the same direction though!)
 
Do I really need to waste hours googeling hoping I will stumble over the
(most likely rather simple) answer ?
 
Regards,
Rudy Wieser
Paavo Helde <myfirstname@osa.pri.ee>: Aug 30 12:38PM +0300

On 30.08.2016 11:57, R.Wieser wrote:
 
>> Anyway, if you have troubles with basic syntax like this,
>> then I suggest to start with a proper textbook or tutorial.
 
> Thanks, but al you seem to be saying is "go away". Thanks bub.
 
I'm just saying that it is not productive to learn language basics from
a newsgroup, it's too tedious and chaotic. If you get stuck with some
more concrete problem then you can always come back here, and preferably
with a real code sample, not a vague description using wrong terms.
 
> Is it really so hard to dereference something that it can't be described in
> few lines ?
 
You wanted to get a pointer, that's basically the opposite of
dereferencing. Besides, your original question was answered immediately
by Richard so there was no need to repeat this answer.
 
If you have a pointer called 'server', then for dereferencing it you use
'*server' or 'server->', depending on the exact usage. For passing the
pointer without dereferencing you just use 'server'.
 
> *beginner* (at least in respect to Linux and C{something} ). I *assume*
> that library files are the .o files in the "lib" branch, but am not even
> sure about that.
 
No, library files are called .so and .a. I'm sorry I mentioned objdump
and readelf, I should have just said that examining the library symbols
is a rare need and definitely not something a beginner should do at all.
 
Cheers
Paavo
Paavo Helde <myfirstname@osa.pri.ee>: Aug 30 12:44PM +0300

On 30.08.2016 12:17, R.Wieser wrote:
 
>> #include <stdio.h>
 
> Thats exactly the problem. How do I know that I need to include stdio.h and
> not foobar.h to be able to use a certain function ?
 
From the function documentation of course. How else could you know how
to use the function at all?
 
If you want to use fprintf, then type man fprintf in the Linux command
line or google search and the correct #include is among the first things
it says:
 
fprintf(3) - Linux man page
Name
 
printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf,
vsnprintf - formatted output conversion
 
Synopsis
 
#include <stdio.h>
 
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
 
NRHTH
Paavo
"Öö Tiib" <ootiib@hot.ee>: Aug 30 03:55AM -0700

On Tuesday, 30 August 2016 12:14:44 UTC+3, R.Wieser wrote:
 
> I regard your above statement true for the first, not the latter. As far as
> I can tell for the latter "server" is the addres of the variable which holds
> the address to the "hostent" structure (hence the need for dereferencing).
 
Unfortunately you seem to add one indirection that isn't there. In former
'server' is variable that contains all data of structure 'hostent'. To get
address to it you need to type '&server'.
 
In latter 'server' is pointer variable that can contain address of structure
'hostent'. When it is pointing at actual 'hostent' then you may dereference it.
For dereferencing you need to type '*server' (or 'server->' if you want access
particular member of it).
"R.Wieser" <address@not.available>: Aug 30 01:34PM +0200

Paavo,
 
> I'm just saying that it is not productive to learn language
> basics from a newsgroup, it's too tedious and chaotic.
 
Please, let *me* be the judge of that. This is a tiny bit of knowledge,
which, if solved, allows me to progress quite a bit.
 
> If you get stuck with some more concrete problem ...
 
I don't know how much more concrete it can become as what I described.
 
But maybe the following problem casing will help you:
 
two definitions:
 
1) struct hostent server1;
 
2) struct hostent *server2;
 
For *both* of the above I want to give a pointer to the actual hostent
structure to a function called "foobar", which expects the argument to be of
type unsigned char*.
 
Describe the calling method for *both*. (I'm rather sure that my answer
lies in the difference between the two).
 
Concrete enough for you ?
 
> and preferably with a real code sample, not a vague description ...
 
Thats what I posted and am using in my code. I'm sorry, but I have no idea
what "code samples" look like in your reality.
 
> ... using wrong terms.
 
See below:
 
> You wanted to get a pointer, that's basically the opposite of
> dereferencing.
 
And you talk about vague ?
 
Yes, I want to "get a pointer". But I want to move *towards* the structure,
not *away* from it. And as far as I'm aware thats indeed called
"dereferencing"
 
> Besides, your original question was answered immediately
> by Richard so there was no need to repeat this answer.
 
Bull. And if you think otherwise than quote it.
 
All he did was quoting the first line of my code sample. Yeah, thats
really helpfull. Not.
 
> then for dereferencing it you use '*server' or 'server->',
> depending on the exact usage
 
Also bull. Both
 
foobar( (unsigned char*) *server , ... )
 
as well as
 
foobar( (unsigned char*) server-> , ... )
 
throw a compile-time error (invalid cast for the first, unqualified
identifier for the second). And FYI, I have tried the former before posting
here (didn't think the latter having any chance of succeeding, so I didn't
even try it).
 
> No, library files are called .so and .a.
 
Thanks. So, I was looking in the wrong files. That could explain a lot.
:-)
 
Regards,
Rudy Wieser
 
 
 
-- Origional message:
Paavo Helde <myfirstname@osa.pri.ee> schreef in berichtnieuws
_LidnQTXd5SPyVjKnZ2dnUU78LPNnZ2d@giganews.com...
> more concrete problem then you can always come back here, and preferably
> with a real code sample, not a vague description using wrong terms.
 
> > Is it really so hard to dereference something that it can't be described
in
> >> like objdump and readelf which might be useful for
> >> examining so-s.
 
> > Using 20-20 vision, sure. Mind you, I gave you a fair warning that I'm
a
> > *beginner* (at least in respect to Linux and C{something} ). I
*assume*
"R.Wieser" <address@not.available>: Aug 30 01:48PM +0200

Ian,
 
> man <certain function>
 
:-\ I had not thought of that.
...
 
Just tested it, and I had ofcourse (among a few others) to choose a function
which clashes with a console command : write. :-( :-) Any idea what to do
in such cases ?
 
Regards,
Rudy Wieser
 
 
-- Origional message:
Ian Collins <ian-news@hotmail.com> schreef in berichtnieuws
e2l1dgFec6gU3@mid.individual.net...
> >> to include stdio.h, use
 
> >> #include <stdio.h>
 
> > Thats exactly the problem. How do I know that I need to include stdio.h
and
Paavo Helde <myfirstname@osa.pri.ee>: Aug 30 02:57PM +0300

On 30.08.2016 14:34, R.Wieser wrote:
>> basics from a newsgroup, it's too tedious and chaotic.
 
> Please, let *me* be the judge of that. This is a tiny bit of knowledge,
> which, if solved, allows me to progress quite a bit.
 
Why are not judging here anything, I express my opinions, you express yours.
 
> structure to a function called "foobar", which expects the argument to be of
> type unsigned char*.
 
> Concrete enough for you ?
 
Yes, that was concrete enough, thank you. Here are the answers to these
questions.
 
1) foobar(reinterpret_cast<unsigned char*>(&server1));
 
2) foobar(reinterpret_cast<unsigned char*>(server2));
 
(side note: this is C++ so you probably need to compile it with g++, not
gcc).
 
(side note 2: there is no dereferencing here, & is the address-of
operator. The term "dereferencing" has very specific meaning in C and
C++ and if you use it in a C or C++ forum everybody assumes you are
using it in the same meaning.)
 
 
>> and preferably with a real code sample, not a vague description ...
 
> Thats what I posted and am using in my code. I'm sorry, but I have no idea
> what "code samples" look like in your reality.
 
A proper code sample is preferably a complete source file including all
the #includes as well as int main(). This makes it easy for other people
to try to compile it and understand where the problems appear.
 
See e.g.
 
https://groups.google.com/forum/#!topic/comp.lang.c++/VaDCk_oujMg
"R.Wieser" <address@not.available>: Aug 30 02:01PM +0200

嘱 Tiib,
 
> In former 'server' is variable that contains all data of structure
> 'hostent'. To get address to it you need to type '&server'.
 
Agreed.
 
> In latter 'server' is pointer variable that can contain address
> of structure 'hostent'.
 
Again, agreed.
 
> When it is pointing at actual 'hostent' then you may dereference it.
 
YES. How do I do that ?
 
> For dereferencing you need to type '*server' (or 'server->' if you
> want access particular member of it).
 
In my first post I indicated I used the latter using the first member of the
structure, but I consider that to be a hack. I also indicated that I tried
"(*server)", and that it gave me a compile-time error.
 
Any other ideas ?
 
Regards,
Rudy Wieser
 
 
-- Origional message:
嘱 Tiib <ootiib@hot.ee> schreef in berichtnieuws
6fe758df-bf42-430f-be92-88c7455378f3@googlegroups.com...
 
> > > Here 'server' is a variable holding the address of a
> > > 'hostent' structure. server *is* the address.
 
> > I'm sorry, but I disagree with you. Just consider the difference
between
 
> > and
 
> > struct hostent *server;
 
> > I regard your above statement true for the first, not the latter. As
far as
> > I can tell for the latter "server" is the addres of the variable which
holds
> > the address to the "hostent" structure (hence the need for
dereferencing).
> 'server' is variable that contains all data of structure 'hostent'. To get
> address to it you need to type '&server'.
 
> In latter 'server' is pointer variable that can contain address of
structure
> 'hostent'. When it is pointing at actual 'hostent' then you may
dereference it.
> For dereferencing you need to type '*server' (or 'server->' if you want
access
scott@slp53.sl.home (Scott Lurndal): Aug 30 12:36PM

>being able to pick a DLL and look inside it to see which functions it
>exposes that I can use (yeah, I still have to generate a library from it and
>find the function declarations, but thats not the point).
 
$ nm -D /usr/lib64/libzip.so.2 | grep " T " |more
scott@slp53.sl.home (Scott Lurndal): Aug 30 12:42PM


>> #include <stdio.h>
 
>Thats exactly the problem. How do I know that I need to include stdio.h and
>not foobar.h to be able to use a certain function ?
 
The man pages are your friend.
 
$ man -k printf
asprintf (3) - print to allocated string
dprintf (3) - print to a file descriptor
format (n) - Format a string in the style of sprintf
fprintf (3) - formatted output conversion
fprintf (3p) - print formatted output
fwprintf (3) - formatted wide-character output conversion
fwprintf (3p) - print formatted wide-character output
printf (1) - format and print data
printf (1p) - write formatted output
printf (3) - formatted output conversion
printf (3p) - print formatted output
snprintf (3) - formatted output conversion
snprintf (3p) - print formatted output
sprintf (3) - formatted output conversion
sprintf (3p) - print formatted output
swprintf (3) - formatted wide-character output conversion
...
 
$ man 3 printf | head -20
PRINTF(3) Linux Programmer's Manual PRINTF(3)
 
 
 
NAME
printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - formatted output conversion
 
SYNOPSIS
#include <stdio.h>
 
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);
 
#include <stdarg.h>
 
int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
 
Or you can use the appropriate standard document, for example:
 
http://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html
scott@slp53.sl.home (Scott Lurndal): Aug 30 12:45PM


>Just tested it, and I had ofcourse (among a few others) to choose a function
>which clashes with a console command : write. :-( :-) Any idea what to do
>in such cases ?
 
If you don't already know what manual section it is in, use
man -k to find all the relevent pages:
 
$ man -k fprintf
asprintf (3) - print to allocated string
dprintf (3) - print to a file descriptor
format (n) - Format a string in the style of sprintf
fprintf (3) - formatted output conversion
fprintf (3p) - print formatted output
fwprintf (3) - formatted wide-character output conve
 
Then use the section number:
 
$ man 3 fprintf
 
Section 1 is commands, 2 is system calls, 3 is library functions,
4 is device special files, etc.
 
1 Executable programs or shell commands
2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special files (usually found in /dev)
5 File formats and conventions eg /etc/passwd
6 Games
7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
8 System administration commands (usually only for root)
9 Kernel routines [Non standard]
Jerry Stuckle <jstucklex@attglobal.net>: Aug 30 08:47AM -0400

On 8/30/2016 8:01 AM, R.Wieser wrote:
 
> Any other ideas ?
 
> Regards,
> Rudy Wieser
 
Look at his update again. He isn't using the address of the first
member - he isn't referencing any member of the struct. You can take
the address of it with & and dereference it with *. For instance:
 
struct hostent server; // the actual structure
 
struct hostent* pserver = &server; // A pointer to the structure
 
At this level, it's really no different than other variables, i.e.
 
int i; // an int
int *ip = &i; // A pointer to an int
 
And you can use them just like other pointers, i.e.
 
void f(struct hostent * ps);
 
f(&server); // Use the address of the structure
f(pserver); // Use a pointer to the structure
 
Does this help?
 
And I agree with the others - you really need to find a good basic book
on C++ (or here, even C). It will help you much more than trying to
fumble your way through.
 
--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================
scott@slp53.sl.home (Scott Lurndal): Aug 30 12:48PM

>> the function receives a pointer to the structure ?
 
>Odd: several replies, but none that actually told me how its done. Is it
>some deep-dark secret that only masters of the trade are allowed to know ?
 
Pointers are the most basic of concepts in C, and C++.
 
>hints in the same direction though!)
 
>Do I really need to waste hours googeling hoping I will stumble over the
>(most likely rather simple) answer ?
 
If you don't craft your question carefully, you won't get an useful answer.
 
The question, as originally posted, didn't really make sense in the context
of C or C++ programming. Casting your 'server' variable to an unsigned
char pointer is incorrect in either language.
Piotr Wyderski <piotr.wyderski.no.spam@gmail.com>: Aug 30 09:25AM +0200

Paavo Helde wrote:
 
> T*' are already non-portable enough so that additional cast to const C*
> would probably be the safest step here (assuming standard layout, T as
> the first member and equal sizeof-s).
 
Yes, you understood the use case correctly. It is all about bulk loading
-- I want to load a big chunk of disk data (not even memory-mapping the
file, full-blown asynchronous IO operations will be used instead) and
then apply some non-mutating operations on this array of Ts as if it
were an array of Cs. In the moment of content creation it was an array
of Cs, so technically it is a sequence of reinterpret_casts
C[]=>T[]=>C[], which just happened in different runs of the same
program. The content does not have to be portable, it will never leave
the machine. I just wanted it to be as C++-conforming as possible.
Since C++ appears to know something about the standard_layout
peculiarities, it seems doable without excessive violations of the standard.
 
Best regards, Piotr
"Öö Tiib" <ootiib@hot.ee>: Aug 30 03:30AM -0700

On Tuesday, 30 August 2016 10:26:08 UTC+3, Piotr Wyderski wrote:
> Since C++ appears to know something about the standard_layout
> peculiarities, it seems doable without excessive violations of the standard.
 
> Best regards, Piotr
 
Add something like that into code after that struct C is defined:
 
static_assert( std::is_standard_layout<C>::value
&& std::is_arithmetic<T>::value
&& std::is_same<T, decltype(C::t)>::value
&& sizeof (T[42]) == sizeof (C[42])
, "need to cast between T[] and C[]" );
 
That double-checks the situation in paranoid manner and so documents
why it is important and makes sure that some future maintenance does
not accidentally break it. Files on disk tend to live longer than versions
of software.
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Aug 30 12:28PM +0100

On Tue, 30 Aug 2016 09:25:59 +0200
> C++-conforming as possible. Since C++ appears to know something about
> the standard_layout peculiarities, it seems doable without excessive
> violations of the standard.
 
This looks like a very bad idea. If what you have committed to disk
is a binary representation of an array of T objects of some kind, you
cannot recreate your C objects from this in a portable fashion because
the C objects no longer exist. You could only do the reverse portably.
The operative word is "portably" - if sizeof C is the same as sizeof T
you will probably get away with it.
 
By virtue of the sixth bullet of §3.10/10 read with §9.2/20, given
a type T which enables struct C { T t; int i;}; to be both an aggregate
and a standard layout struct, and an object of type T:
 
T t;
 
what you can do on my reading of the standard is this:
 
C* c = reinterpret_cast<C*>(&t); // OK so far, but be very afraid
T t2 = c->t; // this is all you can do with C
 
But there may be something else in the standard which precludes even
this.
 
Obviously this would not allow you to treat your array of T objects as
if it were an array of C objects.
 
The correct thing to do is to deal with your T objects as T objects.
 
Chris
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Aug 30 02:39AM +0100

int main(int argc, char* argv[])
{
/* Introducing "indexitor", a new C++ container... */
 
std::vector<int> v;
for (int i = 0; i < 15; ++i)
v.push_back(i + 700);
 
neolib::indexitor<std::string, std::size_t> idx;
 
idx.push_back(std::make_pair("first", 5));
idx.push_back(std::make_pair("second", 5));
idx.push_back(std::make_pair("third", 5));
 
/* idx is holding three elements which map to first 5,
second 5 and third 5 elements respectively of a foreign container. */
 
/* Now for primary use-case: modify foreign container: */
v.erase(v.begin() + 2); // remove third element
auto ii = idx.begin();
idx.update_foreign_index(idx.begin(), 5 - 1); // update indexitor
 
for (std::size_t i = 0; i < v.size(); ++i)
std::cout << i << ": " << idx.find_by_foreign_index(i)->first << " ->
" << v[i] << std::endl;
 
/* All indexitor index/update operations are worst-case O(lg N)
complexity. */
}
 
Output:
 
0: first -> 700
1: first -> 701
2: first -> 703
3: first -> 704
4: second -> 705
5: second -> 706
6: second -> 707
7: second -> 708
8: second -> 709
9: third -> 710
10: third -> 711
11: third -> 712
12: third -> 713
13: third -> 714
 
Download (at own risk as not yet fully tested) from:
 
https://github.com/FlibbleMr/neolib
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.