Sunday, February 28, 2010

comp.lang.c - 26 new messages in 7 topics - digest

comp.lang.c
http://groups.google.com/group/comp.lang.c?hl=en

comp.lang.c@googlegroups.com

Today's topics:

* initialize for a 2-D dynamic array. - 3 messages, 2 authors
http://groups.google.com/group/comp.lang.c/t/10b972fd49323b34?hl=en
* fputs and fprintf - 10 messages, 7 authors
http://groups.google.com/group/comp.lang.c/t/d665d0bf528f7433?hl=en
* Error with threads opening files - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c/t/2dc4e1c41436509b?hl=en
* Various ways to get month name from inbuilt C library - 2 messages, 1 author
http://groups.google.com/group/comp.lang.c/t/7aa38ff82f37f58e?hl=en
* usage of size_t - 3 messages, 2 authors
http://groups.google.com/group/comp.lang.c/t/19e0ad96d01b9898?hl=en
* More quirks importing from bsd - 4 messages, 2 authors
http://groups.google.com/group/comp.lang.c/t/3879f22a8a1b80ee?hl=en
* What is the explanation? - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c/t/43491a16c11fd4e0?hl=en

==============================================================================
TOPIC: initialize for a 2-D dynamic array.
http://groups.google.com/group/comp.lang.c/t/10b972fd49323b34?hl=en
==============================================================================

== 1 of 3 ==
Date: Sun, Feb 28 2010 12:16 am
From: Seebs


On 2010-02-28, MBALOVER <mbalover9@gmail.com> wrote:
> I want to create a 2D dynamic array and then initialize all elements
> with 0.

Really?

Hmm.

> I did a search and found:

> 1. array = (** unsigned char) calloc(NROW, sizeof(unsigned char*));

This is clearly incorrect.

> 2. for(i = 0; i < NROW ; i++)
> 3. {
> 4. array[i] = calloc(NCOL, sizeof(unsigned char));
> 5. }

Are NROW and NCOL known values? If they are, then you can do this much more
simply.

> I am wondering if I can replace calloc in line 1 by malloc function
> and still get a 2D array with all array[i][j]=0.

No, because malloc doesn't guarantee initialized memory. In general, you
can't allocate a multidimensional array of unknown size.

> By the way, I am wondering if malloc will be faster than calloc?

Maybe, maybe not.

> In my understanding, calloc is equivalent to malloc plus zero-
> initialization. Therefore calloc may be slower than malloc. Is it
> right?

Maybe. Honestly: You are not at a stage where you should be trying to
figure things like that out. Understand what they do, don't get caught
up trying to figure out what's "faster". Write for clarity and
understanding for now -- you can work on speed once you can make programs
that do what you want them to do.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


== 2 of 3 ==
Date: Sun, Feb 28 2010 3:12 am
From: ike@localhost.claranet.nl (Ike Naar)


In article <a188e3d4-29ff-4a4e-a716-807d837c749e@y17g2000yqd.googlegroups.com>,
MBALOVER <mbalover9@gmail.com> wrote:
>I want to create a 2D dynamic array and then initialize all elements
>with 0.
>
>I did a search and found:
>
>1. array = (** unsigned char) calloc(NROW, sizeof(unsigned char*));
>
>2. for(i = 0; i < NROW ; i++)
>3. {
>4. array[i] = calloc(NCOL, sizeof(unsigned char));
>5. }
>
>I am wondering if I can replace calloc in line 1 by malloc function
>and still get a 2D array with all array[i][j]=0.

You can; initializing each array[i] with all-bytes-zero in line 1
is not useful since you immediately overwrite each array[i] with a
pointer value in lines 2-5.
In face, even if you wouldn't overwrite each array[i] with a pointer value,
initializing each array[i] with all-bytes-zero would not be very useful,
because all-bytes-zero is not guaranteed to represent a valid pointer.

So I agree that it's better to replace calloc in line 1 by malloc.


== 3 of 3 ==
Date: Sun, Feb 28 2010 3:31 am
From: ike@localhost.claranet.nl (Ike Naar)


In article <slrnhok9jn.mqh.usenet-nospam@guild.seebs.net>,
Seebs <usenet-nospam@seebs.net> wrote:
>On 2010-02-28, MBALOVER <mbalover9@gmail.com> wrote:
>> I want to create a 2D dynamic array and then initialize all elements
>> with 0.
>
>Really?
>Hmm.
>
>> I did a search and found:
>> 1. array = (** unsigned char) calloc(NROW, sizeof(unsigned char*));
>
>This is clearly incorrect.

Why? It looks like mbalover is constructing a "dynamic 2D array"
as an array of pointers, pointing to 1D arrays. What's incorrect
about that? It's a well-known idiom.

>> 2. for(i = 0; i < NROW ; i++)
>> 3. {
>> 4. array[i] = calloc(NCOL, sizeof(unsigned char));
>> 5. }
>
>Are NROW and NCOL known values? If they are, then you can do this much more
>simply.

You mean, like "char array[NROW][NCOL]"? That's indeed a possibility,
but apparently not what mbalover wants (perhaps he wants to be able to
later resize his "dynamic 2D array", something he can't do with a
regular 2D array).

>> I am wondering if I can replace calloc in line 1 by malloc function
>> and still get a 2D array with all array[i][j]=0.
>
>No, because malloc doesn't guarantee initialized memory. In general, you
>can't allocate a multidimensional array of unknown size.

I think you are using the term "multidimensional array" in a too
restrictive way in this context.

>> By the way, I am wondering if malloc will be faster than calloc?
>
>Maybe, maybe not.
>
>> In my understanding, calloc is equivalent to malloc plus zero-
>> initialization. Therefore calloc may be slower than malloc. Is it
>> right?
>
>Maybe. Honestly: You are not at a stage where you should be trying to
>figure things like that out. Understand what they do, don't get caught
>up trying to figure out what's "faster". Write for clarity and
>understanding for now -- you can work on speed once you can make programs
>that do what you want them to do.

Agreed; the speed difference between calloc and malloc (if there is one,
and if it is measurable) is probably the last thing to worry about.

==============================================================================
TOPIC: fputs and fprintf
http://groups.google.com/group/comp.lang.c/t/d665d0bf528f7433?hl=en
==============================================================================

== 1 of 10 ==
Date: Sun, Feb 28 2010 12:26 am
From: _JusSx_


Hi,
I don't know if the question is in newsgroup FAQ because I haven't found
it yet and so I haven't read it yet.

I would like to know what differences are between these two
C functions: *fputs* and *fprintf*.

fputs C function has been used much in coreutils C source code while fprintf
has been used to printf help or error.

$ grep fputs *.c

#v+
base64.c: fputs (_("\
base64.c: fputs (_("\
base64.c: fputs (_("\
base64.c: fputs (_("\
base64.c: if (fputs ("\n", out) < 0)
base64.c: if (wrap_column && current_column > 0 && fputs ("\n", out) < 0)
basename.c: fputs (_("\
basename.c: fputs (HELP_OPTION_DESCRIPTION, stdout);
basename.c: fputs (VERSION_OPTION_DESCRIPTION, stdout);
cat.c: fputs (_("\
cat.c: fputs (_("\
cat.c: fputs (HELP_OPTION_DESCRIPTION, stdout);
cat.c: fputs (VERSION_OPTION_DESCRIPTION, stdout);
cat.c: fputs (_("\
chcon.c: fputs (_("\
...
#v-

#v+
$ grep fprintf *.c

base64.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
basename.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
cat.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
chcon.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
chgrp.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
...
#v-

Does using one function depend on only programmer's taste?


Thanks in advance

-JusSx-

--
Linux is only free if your time has no value


== 2 of 10 ==
Date: Sun, Feb 28 2010 12:43 am
From: santosh


_JusSx_ <jussx0NOSPAM@gmail.com.invalid> writes:

> Hi,
> I don't know if the question is in newsgroup FAQ because I haven't
> found it yet and so I haven't read it yet.

Google could've helped you...

<http://c-faq.com/>

> I would like to know what differences are between these two
> C functions: *fputs* and *fprintf*.

fprintf does formatted output. That is, it reads and interprets a
format string that you supply and writes to the output stream the
results. You can use it to print the values of nearly all of C's
object types.

fputs simply writes the string you supply it to the indicated output
stream.

<http://www.dinkumware.com/manuals/?manual=compleat&page=stdio.html>
<http://www.dinkumware.com/manuals/?manual=compleat&page=lib_over.html>

and man fprintf/fputs on your system too.

> Does using one function depend on only programmer's taste?

On the programmer's requirements, more than taste. fprintf is the way
to go for writing out the values of the various supported types,
unless you have your own version. fputs doesn't do that, but it's
fine for simply writing C strings.


== 3 of 10 ==
Date: Sun, Feb 28 2010 12:46 am
From: Seebs


On 2010-02-28, _JusSx_ <jussx0NOSPAM@gmail.com.invalid> wrote:
> I would like to know what differences are between these two
> C functions: *fputs* and *fprintf*.

One of them formats output, one prints a string without formatting it.

> Does using one function depend on only programmer's taste?

No.

Have you considered the idea of getting some kind of documentation, book,
or anything like that? If "man fprintf" doesn't tell you anything, your
system is misconfigured. (I'm assuming something unixy because you're
looking at coreutils.)

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


== 4 of 10 ==
Date: Sun, Feb 28 2010 12:54 am
From: Richard Heathfield


_JusSx_ wrote:
> Hi,
> I don't know if the question is in newsgroup FAQ because I haven't found
> it yet and so I haven't read it yet.
>
> I would like to know what differences are between these two
> C functions: *fputs* and *fprintf*.

fputs writes a string to the output stream, and you have to know the
string up front; whereas the data that fprintf writes can be built at
run-time:

Contrast:

fputs("now is the time for all good men to party\n", stderr);

with:

fprintf(stderr,
"now is the time for %d good men to party\n",
goodmenqty);

<snip>

> Does using one function depend on only programmer's taste?

No, they have different purposes. If you just want to write a single
fixed string to the output stream, fputs can do this quickly and without
fuss. But if your data needs are more complex, fputs isn't up to it, and
that's the time to look at fprintf.

(Some people just use fprintf for everything and be done with it. That's
a perfectly workable strategy.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within


== 5 of 10 ==
Date: Sun, Feb 28 2010 2:09 am
From: _JusSx_


On 2010-02-28, santosh <santosh.k83@gmail.com> wrote:
> _JusSx_ <jussx0NOSPAM@gmail.com.invalid> writes:
>
>> Hi,
>> I don't know if the question is in newsgroup FAQ because I haven't
>> found it yet and so I haven't read it yet.
>
> Google could've helped you...
>
><http://c-faq.com/>
>
> ...

Thank you very much. I have bookmarked c-faq link. I will give a look
at it.

-JusSx-


--
Linux is only free if your time has no value


== 6 of 10 ==
Date: Sun, Feb 28 2010 2:08 am
From: _JusSx_


On 2010-02-28, Seebs <usenet-nospam@seebs.net> wrote:
> On 2010-02-28, _JusSx_ <jussx0NOSPAM@gmail.com.invalid> wrote:
>> I would like to know what differences are between these two
>> C functions: *fputs* and *fprintf*.
>
> One of them formats output, one prints a string without formatting it.
>
>> Does using one function depend on only programmer's taste?
>
> No.
>
> Have you considered the idea of getting some kind of documentation, book,
> or anything like that? If "man fprintf" doesn't tell you anything, your
> system is misconfigured. (I'm assuming something unixy because you're
> looking at coreutils.)
>
> -s

Yes, I read fputs and fprintf man pages but maybe I didn't read them
carefully.

Now I know the difference is: fprintf prints formatted output, fputs
doesn't print formatted output.

I thought fputs printed formatted output like fprint does but I was
wrong.

-JusSx-

--
Linux is only free if your time has no value


== 7 of 10 ==
Date: Sun, Feb 28 2010 3:13 am
From: pete


_JusSx_ wrote:
> Hi,
> I don't know if the question is in newsgroup FAQ because I haven't found
> it yet and so I haven't read it yet.
>
> I would like to know what differences are between these two
> C functions: *fputs* and *fprintf*.
>
> fputs C function has been used much in coreutils C source code while fprintf
> has been used to printf help or error.
>
> $ grep fputs *.c
>
> #v+
> base64.c: fputs (_("\
> base64.c: fputs (_("\
> base64.c: fputs (_("\
> base64.c: fputs (_("\
> base64.c: if (fputs ("\n", out) < 0)
> base64.c: if (wrap_column && current_column > 0 && fputs ("\n", out) < 0)
> basename.c: fputs (_("\
> basename.c: fputs (HELP_OPTION_DESCRIPTION, stdout);
> basename.c: fputs (VERSION_OPTION_DESCRIPTION, stdout);
> cat.c: fputs (_("\
> cat.c: fputs (_("\
> cat.c: fputs (HELP_OPTION_DESCRIPTION, stdout);
> cat.c: fputs (VERSION_OPTION_DESCRIPTION, stdout);
> cat.c: fputs (_("\
> chcon.c: fputs (_("\
> ...
> #v-
>
> #v+
> $ grep fprintf *.c
>
> base64.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
> basename.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
> cat.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
> chcon.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
> chgrp.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
> ...
> #v-
>
> Does using one function depend on only programmer's taste?

fputs is a much simpler function than fprintf is.
I once advised a colleague that he might be able to shrink his code size
if he was able to replace all of his fprintf calls with fputs calls.
It worked, which was good because at that time
he did need to shrink his code size.

--
pete


== 8 of 10 ==
Date: Sun, Feb 28 2010 4:21 am
From: "bartc"


"_JusSx_" <jussx0NOSPAM@gmail.com.invalid> wrote in message
news:slrn.20100228090849.601@news.eternal-september.org...
> Hi,
> I don't know if the question is in newsgroup FAQ because I haven't found
> it yet and so I haven't read it yet.
>
> I would like to know what differences are between these two
> C functions: *fputs* and *fprintf*.

fputs/puts prints a string, while fprintf/printf prints a (format) string
*and* any number of other values.

But one important difference is that puts writes a newline at the end, while
printf requires you to insert the fiddly \n sequence at the end of the
string (significant for terrible typists like me, with \ being one of those
keys that is in a different place on every keyboard).

--
Bartc

== 9 of 10 ==
Date: Sun, Feb 28 2010 4:46 am
From: Nick Keighley


On 28 Feb, 08:26, _JusSx_ <jussx0NOS...@gmail.com.invalid> wrote:


> I don't know if the question is in newsgroup FAQ because I haven't found
> it yet and so I haven't read it yet.
>
> I would like to know what differences are between these two
> C functions: *fputs* and *fprintf*.

<snip>

the FAQ is at
http://c-faq.com/

but the FAQ won't help with your question, you need a C library
reference for that
http://www.dinkumware.com/manuals/#Standard C Library


== 10 of 10 ==
Date: Sun, Feb 28 2010 5:00 am
From: pete


bartc wrote:

> fputs/puts prints a string,
> while fprintf/printf prints a (format) string
> *and* any number of other values.
>
> But one important difference is that
> puts writes a newline at the end, while
> printf requires you to insert the fiddly \n sequence at the end of the
> string (significant for terrible typists like me,
> with \ being one of those
> keys that is in a different place on every keyboard).

Another important difference is that
puts writes a newline at the end, while fputs doesn't.

--
pete

==============================================================================
TOPIC: Error with threads opening files
http://groups.google.com/group/comp.lang.c/t/2dc4e1c41436509b?hl=en
==============================================================================

== 1 of 2 ==
Date: Sun, Feb 28 2010 12:41 am
From: Richard Heathfield


lancer6238@yahoo.com wrote:
> [...] each thread only opens (using a FILE *fin declared in
> Process()) and works on a portion of the data files using a
> start_index and an end_index. For example, if there are 100 files,
> then each thread will handle filename[0] to filename[24], filename[25]
> to filename[49], filename[50] to filename[74] and filename[75] to
> filename[99] respectively. After they're done, there is a pthread_join
> in main() for all 4 threads.
>
> I have checked that the filenames have been stored correctly, both in
> main() and Process(). However, I keep getting segmentation fault here,
> in Process():
>
> for (i = start_index, i <= end_index ; i++)
> fin = fopen(filename[i], "rb"); <--- Seg fault

Given the tiny amount of information available, my best guess can only
be a guess:

s/<=/<//

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within


== 2 of 2 ==
Date: Sun, Feb 28 2010 2:52 am
From: Nick <3-nospam@temporary-address.org.uk>


Richard Heathfield <rjh@see.sig.invalid> writes:

> lancer6238@yahoo.com wrote:
>> [...] each thread only opens (using a FILE *fin declared in
>> Process()) and works on a portion of the data files using a
>> start_index and an end_index. For example, if there are 100 files,
>> then each thread will handle filename[0] to filename[24], filename[25]
>> to filename[49], filename[50] to filename[74] and filename[75] to
>> filename[99] respectively. After they're done, there is a pthread_join
>> in main() for all 4 threads.
>>
>> I have checked that the filenames have been stored correctly, both in
>> main() and Process(). However, I keep getting segmentation fault here,
>> in Process():
>>
>> for (i = start_index, i <= end_index ; i++)
>> fin = fopen(filename[i], "rb"); <--- Seg fault
>
> Given the tiny amount of information available, my best guess can only
> be a guess:
>
> s/<=/<//

That looks a good guess. I've seen segfaults from fopen if a single
FILE * has been fclosed twice (no problem with the fclose).
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk

==============================================================================
TOPIC: Various ways to get month name from inbuilt C library
http://groups.google.com/group/comp.lang.c/t/7aa38ff82f37f58e?hl=en
==============================================================================

== 1 of 2 ==
Date: Sun, Feb 28 2010 12:51 am
From: santosh


Debanjan <debanjan4you@gmail.com> writes:

> What are the various ways to get the name of the month
> corresponding to an integer value from inbuilt C/C++ library, I am
> familiar to strftime. Any other means to do the same ?

ctime() and the deprecated asctime() return string representations of
their calender time and broken down time respectively. It's possible
to extract the month name (or it's abbreviation) from the string. But
strftime() is far better for this purpose.


== 2 of 2 ==
Date: Sun, Feb 28 2010 1:41 am
From: santosh


santosh <santosh.k83@gmail.com> writes:

> Debanjan <debanjan4you@gmail.com> writes:
>
>> What are the various ways to get the name of the month
>> corresponding to an integer value from inbuilt C/C++ library, I am
>> familiar to strftime. Any other means to do the same ?
>
> ctime() and the deprecated asctime() return string representations
> of their calender time and broken down time respectively. It's
> possible to extract the month name (or it's abbreviation) from the
> string. But strftime() is far better for this purpose.

Correction: asctime() isn't deprecated. It's just fragile to use.

==============================================================================
TOPIC: usage of size_t
http://groups.google.com/group/comp.lang.c/t/19e0ad96d01b9898?hl=en
==============================================================================

== 1 of 3 ==
Date: Sun, Feb 28 2010 1:49 am
From: fj


On 22 fév, 11:52, Malcolm McLean <malcolm.mcle...@btinternet.com>
wrote:
> On Feb 22, 12:46 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
> wrote:
>
> > I'm never happy with do-while. Are we certain the loop body should
> > always be executed at least once? I use do-while but I think rather
> > carefully first. This was a bug in the original Fortran- it always did
> > a loop at least once.
>
> I almost never use a do-while.
>
> Usually you need a empty case where the loop never executes. Other
> times it's easier to code the logic into a while loop.
> In Fortran 77 the fact that a loop body always executes at least once
> can be a real nuisance..

Except that this strange behavior has been precisely REMOVED by the
arrival of FORTRAN-77 !

You are referring to a very old FORTRAN version called either
FORTRAN-66 or FORTRAN-IV.


== 2 of 3 ==
Date: Sun, Feb 28 2010 5:02 am
From: Nick Keighley


On 26 Feb, 16:27, Julienne Walker <happyfro...@hotmail.com> wrote:
> On Feb 26, 3:00 am, spinoza1111 <spinoza1...@yahoo.com> wrote:
> > On Feb 26, 3:36 am, Julienne Walker <happyfro...@hotmail.com> wrote:
> > > On Feb 25, 1:46 pm,spinoza1111<spinoza1...@yahoo.com> wrote:
> > > > On Feb 25, 10:54 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
> > > > > spinoza1111wrote:

<snip>

> > > > I saw no try catch in your code.
>
> > > Probably because there's no such construct in portable C. You can
> > > simulate exceptions using standard C, but it gets ugly very quickly.
>
> > Pity.
>
> Not really. C isn't a language that lends itself to proper exception
> support because there are too many points where exceptions fall flat.

I'm surprised

> One can see the problems by looking at C++. C++ exceptions are useful
> to a point, but in my opinion they're not nearly as beneficial as they
> would be if C++ integrated them more fully into the language (throwing
> an exception on out of bounds array indexing, for example)

that might be nice, though it would be a rather different language

> and had native garbage collection.

I don't see why garbage collection is necessary for exceptions to work
properly. I always thought RAII was one of C++'s nicer features. With
Java etal you are never really sure when the resource disappears.


> By the time you plug the holes, you'll have something more like Java
> or C#.

well only because you insisted on garbage collection...

<snip>

== 3 of 3 ==
Date: Sun, Feb 28 2010 5:08 am
From: Nick Keighley


On 27 Feb, 22:11, Seebs <usenet-nos...@seebs.net> wrote:
> On 2010-02-27, Julienne Walker <happyfro...@hotmail.com> wrote:
> > On Feb 27, 11:52 am, spinoza1111 <spinoza1...@yahoo.com> wrote:

Julienne Walker wrote:
***
By the time you plug the holes, you'll have something more like Java
or C#.
***

> >> Which some brilliant people at Sun and Microsoft have done. Moore's
> >> Law should take care of the rest. Why is C not given a hero's funeral?
>
> > For the same reason COBOL hasn't: there's a great deal of existing
> > code out in the wild.
>
> Not just that.  There are problems where Moore's Law isn't enough, because
> ANY improvement in speed is pretty worthwhile, and where native C tends
> to win.
>
> Kernels, for instance.

and garbage collectors

==============================================================================
TOPIC: More quirks importing from bsd
http://groups.google.com/group/comp.lang.c/t/3879f22a8a1b80ee?hl=en
==============================================================================

== 1 of 4 ==
Date: Sun, Feb 28 2010 2:08 am
From: markhobley@hotpop.donottypethisbit.com (Mark Hobley)


In comp.unix.programmer Seebs <usenet-nospam@seebs.net> wrote:

> More likely, it was in <string.h> on the system the OP was porting from,
> but isn't in it on most other systems, since it's a BSD extension.
> (strlcat/strlcpy are what strncat/strncpy probably should have been).

Right that is interesting. I am reading the manual pages online, and I have
found something very odd:

The man page for string(3)

http://linux.die.net/man/3/string

(This does not mention strlcat)

From the same website, a man page for strlcat:

http://linux.die.net/man/3/strlcat

Strangely, there is a string.h and a strings.h

I tried included both:

#include <string.h>
#include <strings.h>

That didn't fix it. Maybe it is in unistd.h:

#include <unistd.h>

Nope. That didn't fix it either.

I think you are right. Maybe this is a BSD extension and I am reading the wrong
documents.

Mark.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

== 2 of 4 ==
Date: Sun, Feb 28 2010 2:39 am
From: santosh


Mark Hobley <markhobley@hotpop.donottypethisbit.com> writes:

> In comp.unix.programmer Seebs <usenet-nospam@seebs.net> wrote:
>
>> More likely, it was in <string.h> on the system the OP was porting
>> from, but isn't in it on most other systems, since it's a BSD
>> extension. (strlcat/strlcpy are what strncat/strncpy probably
>> should have been).
>
> Right that is interesting. I am reading the manual pages online,
> and I have found something very odd:
>
> The man page for string(3)
>
> http://linux.die.net/man/3/string
>
> (This does not mention strlcat)
>
> From the same website, a man page for strlcat:
>
> http://linux.die.net/man/3/strlcat
>
> Strangely, there is a string.h and a strings.h
>
> I tried included both:
>
> #include <string.h>
> #include <strings.h>
>
> That didn't fix it. Maybe it is in unistd.h:
>
> #include <unistd.h>
>
> Nope. That didn't fix it either.
>
> I think you are right. Maybe this is a BSD extension and I am
> reading the wrong documents.

It does seem to be a BSD specific function, along with strlcpy().

<http://en.wikipedia.org/wiki/Strlcpy>
<http://www.openbsd.org/cgi-
bin/cvsweb/~checkout~/src/lib/libc/string/strlcat.c?rev=1.13
>
<http://www.openbsd.org/cgi-bin/man.cgi?query=strlcpy&sektion=3>

Since the source is available, and it's easy to implement your own as
well, with the same semantics, it's not a big portability problem,
AFAICS.


== 3 of 4 ==
Date: Sun, Feb 28 2010 3:08 am
From: markhobley@hotpop.donottypethisbit.com (Mark Hobley)


In comp.unix.programmer Ian Collins <ian-news@hotmail.com> wrote:
> Then you haven't included the header where it is declared.

Yeah. That is weird, because there were no additional #include statements in
the original bsd code. This must be an implementation difference.

Mark.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

== 4 of 4 ==
Date: Sun, Feb 28 2010 5:08 am
From: markhobley@hotpop.donottypethisbit.com (Mark Hobley)


In comp.unix.programmer santosh <santosh.k83@gmail.com> wrote:
> Since the source is available, and it's easy to implement your own as
> well, with the same semantics, it's not a big portability problem,
> AFAICS.

Yeah. I am just deciding how to do that now. I have stumbled across a library
called libnostd that contains an implementation of this. I could use that
library. I am a bit concerned about the transparency issue of that though,
because it still uses string.h and this means that code using string.h
but using strlcat will work directly on some systems, but not on other
systems where strlcat is not implement (so much for standard C libraries huh!).

I am thinking it might be better to create a new header bsdlib.h containing
the additional functionality, and using that. However, this may then create
another problem in that I need to provide fixups so that code including
bsdlib.h and string.h on a host that includes strlcat in the string.h header
does not create a conflicting definition. Also, what do I do about alternative
implementations of strlcat, which zero the last byte of the buffer? Do I just
drop support for those platforms, or do I create a new function name with
equal functionality to avoid ambiguity? What a mess!

The other thing is, I have some fixups to make to glibc anyway to make it
portable across IA32 compatible machines (Currently it contains embedded
invalid instructions which do not work on traditional Pentiums, the Cyrix 686,
and the AMD K5 and AMD K6, and can cause a crash when the libraries are
transferred between machines containing these processors.) I could just fork
the library, apply the strlcat to the forked library and be done.

Mark.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/


==============================================================================
TOPIC: What is the explanation?
http://groups.google.com/group/comp.lang.c/t/43491a16c11fd4e0?hl=en
==============================================================================

== 1 of 2 ==
Date: Sun, Feb 28 2010 2:08 am
From: Willem


Scott wrote:
) On Sat, 27 Feb 2010 22:39:26 +0000 (UTC), in comp.lang.c, Willem
)<willem@snail.stack.nl> wrote:
)>What do you expect the value of (7||8) to be ?
)
) I would not expect it to be 1. I would just expect it to not be zero.

Perhaps you should switch to Perl. There, (a||b) is defined as (a?a:b)
(But, obviously, with a only being evaluated once).
And, incidentally, (a&&b) is defined as (a?b:a).

In C, the result of any boolean operator is always 0 or 1.
Dunno why, perhaps because existing compilers did it that way and
code existed that used the result of the boolean for arithmetics.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


== 2 of 2 ==
Date: Sun, Feb 28 2010 2:41 am
From: Nick <3-nospam@temporary-address.org.uk>


Willem <willem@snail.stack.nl> writes:

> Scott wrote:
> ) On Sat, 27 Feb 2010 22:39:26 +0000 (UTC), in comp.lang.c, Willem
> )<willem@snail.stack.nl> wrote:
> )>What do you expect the value of (7||8) to be ?
> )
> ) I would not expect it to be 1. I would just expect it to not be zero.
>
> Perhaps you should switch to Perl. There, (a||b) is defined as (a?a:b)
> (But, obviously, with a only being evaluated once).
> And, incidentally, (a&&b) is defined as (a?b:a).
>
> In C, the result of any boolean operator is always 0 or 1.
> Dunno why, perhaps because existing compilers did it that way and
> code existed that used the result of the boolean for arithmetics.

Spectrum Basic did that well before Perl IIRC. Useful for writing
things like:
PRINT a$ OR 'nothing'
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk


==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.c"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.c?hl=en

To unsubscribe from this group, send email to comp.lang.c+unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.c/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate