Re: Am I programming correctly with array parameter?

From: Alwyn (dt015a1979_at_mac.com.invalid)
Date: 08/29/04


Date: Sun, 29 Aug 2004 15:07:18 +0100

In article <kZiG4$LUXdMBFwqT@robinton.demon.co.uk>, Francis Glassborow
<francis@robinton.demon.co.uk> wrote:

> In article <290820041223102174%dt015a1979@mac.com.invalid>, Alwyn
> <dt015a1979@mac.com.invalid> writes
>
> >But there are cases where allocating memory within a function and
> >depending on the user to free it is unavoidable. Such cases must be
> >clearly and prominently documented, of course.
>
> I have no argument with that, but newcomers to the language probably
> lack the experience to identify those cases,

Here's one such case, which any beginner can understand, in my humble
opinion:

Standard C does not provide the POSIX 'strdup' function, perhaps for
the reason that the user needs to do the work of 'cleaning up' after
it. However, the fact remains that duplicating a string is something
one often has to do. So we can write our own:

char *dupstr(const char *source)
{
   char *dest = malloc(strlen(source)+1);
   if (dest != NULL)
      strcpy(dest, source);
   return dest;
}

> and simple emulating idioms
> they have used in other languages will hinder their progress.

Yes, of course, though it's not clear to me that this is what the OP
was doing in this instance. I believe he was just practising his newly
acquired ability to manipulate pointers.

Alwyn



Relevant Pages

  • Re: An Observation
    ... arcane things that matter only for special purposes. ... You mean like the fact that "CHAR X" means different things depending on ... In a reasonable language, it should be clear which "noun" an "adjective" like CHAR modifies, and the syntactic relationship shouldn't change depending on some implicit state. ...
    (comp.lang.forth)
  • Re: Are _T() and TEXT() macros equivalent?
    ... are not concerned if a char takes 1 byte or 100. ... since the language defines an object ... various ramifications of using and not using the switch. ... Why the fixation on basing the meaning of the keyword "char" on a compiler ...
    (microsoft.public.vc.mfc)
  • Re: Are _T() and TEXT() macros equivalent?
    ... by every single application developed in that language for the last 30 ... are not concerned if a char takes 1 byte or 100. ... Go through the standard and list the sections that use char as byte. ... void write ...
    (microsoft.public.vc.mfc)
  • Re: An Observation
    ... there is a problem) is that CHAR doesn't always execute. ... In a reasonable language, it should be clear which "noun" an "adjective" like CHAR modifies, and the syntactic relationship shouldn't change depending on some implicit state. ... So when newbies encounter Forth, they get stuck on elaborate stack gymnastics. ...
    (comp.lang.forth)
  • Re: learning c and c++
    ... >> books on each desired language, more would be better, but of course ... > possible) int to char, ... 'int') If you know that a particular 'int' value is within ... to a 'string'. ...
    (alt.comp.lang.learn.c-cpp)