Re: TREE structure in MFC, how, where and why?
From: Mike Stephenson (none_at_none.com)
Date: 08/16/04
- Next message: Jeff Partch [MVP]: "Re: The Edit Control of a Combo Box..."
- Previous message: Mark Robinson: "Re: How to get a list of all the controls of a CDialog?"
- In reply to: Joseph M. Newcomer: "Re: TREE structure in MFC, how, where and why?"
- Next in thread: Joseph M. Newcomer: "Re: TREE structure in MFC, how, where and why?"
- Reply: Joseph M. Newcomer: "Re: TREE structure in MFC, how, where and why?"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 16 Aug 2004 16:05:55 GMT
The reason you should use new/delete instead of malloc/free is more than a
matter of quaintness.
Your struct contains a CString object, which is a C++ class with various
overloaded constructors.
"malloc", a C-language construct, simply allocates memory and assigns it a type,
whereas "new", a C++ construct, knows about classes. One of the things that new
does is guarantee that the constructor of your object and any member objects are
called upon creation, in addition to allocating memory and assigning a type.
By using malloc, the constructor on your CString is not being called. This
constructor (the default constructor in this case since you pass no parameters),
is probably required to set up an internal buffer, etc., and since it is not
called none of this is happening, and you probably get an memory fault.
Same goes for "free" vs. "delete". "free" simply deallocates the memory,
whereas "delete" guarantees that the object's destructor and any member objects'
destructors are called.
On Sun, 15 Aug 2004 00:04:22 -0400, Joseph M. Newcomer <newcomer@flounder.com>
wrote:
>THis is C++. Therefore, it should be
>
>class tree {
> public:
> tree * parent;
> CArray<tree *, tree*>children;
> CString filename;
>};
>
>There is no need to have two different object types because the only difference between a
>terminal node and a tree node is that a terminal node has no children.
>
>Or, if you want to differentiate them,
>
>class B;
>
>class A {
> public:
> A() { B = NULL; }
> B * parent;
> CString name;
>};
>
>class B : public A {
> public:
> virtual ~B { children.RemoveAll(); }
> CArray<B*, B*> children;
>};
>
>
>
>Then you can use virtual methods to determine what action you take. Generally, when using
>C++, you should immediately, upon writing 'struct', think 'I should be using a class
>here". Almost always, the answer will be "Use a class".
>
>Stack is probably irrelevant since it is unlikely you will have a directory depth large
>enough to exhaust the megabyte of stack space. Heap space is only based on how many files
>you have on the path and subcomponents. It is possible that with a sufficiently large file
>system you might get tight, bug figure you've got to have enough files to consume about a
>gigabyte before it starts being troublesome in terms of address space. Paging traffic
>could be a more serious problem in performance..
>
>I have no idea why you would be using malloc in C++. It is generally considered quaintly
>obsolete. Use new.
>
>Since you don't give anything other than a trivial example and a poorly-documented access
>fault, it is impossible to guess what has gone wrong.
>
>But get rid of the struct declarations, the malloc, use classes and constructors, and read
>about how to add a helper function that will delete an element of the array when it is
>removed. Or use STL (I don't use it, so can't help much there).
> joe
>
>
>On Thu, 12 Aug 2004 16:19:20 -0700, Casper <casper@jbr.dk> wrote:
>
>>In scanning a drive and comparing file content, I need to remember
>>filepaths to every single file I encounter so I can take actions on
>>certain files later on.
>>
>>Rather than having a huge list enumerating the complete filepath to
>>every file it seems the smarter way (faster, more memmory efficient) is
>>to model the filesystem treestructure in a abstract tree - and having
>>only the filenames & node pointer in a CArray.
>>
>>struct tree {
>> struct tree *parent;
>> struct tree *child;
>> CString filename;
>>};
>>
>>struct file {
>> CString name;
>> struct tree *path;
>>};
>>
>>Does this sound like a good idea? Specifically, can I utilize an
>>existing data structure? What about stack/heap issues and tradeoffs?
>>
>>Not being awfully experienced with trees and pointers, why won't this run:
>>
>>struct tree *root;
>>root = ( struct tree * ) malloc ( sizeof( struct tree ) );
>>root->filename = "Test";
>>
>>---
>>Unhandled exception at 0x0041b07b in DubWiz.exe: 0xC0000005: Access
>>violation reading location 0xcdcdcdc1. (atlsimpstr.h, GetLength() method)
>>---
>>
>>Thanks in advance,
>>Casper
>
>Joseph M. Newcomer [MVP]
>email: newcomer@flounder.com
>Web: http://www.flounder.com
>MVP Tips: http://www.flounder.com/mvp_tips.htm
- Next message: Jeff Partch [MVP]: "Re: The Edit Control of a Combo Box..."
- Previous message: Mark Robinson: "Re: How to get a list of all the controls of a CDialog?"
- In reply to: Joseph M. Newcomer: "Re: TREE structure in MFC, how, where and why?"
- Next in thread: Joseph M. Newcomer: "Re: TREE structure in MFC, how, where and why?"
- Reply: Joseph M. Newcomer: "Re: TREE structure in MFC, how, where and why?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|