* Question about Malloc
@ 2004-10-10 5:31 Edward Parrilla
2004-10-10 6:13 ` Jeff Woods
2004-10-10 11:13 ` Jan-Benedict Glaw
0 siblings, 2 replies; 6+ messages in thread
From: Edward Parrilla @ 2004-10-10 5:31 UTC (permalink / raw)
To: linux prg
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.
What should be the right sintax?
Any help would be appreciated.
Thanks in advance
Ed
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: Question about Malloc
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
1 sibling, 0 replies; 6+ messages in thread
From: Jeff Woods @ 2004-10-10 6:13 UTC (permalink / raw)
To: Edward Parrilla; +Cc: linux prg
At 10/10/2004 12:31 AM -0500, Edward Parrilla wrote:
>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.
>
>What should be the right sintax?
The word in "syntax". (A "sin tax" would be too expensive.)
"man malloc" says (in part):
>> void *calloc(size_t nmemb, size_t size);
>> void *malloc(size_t size);
>>
>> calloc() allocates memory for an array of nmemb elements of
>> size bytes each and returns a pointer to the allocated memory. The
>> memory is set to zero.
>>
>> malloc() allocates 'size' bytes and returns a pointer to the
>> allocated memory. The memory is not cleared.
If you want to allocate exactly one TABLE struct, it should be something
more like:
r=malloc(sizeof(TABLE));
assert(NULL != r);
If you want to allocate an array of TABLE structs (which the name implies),
use something like:
#define TABLE_MAX 100
r = calloc(TABLE_MAX, sizeof(TABLE));
assert(NULL != r);
>Any help would be appreciated.
RTFM: Read the manpage. Or, "Use the manpage, Luke!" It wouldn't hurt to
read K&R 2nd edition either.
--
Jeff Woods <kazrak+kernel@cesmail.net>
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: Question about Malloc
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
2004-10-19 12:41 ` Matías Aguirre
1 sibling, 1 reply; 6+ messages in thread
From: Jan-Benedict Glaw @ 2004-10-10 11:13 UTC (permalink / raw)
To: linux prg
[-- 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 --]
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: Question about Malloc
2004-10-10 11:13 ` Jan-Benedict Glaw
@ 2004-10-19 12:41 ` Matías Aguirre
2004-10-19 13:27 ` Jan-Benedict Glaw
0 siblings, 1 reply; 6+ messages in thread
From: Matías Aguirre @ 2004-10-19 12:41 UTC (permalink / raw)
To: linux prg
It's not safer to make:
Recpointer r;
r=(Recpointer)malloc(Recpointer);
On Sun, 10 Oct 2004 13:13:16 +0200, Jan-Benedict Glaw <jbglaw@lug-owl.de> wrote:
> 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.
>
--
Matías Aguirre
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: Question about Malloc
2004-10-19 12:41 ` Matías Aguirre
@ 2004-10-19 13:27 ` Jan-Benedict Glaw
2004-10-19 13:52 ` Matías Aguirre
0 siblings, 1 reply; 6+ messages in thread
From: Jan-Benedict Glaw @ 2004-10-19 13:27 UTC (permalink / raw)
To: linux prg
[-- Attachment #1: Type: text/plain, Size: 1072 bytes --]
On Tue, 2004-10-19 09:41:49 -0300, Matías Aguirre <matiasaguirre@gmail.com>
wrote in message <a3c4234104101905413e127d3e@mail.gmail.com>:
> It's not safer to make:
>
> Recpointer r;
> r=(Recpointer)malloc(Recpointer);
malloc is void, while r isn't. So you don't need the cast here. However,
notice that your malloc call is actually wrong: the argument isn't a
number (or sizeof(something)), but only a type.
General rule of thumb:
The "output" of malloc should be assigned to a variable whose
type is a pointer to whatever you give to sizeof(xxx) as the
malloc argument.
or in short:
One star more on the left than on the right.
Of course, if you play with types, this doesn't apply any longer...
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 --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Question about Malloc
2004-10-19 13:27 ` Jan-Benedict Glaw
@ 2004-10-19 13:52 ` Matías Aguirre
0 siblings, 0 replies; 6+ messages in thread
From: Matías Aguirre @ 2004-10-19 13:52 UTC (permalink / raw)
To: linux prg
i agree with that, i didn't fix inside de malloc, i only suggested
about the cast
but my big error was speak about c++ and not about c, in c++ the
compiler (g++ in this case) warn you about that conversion....
sorry
On Tue, 19 Oct 2004 15:27:38 +0200, Jan-Benedict Glaw <jbglaw@lug-owl.de> wrote:
> On Tue, 2004-10-19 09:41:49 -0300, Matías Aguirre <matiasaguirre@gmail.com>
> wrote in message <a3c4234104101905413e127d3e@mail.gmail.com>:
> > It's not safer to make:
> >
> > Recpointer r;
> > r=(Recpointer)malloc(Recpointer);
>
> malloc is void, while r isn't. So you don't need the cast here. However,
> notice that your malloc call is actually wrong: the argument isn't a
> number (or sizeof(something)), but only a type.
>
> General rule of thumb:
>
> The "output" of malloc should be assigned to a variable whose
> type is a pointer to whatever you give to sizeof(xxx) as the
> malloc argument.
>
> or in short:
>
> One star more on the left than on the right.
>
> Of course, if you play with types, this doesn't apply any longer...
>
>
>
> 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));
>
>
>
--
Matías Aguirre
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2004-10-19 13:52 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2004-10-19 12:41 ` Matías Aguirre
2004-10-19 13:27 ` Jan-Benedict Glaw
2004-10-19 13:52 ` Matías Aguirre
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).