linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jan-Benedict Glaw <jbglaw@lug-owl.de>
To: linux prg <linux-c-programming@vger.kernel.org>
Subject: Re: Question about Malloc
Date: Sun, 10 Oct 2004 13:13:16 +0200	[thread overview]
Message-ID: <20041010111316.GD5033@lug-owl.de> (raw)
In-Reply-To: <1097386256.6100.16.camel@localhost.localdomain>

[-- Attachment #1: Type: text/plain, Size: 1702 bytes --]

On Sun, 2004-10-10 00:31:05 -0500, Edward Parrilla <eparrilla@comcast.net>
wrote in message <1097386256.6100.16.camel@localhost.localdomain>:
> Hi all,
> I got the following structure:
> typedef struct  {
>  	char *file;
> 	char *ip_addres;
> 	}TABLE;
> 
> then define a pointer to it:
> 
> typedef TABLE *Recpointer;
> Recpointer r;
> 
> I'm trying to use malloc as:
> 
> r=malloc(Recpointer);
> but it gives me error.

...which is correct. malloc() allocates memory, byte-wise, not complex
types. So you tell it how many bytes you want and it tries to do exactly
that. It's not C++! Additionally, your newly created Recpointer type is
a pointer, so if you'd do exactly the above correct, you'd not need to
malloc sizeof(Recpointer), but sizeof(TABLE).

But do you see the general mistake here? You typedef'ed your structures
twice, which completely hides you all the details. _This_ is what leads
you to wrong programming in just this case.

If you'd done it like this

struct table_entry {
	char *file;
	char *ip_address;
};
...
struct table_entry *entry;
entry = malloc (sizeof (struct table_entry));

you'd easily see the flow of types here. It doesn't hide the defails
from your eyes, thus gives you the chance to see what's happening. After
a number of typedefs, you usually don't know any more what is what...

MfG, JBG

-- 
Jan-Benedict Glaw       jbglaw@lug-owl.de    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

  parent reply	other threads:[~2004-10-10 11:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-10  5:31 Question about Malloc Edward Parrilla
2004-10-10  6:13 ` Jeff Woods
2004-10-10 11:13 ` Jan-Benedict Glaw [this message]
2004-10-19 12:41   ` Matías Aguirre
2004-10-19 13:27     ` Jan-Benedict Glaw
2004-10-19 13:52       ` Matías Aguirre

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20041010111316.GD5033@lug-owl.de \
    --to=jbglaw@lug-owl.de \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).