Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Alloc structs..How much?

1 view
Skip to first unread message

Frederick A Ferguson

unread,
Sep 13, 1994, 3:55:37 PM9/13/94
to
Here's the declaration:

typedef struct HostStruct {
char IPAddress[31];
int Port;
char Username[31];
char Password[31];
char Description[81];
char LastDirectory[81];
LONG Type;
LONG Anonymous;
struct HostStruct *Next;
struct HostStruct *Prev;
} HostRecord;
HostRecord *HostList;

To allocate memory for HostList, I am using malloc()... But how much
memory do I need? Doing

HostList = (HostRecord *)malloc(sizeof(HostRecord *));

doesn't seem to work. The system finally crashes after running it a few
times. doing a malloc(2*sizeof(HostRecord *)+2048) does work, but it of
course wastes memory... Any suggestions?

David Ferguson
faf...@usit.net


* Offline Orbit 0.70b * Life is just one BIG beta test cycle
яяяяяяяяяяяяяяяяя

Ken de Veaux

unread,
Sep 14, 1994, 8:57:41 AM9/14/94
to
Frederick A Ferguson (faf...@use.usit.net) wrote:

: To allocate memory for HostList, I am using malloc()... But how much


: memory do I need? Doing

: HostList = (HostRecord *)malloc(sizeof(HostRecord *));

: doesn't seem to work. The system finally crashes after running it a few
: times. doing a malloc(2*sizeof(HostRecord *)+2048) does work, but it of
: course wastes memory... Any suggestions?

What you're doing is allocating enough memory to hold a _pointer_ to
your structure. To allocate memory for the structure itself, try

HostList = (HostRecord *)malloc(sizeof(HostRecord));

Rgds

Kevin Freeman

unread,
Sep 13, 1994, 11:46:00 PM9/13/94
to
In article <35503p$r...@use.usit.net>,
Frederick A Ferguson <faf...@use.usit.net> wrote:
>Here's the declaration:
>

<large struct deleted>

>
>To allocate memory for HostList, I am using malloc()... But how much
>memory do I need? Doing
>
>HostList = (HostRecord *)malloc(sizeof(HostRecord *));

This should get you started:

Your struct is several dozen bytes long. Now ponder this:

"What is the 'sizeof' a pointer?"

If you need further prodding, just ask...

Frank Copeland

unread,
Sep 14, 1994, 3:12:56 AM9/14/94
to
In article <35503p$r...@use.usit.net>, Frederick A Ferguson writes:

> Here's the declaration:
>
> typedef struct HostStruct {

[...]


> } HostRecord;
> HostRecord *HostList;
>
> To allocate memory for HostList, I am using malloc()... But how much
> memory do I need? Doing
>
> HostList = (HostRecord *)malloc(sizeof(HostRecord *));

^^^^^^^^^^^^


> doesn't seem to work. The system finally crashes after running it a few
> times. doing a malloc(2*sizeof(HostRecord *)+2048) does work, but it of
> course wastes memory... Any suggestions?

Umm, C isn't my language, but aren't you taking the size of a _pointer_
here? Why "sizeof (HostRecord *)" instead of "sizeof (HostRecord)"? By my
calculation, your structure is approx. 400 bytes, as compared to 4 bytes
for a pointer. Try using a debugging malloc(), or run MungWall, to find
out how much memory is actually being allocated.

Of course, you would have to be deliberately perverse to get this kind of
problem with Oberon ;-).

Frank Copeland
--
MODULE Sig;
IMPORT StdDisclaimer, CleverQuote, AdvocateOberon;
END Sig.

Doug Walker

unread,
Sep 14, 1994, 8:47:47 AM9/14/94
to

In article <35503p$r...@use.usit.net>, faf...@use.usit.net (Frederick A Ferguson) writes:
|> Here's the declaration:
|>
|> typedef struct HostStruct {
|> char IPAddress[31];
|> int Port;
|> char Username[31];
|> char Password[31];
|> char Description[81];
|> char LastDirectory[81];
|> LONG Type;
|> LONG Anonymous;
|> struct HostStruct *Next;
|> struct HostStruct *Prev;
|> } HostRecord;
|> HostRecord *HostList;
|>
|> To allocate memory for HostList, I am using malloc()... But how much
|> memory do I need? Doing
|>
|> HostList = (HostRecord *)malloc(sizeof(HostRecord *));
|>
|> doesn't seem to work. The system finally crashes after running it a few

How about

HostList = (HostRecord *)malloc(sizeof(HostRecord));

By allocating sizeof(HostRecord *), you're only asking for four
bytes (since that's how much space a HostRecord * needs). Since
you're actually trying to store a HostRecord, you need a little
more than four bytes. sizeof(HostRecord) is the number of bytes
needed to store the record.

--
***** / wal...@unx.sas.com
*|_o_o|\\ Doug Walker< BIX, Portal: djwalker
*|. o.| || \ CompuServe: 71165,2274
| o |//
======
Any opinions expressed are mine, not those of SAS Institute, Inc.

Karl Lukas

unread,
Sep 15, 1994, 10:35:37 AM9/15/94
to
> HostList = (HostRecord *)malloc(sizeof(HostRecord *));

This is incorrect code. If you printf("%ld", sizeof(HostRecord *))
you will get 4 as output. This is the size of a pointer to
HostRecord. You want memory for the whole structure, so you have
to write
HostList = (HostRecord *)malloc(sizeof(HostRecord));
^^^

Karl

Manuel Lemos

unread,
Sep 16, 1994, 10:32:49 AM9/16/94
to
Frederick A Ferguson (faf...@use.usit.net) wrote:
: Here's the declaration:

: typedef struct HostStruct {
: char IPAddress[31];
: int Port;
: char Username[31];
: char Password[31];
: char Description[81];
: char LastDirectory[81];
: LONG Type;
: LONG Anonymous;
: struct HostStruct *Next;
: struct HostStruct *Prev;
: } HostRecord;
: HostRecord *HostList;

: To allocate memory for HostList, I am using malloc()... But how much
: memory do I need? Doing

: HostList = (HostRecord *)malloc(sizeof(HostRecord *));

: doesn't seem to work. The system finally crashes after running it a few
: times. doing a malloc(2*sizeof(HostRecord *)+2048) does work, but it of
: course wastes memory... Any suggestions?

You seem to be a bit confused about pointers and data pointed by those pointers.
If you want allocate memory for data to be pointed to by a pointer variable,
you can obtain the block size using sizeof() expresion specifying the type
of data to be pointed by that pointer. Use something like this.

sizeof(HostStruct)

Using sizeof(HostStruct *) you were allocating space for a pointer to a
HostStruct pointer.

You can also use

sizeof(*HostList)

to specify the size memory space size of the data pointed by the Hostlist
pointer regardless to what kind of data it points to.


0 new messages