Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

C, malloc(sizeof(structure_with_pointers))

abonghit1abonghit1 Posts: 71Member
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]
«1

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : 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]
  • whoiewhoie Posts: 672Member
    : : 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/


  • gustavosserragustavosserra Posts: 201Member
    [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]



  • whoiewhoie Posts: 672Member
    : [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/


  • DB1DB1 Posts: 1,142Member
    : 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]
  • abonghit1abonghit1 Posts: 71Member
    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]
    :

  • abonghit1abonghit1 Posts: 71Member
    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]
    :

  • pingpongpingpong Posts: 937Member
    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.


  • abonghit1abonghit1 Posts: 71Member
    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.
    :
    :
    :

  • pingpongpingpong Posts: 937Member
    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.
    : :
    : :
    : :
    :
    :

«1
Sign In or Register to comment.