Skip to main content
gamedev.net
🔒 Locked

Simple Constructor Question

Started by Indeterminatus 1 reply 400+ views
Original Post
Indeterminatus
Indeterminatus
Hi! I have a very quick-to-answer-question. Does the following code lead to a crash?
  
struct     my_struct
{
  char    *_a_string;

  // Constructor

  my_struct()
  {
    _a_string = (char *)calloc( 20, sizeof( char ) );
  }

  ~my_struct()
  {
    free( (void *)_a_string );
  }
};
   
The point is whether the function call of malloc or calloc(never mind) in a constructor is recommended or not. I experienced a crash in my program, even before main-routine enters, and I use structs and classes similar to the above scheme. I can't locate the error, but I'm not sure if THAT might be the source of all evil or not... Thanks in advance for your help! Yours, Indeterminatus --si tacuisses, philosophus mansisses-- [edited by - Indeterminatus on May 27, 2002 4:26:16 PM]
Indeterminatus--si tacuisses, philosophus mansisses--
Fruny
Fruny
Nothing wrong with that, though the C allocations functions are superceded by new and the fact that you must not mix them (new/delete - malloc/free).



Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Indeterminatus
Indeterminatus
Thanks! You helped me a lot...looks like i had to do another debugging-session...darn...*gg*

Yours,

Indeterminatus

--si tacuisses, philosophus mansisses--
Indeterminatus--si tacuisses, philosophus mansisses--

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.