operators new/delete in constructor/destructor

Started by
18 comments, last by kudi 21 years, 6 months ago
stoffel, sorry, i dont understand what you mean, perhaps it is because i am not so good in english.

what do you mean with "copied by value", or how can i solve the problem?
The problem of Object Oriented Programming:Everybody tells you how to use his classes, but nobody how not to do it !
Advertisement
Class a; // This allocates memory on construction
Class b; // This also allocates memory on construction

a = b; // a and b now use the memory b was pointing to

// a and b get destructed, and free the same data (crash!)
// also the memory a used before is leaked.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
siaspete, that''s not possible in my program, becaus i dont do anything with my class.

just:

void main()
{
while(1)
{
example var;
}
}


The problem of Object Oriented Programming:Everybody tells you how to use his classes, but nobody how not to do it !
my application is multithreaded. could that be the problem?

and another question:
which runtime library do i have to select in the "project settings -> Code generation", or what are the differences. can anybody explaine that?

-multithreaded
-multithreaded DLL
-debug multithreaded
-debug multithreaded DLL


The problem of Object Oriented Programming:Everybody tells you how to use his classes, but nobody how not to do it !
Kudi:

"Pass-by-value" means that the object is being passed into a function by value instead of by pointer or reference (which are both referred-to as "pass-by-reference", which is admittedly confusing).

Example of how your program will crash with your object constructed as you say:

  void foo (example e) // pass-by value{  // doesn''t really need to do anything..}void main (){  while (true)  {    example e; // ctor called, data allocated at 0x12345678    foo (e); // creates a copy of data by value--now there             // is a second example object with data at             // 0x12345678             // as function returns, object is destroyed and             // 0x12345678 is deleted  } // end of loop; ~example is called on e, trying to delete    // 0x1234567, but it''s already been deleted.  Crash.}  

Now, this will crash instantly. If it''s crashing after a while, then something else is going on. If possible, you might have to show us your code of the main loop in its entirety, and at least the function declarations of every function you call.
show us the real code, please.

also why do you need to do 'new'?
char data[SIZE]  

works just as well as long as SIZE is a constant, known at compile time.

if you're using c++ and need a variable size try using vector&ltchar> as that can be resized and won't cause you memory allocation issues.

[edited by - petewood on October 7, 2002 1:51:41 PM]
thanks for all your answers, but i think i know what my mistake was (i am still not sur, because it is a runtime error and it sometimes takes 10 minutes until it appears). i didn''t chose multihreaded in /project settings/code generation/runtime library

actually i am a bit confused, because i think i had activated it once it and it still didnt function, but i will see. or it also could be the printf function which needs mutexes?
The problem of Object Oriented Programming:Everybody tells you how to use his classes, but nobody how not to do it !
Many standard library functions are not threadsafe. That''s what the multi-threaded runtime is for. Using the single-threaded runtime can cause crashes or undefined behaviour at seemingly random times.

Kippesoep
does this slow down my program?
The problem of Object Oriented Programming:Everybody tells you how to use his classes, but nobody how not to do it !
In general, yes. But if you need multi-threading, you need multi-threading.

This topic is closed to new replies.

Advertisement