C, malloc(sizeof(structure_with_pointers))

hello everyone.
i am having a problem with malloc.
i need to allocate memory for a structure holding pointers to other structures.
when malloc is called, i get an IPF (windows 95 osr2), one of those normal ones that happen
when you try to use a pointer incorrectly.
i am using borland's 32 bit c compiler, command line.
anyone have any ideas?

[code]
typedef struct nbname_response_header {
unsigned short transaction_id;
unsigned short flags;
unsigned short question_count;
unsigned short answer_count;
unsigned short name_service_count;
unsigned short additional_record_count;
char question_name[34];
unsigned short question_type;
unsigned short question_class;
unsigned long ttl;
unsigned short rdata_length;
unsigned char number_of_names;
} nbname_response_header_t;

struct nbname {
char ascii_name [16] ;
unsigned short rr_flags;
};

typedef struct nbname_response_footer {
unsigned char adapter_address [6];
unsigned char version_major;
unsigned char version_minor;
unsigned short duration;
unsigned short frmps_received;
unsigned short frmps_transmitted;
unsigned short iframe_receive_errors;
unsigned short transmit_aborts;
unsigned long transmitted;
unsigned long received;
unsigned short iframe_transmit_errors;
unsigned short no_receive_buffer;
unsigned short tl_timeouts;
unsigned short ti_timeouts;
unsigned short free_ncbs;
unsigned short ncbs;
unsigned short max_ncbs;
unsigned short no_transmit_buffers;
unsigned short max_datagram;
unsigned short pending_sessions;
unsigned short max_sessions;
unsigned short packet_sessions;
} nbname_response_footer_t ;

// sizeof nb_host_info is 16, i guess 4*sizeof(int)?

struct nb_host_info {
struct nbname_response_header* header;
struct nbname* names;
struct nbname_response_footer* footer;
int is_broken;
};

int main(){
struct nb_host_info *hostinfo = NULL;
if((hostinfo = malloc(sizeof(struct nb_host_info)))==NULL) return 1; // she blows up here
return 0;
}
[/code]

Comments

  • : hello everyone.
    : i am having a problem with malloc.
    : i need to allocate memory for a structure holding pointers to other structures.
    : when malloc is called, i get an IPF (windows 95 osr2), one of those normal ones that happen
    : when you try to use a pointer incorrectly.
    : i am using borland's 32 bit c compiler, command line.
    : anyone have any ideas?
    :
    : [code]
    : typedef struct nbname_response_header {
    : unsigned short transaction_id;
    : unsigned short flags;
    : unsigned short question_count;
    : unsigned short answer_count;
    : unsigned short name_service_count;
    : unsigned short additional_record_count;
    : char question_name[34];
    : unsigned short question_type;
    : unsigned short question_class;
    : unsigned long ttl;
    : unsigned short rdata_length;
    : unsigned char number_of_names;
    : } nbname_response_header_t;
    :
    : struct nbname {
    : char ascii_name [16] ;
    : unsigned short rr_flags;
    : };
    :
    : typedef struct nbname_response_footer {
    : unsigned char adapter_address [6];
    : unsigned char version_major;
    : unsigned char version_minor;
    : unsigned short duration;
    : unsigned short frmps_received;
    : unsigned short frmps_transmitted;
    : unsigned short iframe_receive_errors;
    : unsigned short transmit_aborts;
    : unsigned long transmitted;
    : unsigned long received;
    : unsigned short iframe_transmit_errors;
    : unsigned short no_receive_buffer;
    : unsigned short tl_timeouts;
    : unsigned short ti_timeouts;
    : unsigned short free_ncbs;
    : unsigned short ncbs;
    : unsigned short max_ncbs;
    : unsigned short no_transmit_buffers;
    : unsigned short max_datagram;
    : unsigned short pending_sessions;
    : unsigned short max_sessions;
    : unsigned short packet_sessions;
    : } nbname_response_footer_t ;
    :
    : // sizeof nb_host_info is 16, i guess 4*sizeof(int)?
    :
    : struct nb_host_info {
    : struct nbname_response_header* header;
    : struct nbname* names;
    : struct nbname_response_footer* footer;
    : int is_broken;
    : };
    :
    : int main(){
    : struct nb_host_info *hostinfo = NULL;
    : if((hostinfo = malloc(sizeof(struct nb_host_info)))==NULL) return 1; // she blows up here
    : return 0;
    : }
    : [/code]
    :
    [blue]The code looks clean, aside from wasteful "*hostinfo=NULL;". No idea... [/blue]
  • : : hello everyone.
    : : i am having a problem with malloc.
    : : i need to allocate memory for a structure holding pointers to other structures.
    : : when malloc is called, i get an IPF (windows 95 osr2), one of those normal ones that happen
    : : when you try to use a pointer incorrectly.
    : : i am using borland's 32 bit c compiler, command line.
    : : anyone have any ideas?
    : :



    : :
    : [blue]The code looks clean, aside from wasteful "*hostinfo=NULL;". No idea... [/blue]

    [code=000000][color=ffffff]
    C:Csrc>bcc32 foo.c
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    foo.c:
    Warning W8065 foo.c 59: Call to function 'malloc' with no prototype in function
    main
    Warning W8069 foo.c 59: Nonportable pointer conversion in function main
    Warning W8004 foo.c 61: 'hostinfo' is assigned a value that is never used in function main
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

    C:Csrc>foo

    C:Csrc>
    [/color][/code]

    There are a couple of warnings, but I did not get the errors that you describe. First, try including for malloc's prototype (unless you just snipped it from your post). Else, I'm afraid you will need to contact Borland, because I am with AsmGuru on this one ... no idea.


    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • [b][red]This message was edited by gustavosserra at 2002-10-28 10:55:30[/red][/b][hr]
    : if((hostinfo = malloc(sizeof(struct nb_host_info)))==NULL) return 1; // she blows up here

    Im not sure, but mustnt you use the casting?(bad english..sorry)

    This way:

    [blue] if((hostinfo =(struct nb_host_info*) malloc
    (sizeof(struct nb_host_info)))==NULL) return 1;
    [/blue]

    Or you can use the new operator:

    [blue]if((hostinfo =new nb_host_info)==NULL)
    [/blue]



  • : [b][red]This message was edited by gustavosserra at 2002-10-28 10:55:30[/red][/b][hr]
    : : if((hostinfo = malloc(sizeof(struct nb_host_info)))==NULL) return 1; // she blows up here
    :
    : Im not sure, but mustnt you use the casting?(bad english..sorry)
    :
    : This way:
    :
    : [blue] if((hostinfo =(struct nb_host_info*) malloc
    : (sizeof(struct nb_host_info)))==NULL) return 1;
    : [/blue]

    No, this kind of abhorrent technique is only necessary in C++. C has well defined conversions from void * to (any object type) *. The best approach, is to leave type information completely out of the expression:
    [code=ffffff]
    if ((hostinfo = malloc(sizeof *hostinfo)) == NULL ) {
    /* ... */
    }
    [/code]

    That way, it is impossible to get wrong.


    : Or you can use the new operator:
    :
    : [blue]if((hostinfo =new nb_host_info)==NULL)
    : [/blue]

    Not in C.


    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • : hello everyone.
    : i am having a problem with malloc.
    : i need to allocate memory for a structure holding pointers to other structures.
    : when malloc is called, i get an IPF (windows 95 osr2), one of those normal ones that happen
    : when you try to use a pointer incorrectly.
    : i am using borland's 32 bit c compiler, command line.
    : anyone have any ideas?
    :
    : [code]
    : typedef struct nbname_response_header {
    : unsigned short transaction_id;
    : unsigned short flags;
    : unsigned short question_count;
    : unsigned short answer_count;
    : unsigned short name_service_count;
    : unsigned short additional_record_count;
    : char question_name[34];
    : unsigned short question_type;
    : unsigned short question_class;
    : unsigned long ttl;
    : unsigned short rdata_length;
    : unsigned char number_of_names;
    : } nbname_response_header_t;
    :
    : struct nbname {
    : char ascii_name [16] ;
    : unsigned short rr_flags;
    : };
    :
    : typedef struct nbname_response_footer {
    : unsigned char adapter_address [6];
    : unsigned char version_major;
    : unsigned char version_minor;
    : unsigned short duration;
    : unsigned short frmps_received;
    : unsigned short frmps_transmitted;
    : unsigned short iframe_receive_errors;
    : unsigned short transmit_aborts;
    : unsigned long transmitted;
    : unsigned long received;
    : unsigned short iframe_transmit_errors;
    : unsigned short no_receive_buffer;
    : unsigned short tl_timeouts;
    : unsigned short ti_timeouts;
    : unsigned short free_ncbs;
    : unsigned short ncbs;
    : unsigned short max_ncbs;
    : unsigned short no_transmit_buffers;
    : unsigned short max_datagram;
    : unsigned short pending_sessions;
    : unsigned short max_sessions;
    : unsigned short packet_sessions;
    : } nbname_response_footer_t ;
    :
    : // sizeof nb_host_info is 16, i guess 4*sizeof(int)?
    :
    : struct nb_host_info {
    : struct nbname_response_header* header;
    : struct nbname* names;
    : struct nbname_response_footer* footer;
    : int is_broken;
    : };
    :
    : int main(){
    : struct nb_host_info *hostinfo = NULL;
    : if((hostinfo = malloc(sizeof(struct nb_host_info)))==NULL) return 1; // she blows up here
    : return 0;
    : }
    : [/code]
    :
    if ((hostinfo = malloc(sizeof(struct nb_host_info))) == NULL ) is the right way to do it. One problem I think is the use of pointers in struct nb_host_info. You are not allocating enough memory in the first place, thats why sizeof(struct nb_host_info) is only 16. Try
    [code]
    struct nb_host_info {
    struct nbname_response_header header;
    struct nbname names;
    struct nbname_response_footer footer;
    int is_broken;
    };
    [/code]
  • Thank you for your help.
    I tried using malloc(sizeof *hostinfo) but it still craps out.
    could it be that i simply do not have enough memory? wouldnt malloc return null in that case?
    it is probably some dumb error of mine, i am an amateur at best (in the middle of self-teaching). i would post the whole program but it was written by someone else for linux and i am porting it to winblows (i dont own it..), it actually works very well except for this problem( is a utility to enumerate nbt names and shares on a subnet), i have rewritten the part in question (breaks smb packet apart and gets the good data) but my way is nowhere near as succinct plus i just want to know why this is happening.
    could it be related to the fact that the nb_host_info structure has pointers to structures with pointers, is there some sort of recursion going on that my compiler and os (and really REALLY crappy machine) cannot handle?
    could it be related to the fact that one element of the nb_host_INFO structure is declared with 'struct' while the others are 'typedef struct'? im gonna try this on a win2k box tonight and see what happens.
    thanks for all the help./
    : hello everyone.
    : i am having a problem with malloc.
    : i need to allocate memory for a structure holding pointers to other structures.
    : when malloc is called, i get an IPF (windows 95 osr2), one of those normal ones that happen
    : when you try to use a pointer incorrectly.
    : i am using borland's 32 bit c compiler, command line.
    : anyone have any ideas?
    :
    : [code]
    : typedef struct nbname_response_header {
    : unsigned short transaction_id;
    : unsigned short flags;
    : unsigned short question_count;
    : unsigned short answer_count;
    : unsigned short name_service_count;
    : unsigned short additional_record_count;
    : char question_name[34];
    : unsigned short question_type;
    : unsigned short question_class;
    : unsigned long ttl;
    : unsigned short rdata_length;
    : unsigned char number_of_names;
    : } nbname_response_header_t;
    :
    : struct nbname {
    : char ascii_name [16] ;
    : unsigned short rr_flags;
    : };
    :
    : typedef struct nbname_response_footer {
    : unsigned char adapter_address [6];
    : unsigned char version_major;
    : unsigned char version_minor;
    : unsigned short duration;
    : unsigned short frmps_received;
    : unsigned short frmps_transmitted;
    : unsigned short iframe_receive_errors;
    : unsigned short transmit_aborts;
    : unsigned long transmitted;
    : unsigned long received;
    : unsigned short iframe_transmit_errors;
    : unsigned short no_receive_buffer;
    : unsigned short tl_timeouts;
    : unsigned short ti_timeouts;
    : unsigned short free_ncbs;
    : unsigned short ncbs;
    : unsigned short max_ncbs;
    : unsigned short no_transmit_buffers;
    : unsigned short max_datagram;
    : unsigned short pending_sessions;
    : unsigned short max_sessions;
    : unsigned short packet_sessions;
    : } nbname_response_footer_t ;
    :
    : // sizeof nb_host_info is 16, i guess 4*sizeof(int)?
    :
    : struct nb_host_info {
    : struct nbname_response_header* header;
    : struct nbname* names;
    : struct nbname_response_footer* footer;
    : int is_broken;
    : };
    :
    : int main(){
    : struct nb_host_info *hostinfo = NULL;
    : if((hostinfo = malloc(sizeof(struct nb_host_info)))==NULL) return 1; // she blows up here
    : return 0;
    : }
    : [/code]
    :

  • thx DB1, I will try that.
    there are like 4,000 "->"s that i have to change to "."s to get it to compile, ill get back to the board later when thats done.

    : : hello everyone.
    : : i am having a problem with malloc.
    : : i need to allocate memory for a structure holding pointers to other structures.
    : : when malloc is called, i get an IPF (windows 95 osr2), one of those normal ones that happen
    : : when you try to use a pointer incorrectly.
    : : i am using borland's 32 bit c compiler, command line.
    : : anyone have any ideas?
    : :
    : : [code]
    : : typedef struct nbname_response_header {
    : : unsigned short transaction_id;
    : : unsigned short flags;
    : : unsigned short question_count;
    : : unsigned short answer_count;
    : : unsigned short name_service_count;
    : : unsigned short additional_record_count;
    : : char question_name[34];
    : : unsigned short question_type;
    : : unsigned short question_class;
    : : unsigned long ttl;
    : : unsigned short rdata_length;
    : : unsigned char number_of_names;
    : : } nbname_response_header_t;
    : :
    : : struct nbname {
    : : char ascii_name [16] ;
    : : unsigned short rr_flags;
    : : };
    : :
    : : typedef struct nbname_response_footer {
    : : unsigned char adapter_address [6];
    : : unsigned char version_major;
    : : unsigned char version_minor;
    : : unsigned short duration;
    : : unsigned short frmps_received;
    : : unsigned short frmps_transmitted;
    : : unsigned short iframe_receive_errors;
    : : unsigned short transmit_aborts;
    : : unsigned long transmitted;
    : : unsigned long received;
    : : unsigned short iframe_transmit_errors;
    : : unsigned short no_receive_buffer;
    : : unsigned short tl_timeouts;
    : : unsigned short ti_timeouts;
    : : unsigned short free_ncbs;
    : : unsigned short ncbs;
    : : unsigned short max_ncbs;
    : : unsigned short no_transmit_buffers;
    : : unsigned short max_datagram;
    : : unsigned short pending_sessions;
    : : unsigned short max_sessions;
    : : unsigned short packet_sessions;
    : : } nbname_response_footer_t ;
    : :
    : : // sizeof nb_host_info is 16, i guess 4*sizeof(int)?
    : :
    : : struct nb_host_info {
    : : struct nbname_response_header* header;
    : : struct nbname* names;
    : : struct nbname_response_footer* footer;
    : : int is_broken;
    : : };
    : :
    : : int main(){
    : : struct nb_host_info *hostinfo = NULL;
    : : if((hostinfo = malloc(sizeof(struct nb_host_info)))==NULL) return 1; // she blows up here
    : : return 0;
    : : }
    : : [/code]
    : :
    : if ((hostinfo = malloc(sizeof(struct nb_host_info))) == NULL ) is the right way to do it. One problem I think is the use of pointers in struct nb_host_info. You are not allocating enough memory in the first place, thats why sizeof(struct nb_host_info) is only 16. Try
    : [code]
    : struct nb_host_info {
    : struct nbname_response_header header;
    : struct nbname names;
    : struct nbname_response_footer footer;
    : int is_broken;
    : };
    : [/code]
    :

  • Have you tried compiling on the portion you posted? It doesnt crash.

    I think your heap is getting corrupted [italic]before[/italic] you get to this code, your malloc that crashes is trying to access the heap, it bangs. Look at the code that gets called before the lines you posted.


  • Any suggestions as to how to identify a previous problem?
    is there a debugging tool i should learn to use?
    forgive my lack of knowledge, i am kindof fly-by-night here, learning as I go (and i DO read ALOT so plz no RTFMs ;).
    THANK YOU
    : Have you tried compiling on the portion you posted? It doesnt crash.
    :
    : I think your heap is getting corrupted [italic]before[/italic] you get to this code, your malloc that crashes is trying to access the heap, it bangs. Look at the code that gets called before the lines you posted.
    :
    :
    :

  • You are compiling for Windows right? There's no RTFM in Windows :)

    Suggestions... Well, you need to debug it, check your malloc and frees, usually if a malloc fails, then the suspect is the last free called, so trace your code. If you can, starting from top to bottom, comment out suspect blocks of code, see if the problem goes away, narrow down.. Classic debugging.

    I either do that or use my copy of Bounds Checker. Check it out online, very useful tool but last I checked they dont even have an evaluation to download.

    I also heard Borland C++ Builder comes with its own memory/resource checker. You might want to download the trial version of BCB and check it out.

    : Any suggestions as to how to identify a previous problem?
    : is there a debugging tool i should learn to use?
    : forgive my lack of knowledge, i am kindof fly-by-night here, learning as I go (and i DO read ALOT so plz no RTFMs ;).
    : THANK YOU
    : : Have you tried compiling on the portion you posted? It doesnt crash.
    : :
    : : I think your heap is getting corrupted [italic]before[/italic] you get to this code, your malloc that crashes is trying to access the heap, it bangs. Look at the code that gets called before the lines you posted.
    : :
    : :
    : :
    :
    :

  • : Thank you for your help.
    : I tried using malloc(sizeof *hostinfo) but it still craps out.

    Well, that is only to prevent other kinds of awful errors. The error you seem to be experiencing is not the same. Just keep that technique in mind in the future.


    : could it be that i simply do not have enough memory? wouldnt malloc return null in that case?

    Yes, although technically speaking, 'return 1;' isn't truly portable as an exit code. EXIT_FAILURE, EXIT_SUCCESS (both from ), and the integer constant 0(zero) are the only 3 portable return values from main.


    : it is probably some dumb error of mine, i am an amateur at best (in the middle of self-teaching). i would post the whole program but it was written by someone else for linux and i am porting it to windows (i dont own it..),

    Wait, stop right there, is this code snippet generating the same error you are describing? If it is, then there is something seriously wrong with your installation (sans implementation-dependent portion). If not, then don't send us on a wild goose chase like that. It makes it a waste of time.


    : could it be related to the fact that the nb_host_info structure has pointers to structures with pointers, is there some sort of recursion going on that my compiler and os (and really REALLY crappy machine) cannot handle?

    There's no recursion here, but it is a Bad Thing if you are using those pointers before you malloc enough memory for them to point to. IOW, the one single malloc call only allocates enough memory for the pointers themselves. They still aren't pointing anywhere valid. You need to allocate memory for each one of those pointer members to point to an object.


    : could it be related to the fact that one element of the nb_host_INFO structure is declared with 'struct' while the others are 'typedef struct'? im gonna try this on a win2k box tonight and see what happens.
    : thanks for all the help./

    No, all structs in your code have structure tags. struct tags sit in a different namespace than ordinary identifiers, which is where typedef names reside. Do not confuse the word "namespace" here with C++'s definition.


    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • [blue]
    Are you absolutely positive the crash occurs in the line you specified?
    I mean, the code you posted shows potentially harmfull stuff.
    You only malloc spece for four pointers. Those pointers should be malloc-ed also, each one individually.
    If omit to do that, the code is likely to crash when you use those pointers.
    [/blue]
    : Any suggestions as to how to identify a previous problem?
    : is there a debugging tool i should learn to use?
    [blue]
    There is a great debugging tool available:
    visit http://www.programmersheaven.com/zone16/cat286/index.htm and download GVHDebug.zip
    [/blue]

    : forgive my lack of knowledge, i am kindof fly-by-night here, learning as I go (and i DO read ALOT so plz no RTFMs ;).
    : THANK YOU
    : : Have you tried compiling on the portion you posted? It doesnt crash.
    : :
    : : I think your heap is getting corrupted [italic]before[/italic] you get to this code, your malloc that crashes is trying to access the heap, it bangs. Look at the code that gets called before the lines you posted.
    : :
    : :
    : :
    :
    :


    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

  • [b][red]This message was edited by AsmGuru62 at 2002-10-29 8:8:26[/red][/b][hr]
    [blue]If you are using VC++ it has a pile of debugging functions for memory management:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/_core_using_the_debug_heap.asp

    Use the flag to check the heap EVERY time the heap is accessed by malloc/realloc/free - your code will be awfully slow, but it will catch the issue. When debugger will stop - open the 'Call Stack' panel and you will see the code where the block which is damaged or invalid was called.[/blue]


  • Thanks everyone for the very helpful hints, i will definitely remember them for the future.
    To get a clear idea of exactly where the problem is, I have totally rewritten the program, and now find myself with questions of the same nature. I guess I should start from the very beginning:
    I am communicating with several remote machines.
    I will be recieving a chunk of data from each.
    Each chunk of data will contain a variable amount of certain records, the number of records specified by a certain byte in the chunk.
    SO, for each machine I communicate with, I will need a structure like:
    [code]
    struct namestruct {
    int count_of_names;
    char **names;
    };
    [/code]
    How do I malloc() for this structure? Presuming I can know the number of records (names) ahead of time, and the maximum number of bytes in each name, would it be:
    [code]
    struct namestruct name;
    int size = sizeof(int) + (namecount*MAX_NAME_SIZE*sizeof(char))
    name = (char *)malloc(size);
    [/code]
    No?
    Actually, as I asked that question, another came up.
    Given that I know the maximum number of bytes in each name, could I rewrite the struct as:
    [code]
    struct namestruct {
    int count;
    char (*names)[MAX_NAME_SIZE];
    }
    [/code]

    THANKS!

    -Mike



    : hello everyone.
    : i am having a problem with malloc.
    : i need to allocate memory for a structure holding pointers to other structures.
    : when malloc is called, i get an IPF (windows 95 osr2), one of those normal ones that happen
    : when you try to use a pointer incorrectly.
    : i am using borland's 32 bit c compiler, command line.
    : anyone have any ideas?
    :
    : [code]
    : typedef struct nbname_response_header {
    : unsigned short transaction_id;
    : unsigned short flags;
    : unsigned short question_count;
    : unsigned short answer_count;
    : unsigned short name_service_count;
    : unsigned short additional_record_count;
    : char question_name[34];
    : unsigned short question_type;
    : unsigned short question_class;
    : unsigned long ttl;
    : unsigned short rdata_length;
    : unsigned char number_of_names;
    : } nbname_response_header_t;
    :
    : struct nbname {
    : char ascii_name [16] ;
    : unsigned short rr_flags;
    : };
    :
    : typedef struct nbname_response_footer {
    : unsigned char adapter_address [6];
    : unsigned char version_major;
    : unsigned char version_minor;
    : unsigned short duration;
    : unsigned short frmps_received;
    : unsigned short frmps_transmitted;
    : unsigned short iframe_receive_errors;
    : unsigned short transmit_aborts;
    : unsigned long transmitted;
    : unsigned long received;
    : unsigned short iframe_transmit_errors;
    : unsigned short no_receive_buffer;
    : unsigned short tl_timeouts;
    : unsigned short ti_timeouts;
    : unsigned short free_ncbs;
    : unsigned short ncbs;
    : unsigned short max_ncbs;
    : unsigned short no_transmit_buffers;
    : unsigned short max_datagram;
    : unsigned short pending_sessions;
    : unsigned short max_sessions;
    : unsigned short packet_sessions;
    : } nbname_response_footer_t ;
    :
    : // sizeof nb_host_info is 16, i guess 4*sizeof(int)?
    :
    : struct nb_host_info {
    : struct nbname_response_header* header;
    : struct nbname* names;
    : struct nbname_response_footer* footer;
    : int is_broken;
    : };
    :
    : int main(){
    : struct nb_host_info *hostinfo = NULL;
    : if((hostinfo = malloc(sizeof(struct nb_host_info)))==NULL) return 1; // she blows up here
    : return 0;
    : }
    : [/code]
    :

  • [b][red]This message was edited by DB1 at 2002-10-30 14:46:56[/red][/b][hr]
    : Thanks everyone for the very helpful hints, i will definitely remember them for the future.
    : To get a clear idea of exactly where the problem is, I have totally rewritten the program, and now find myself with questions of the same nature. I guess I should start from the very beginning:
    : I am communicating with several remote machines.
    : I will be recieving a chunk of data from each.
    : Each chunk of data will contain a variable amount of certain records, the number of records specified by a certain byte in the chunk.
    : SO, for each machine I communicate with, I will need a structure like:
    : [code]
    : struct namestruct {
    : int count_of_names;
    : char **names;
    : };
    : [/code]
    : How do I malloc() for this structure? Presuming I can know the number of records (names) ahead of time, and the maximum number of bytes in each name, would it be:
    : [code]
    : struct namestruct name;
    : int size = sizeof(int) + (namecount*MAX_NAME_SIZE*sizeof(char))
    : name = (char *)malloc(size);
    : [/code]
    : No?
    : Actually, as I asked that question, another came up.
    : Given that I know the maximum number of bytes in each name, could I rewrite the struct as:
    : [code]
    : struct namestruct {
    : int count;
    : char (*names)[MAX_NAME_SIZE];
    : }
    : [/code]
    :
    : THANKS!
    :
    : -Mike
    :
    :
    :
    [code]
    struct namestruct {
    int count;
    char names[MAX_NAME_SIZE];
    };

    /*
    ...
    ...
    */

    struct namestruct *names;
    names = malloc(sizeof(struct namestruct) * NUM_OF_RECORDS);
    [/code]










Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories