delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/16/04:58:58

Date: Sun, 16 Aug 1998 11:58:45 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Cephaler <cephaler AT hotmail DOT com>
cc: djgpp AT delorie DOT com
Subject: Re: yet more questions about pointers
In-Reply-To: <01bdc7e8$c1ff0e20$4ac3b8cd@scully>
Message-ID: <Pine.SUN.3.91.980816115401.6981c-100000@is>
MIME-Version: 1.0

On 15 Aug 1998, Cephaler wrote:

> This yielded 'raboof' to my delight... Now, have I found a good use for
> pointers? Or is this bad bad code?

It's one of the uses.  I don't see anything particularly bad here, 
except that you never check the values returned by `malloc' (they might 
be NULL pointers if `malloc' fails to allocate), and you rely on "foobar" 
to be shorter than the size of the buffer allocated by `malloc'.

> 2) Having not initialized string2

But you *did* initialize string2, by pointing it to the same address 
where string1 points.

> a) do I have to free string2 and b) does
> that have any effect on string1? (oops didn't free string1)

You need to free any of them (but not both!).

> 3) concerning strcpy...is there any special reason why I shouldn't just use
> string1 = "foobar" ?

THis is something else.  It doesn't copy "foobar" at all, it changes the 
value stored in string1.  After this, string1 points to the address where 
the constant string "foobar" is stored.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019