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
яяяяяяяяяяяяяяяяя
: 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
<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...
> 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.
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.
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
: 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.