Click here to Skip to main content
15,926,035 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralVisual C++ Linking Error Pin
afronaut21-May-02 12:29
afronaut21-May-02 12:29 
GeneralRe: Visual C++ Linking Error Pin
Rama Krishna Vavilala21-May-02 12:46
Rama Krishna Vavilala21-May-02 12:46 
GeneralRe: Visual C++ Linking Error Pin
Christian Graus21-May-02 12:51
protectorChristian Graus21-May-02 12:51 
GeneralRe: Visual C++ Linking Error Pin
Tim Smith21-May-02 14:01
Tim Smith21-May-02 14:01 
GeneralRe: Visual C++ Linking Error Pin
Christian Graus21-May-02 12:50
protectorChristian Graus21-May-02 12:50 
GeneralRe: Visual C++ Linking Error Pin
SevereHammer21-May-02 20:41
SevereHammer21-May-02 20:41 
GeneralReference material on Memory Management Pin
Anton A. Loukine21-May-02 12:05
Anton A. Loukine21-May-02 12:05 
GeneralRe: Reference material on Memory Management Pin
perlmunger21-May-02 17:29
perlmunger21-May-02 17:29 
I don't really know of any web sites the would lay out what you want exactly, but most beginning C books go into memory management in reasonable depth. Here's a few things to think about:

1. If you use a container class (e.g. CArray, std::vector, etc.), keep in mind that any TYPE that you use in that template class, must either be a pointer to that type, or the type must be a class that has a copy constructor defined. For instance:

// Assume you've created a class called MyClass
<br>
// Declare a CArray with type MyClass
CArray<MyClass, MyClass> m_myClassArray;
<br>
for( int x = 0; x < 2000; ++x )
{
    // Create a MyClass object.
    MyClass myClass;
    // Add it to the array
    m_myClassArray.Add( myClass );
}

This example works, but keep in mind that a copy was made with every call to add. Since this call was made several thousand times, you'd have a real bottleneck on your hands. Instead, you should define the container such that it takes pointers in the add statement. Like this:
// Declare a CArray with type MyClass
CArray<MyClass, MyClass*> m_myClassArray;
<br>
for( int x = 0; x < 2000; ++x )
{
    // Create a MyClass object.
    MyClass* pMyClass = new MyClass;
    // Add it to the array
    m_myClassArray.Add( pMyClass );
}

This time a pointer was copied. This is far more efficient. It would be helpful later on as well. Take a look:
for( int x = 0; x < m_myClassArray.GetSize(); ++x )
{
    MyClass* pMyClass = (MyClass*)m_myClassArray.GetAt( x );
    pMyClass->DoSomething();
}

If this had been done the other way, a copy would have been created for every call to GetAt(). Once again a huge bottleneck. (keep in mind that you must iterate through this whole array and call delete on each pointer when you are done with the container).

2. When you want to create a complex class/data structure inside a method and then pass that back to the caller, you *must* use the heap:
// Assume you created a class called MyClass

MyClass* CreateMyClass( CString str )
{
    MyClass* pMyClass = new MyClass( str );
    return pMyClass;
}

You could have it pass back a copy, but again this is inefficient and probably not what you want--especially when you are passing back a container. You just have to be careful that you delete that object when you're done.

I could give some other points here, but I've run out of time. Maybe some others can pick up where I left off.

In general, knowing when to use one over the other (i.e. stack v. heap), depends on the context of where you're working in the code. It is something that starts to make more sense the more programming you do.

-Matt



------------------------------------------

The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
GeneralRe: Reference material on Memory Management Pin
Alex Cramer21-May-02 20:14
Alex Cramer21-May-02 20:14 
GeneralRe: Structs: heap or stack Pin
Anton A. Loukine22-May-02 3:42
Anton A. Loukine22-May-02 3:42 
GeneralRe: Structs: heap or stack Pin
perlmunger22-May-02 5:57
perlmunger22-May-02 5:57 
GeneralOnFilePrint() and multiple switching views Pin
dazinith21-May-02 11:09
dazinith21-May-02 11:09 
GeneralRe: OnFilePrint() and multiple switching views Pin
Prem Kumar21-May-02 11:11
Prem Kumar21-May-02 11:11 
GeneralPaste text into next window Pin
Rickard Andersson2021-May-02 9:49
Rickard Andersson2021-May-02 9:49 
GeneralRe: Paste text into next window Pin
Prem Kumar21-May-02 10:29
Prem Kumar21-May-02 10:29 
GeneralRe: Paste text into next window Pin
Rickard Andersson2021-May-02 10:43
Rickard Andersson2021-May-02 10:43 
GeneralRe: Paste text into next window Pin
Prem Kumar21-May-02 10:44
Prem Kumar21-May-02 10:44 
GeneralRe: Paste text into next window Pin
Rickard Andersson2021-May-02 10:52
Rickard Andersson2021-May-02 10:52 
GeneralRe: Paste text into next window Pin
Prem Kumar21-May-02 10:55
Prem Kumar21-May-02 10:55 
QuestionEndless Loop? Pin
Emearg21-May-02 9:24
Emearg21-May-02 9:24 
AnswerRe: Endless Loop? Pin
Joaquín M López Muñoz21-May-02 9:36
Joaquín M López Muñoz21-May-02 9:36 
AnswerRe: Endless Loop? Pin
PJ Arends21-May-02 9:35
professionalPJ Arends21-May-02 9:35 
AnswerRe: Endless Loop? Pin
Bill Wilson21-May-02 12:04
Bill Wilson21-May-02 12:04 
GeneralRe: Endless Loop? Pin
Ravi Bhavnani21-May-02 12:26
professionalRavi Bhavnani21-May-02 12:26 
General"'StrRetToBuf' : undeclared identifier" even when i have included shlwapi.h and added shlwapi.lib Pin
redeemer21-May-02 8:10
redeemer21-May-02 8:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.