New and Malloc mixed together 
Author Message
 New and Malloc mixed together

Hi all,

That is probably a fairly dumb question however, I a not sure I am allowed
to do what I do.

Here is the story.

I have a master class M that I subclass in several subclasses C1, C2...Cn.

When instanciating my bunch of object I use a global type as below
    LPM *globalPointer.

When creating all the objects I do as follow

    globalpointer=malloc(sizeof(LPM)*NbObjects );

Then I instanciate my subclasses using the new allocator like

    for loop
    {
        GlobalPointer[i]=new C1(...);
    }

In my C1 classe I have the same kind of architecture with array of pointer
on class. The array is created using malloc and the classes instanciated
using new.

All goes good until I get enough objects to{*filter*}something in the memory.
And new or malloc happilly bombs out with a memory protection error. I guess
I must have done something wrong somewhere. Is there anything in what I
describe above that is utterly NOT ALLOWED and that I have overlooked.

That you fdor your help, feel free to ask for more information.

Stephane



Sat, 29 Jun 2002 03:00:00 GMT  
 New and Malloc mixed together
You can mix malloc and new provided you don't delete malloc'd memory or free
new'd memory, but it doesn't seem like a sensible thing to do. Why not
replace:

Quote:
>     globalpointer=malloc(sizeof(LPM)*NbObjects );

with

globalpointer=new LPM[NbObjects];

which is shorter, easier to read and less error prone.

NeilB


Quote:
> Hi all,

> That is probably a fairly dumb question however, I a not sure I am allowed
> to do what I do.

> Here is the story.

> I have a master class M that I subclass in several subclasses C1, C2...Cn.

> When instanciating my bunch of object I use a global type as below
>     LPM *globalPointer.

> When creating all the objects I do as follow

>     globalpointer=malloc(sizeof(LPM)*NbObjects );

> Then I instanciate my subclasses using the new allocator like

>     for loop
>     {
>         GlobalPointer[i]=new C1(...);
>     }

> In my C1 classe I have the same kind of architecture with array of pointer
> on class. The array is created using malloc and the classes instanciated
> using new.

> All goes good until I get enough objects to{*filter*}something in the memory.
> And new or malloc happilly bombs out with a memory protection error. I
guess
> I must have done something wrong somewhere. Is there anything in what I
> describe above that is utterly NOT ALLOWED and that I have overlooked.

> That you fdor your help, feel free to ask for more information.

> Stephane



Sat, 29 Jun 2002 03:00:00 GMT  
 New and Malloc mixed together

Quote:
> Hi all,

> That is probably a fairly dumb question however, I a not sure I am allowed
> to do what I do.

> Here is the story.

> I have a master class M that I subclass in several subclasses C1, C2...Cn.

> When instanciating my bunch of object I use a global type as below
>     LPM *globalPointer.

> When creating all the objects I do as follow

>     globalpointer=malloc(sizeof(LPM)*NbObjects );

> Then I instanciate my subclasses using the new allocator like

>     for loop
>     {
>         GlobalPointer[i]=new C1(...);
>     }

> In my C1 classe I have the same kind of architecture with array of pointer
> on class. The array is created using malloc and the classes instanciated
> using new.

> All goes good until I get enough objects to{*filter*}something in the memory.
> And new or malloc happilly bombs out with a memory protection error. I guess
> I must have done something wrong somewhere. Is there anything in what I
> describe above that is utterly NOT ALLOWED and that I have overlooked.

You can mix source code with new and malloc. But you must sure to
release the memory with the corresponding Fuktion(Opperator). For your
Example you must call
for loop
{
  delete GlobalPointer[i];

Quote:
}

free( GlobalPointer );

--

"640K should be enough for anybody" -- Bill Gates



Sat, 29 Jun 2002 03:00:00 GMT  
 New and Malloc mixed together


[snip]

Quote:
>     globalpointer=malloc(sizeof(LPM)*NbObjects );

Why not change that to this?

     globalpointer = new LPM[NbObjects];

Then later you have the appropriate delete [] and you
don't need to worry about new/malloc.

[snip]

Quote:
> I must have done something wrong somewhere. Is there anything in what I
> describe above that is utterly NOT ALLOWED and that I have overlooked.

IIRC you are allowed to use new and malloc in the same code,
but, other than legacy code situations, there is no need.  I'm
pretty sure your problem is someplace else.

--
Dan Evens
Standard disclaimers etc. No spam please.
Two possible strategies if your boss is a jerk.
1) Polish up your resume and send it to headhunters.
2) Polish up *his* resume and send it to headhunters.



Sat, 29 Jun 2002 03:00:00 GMT  
 New and Malloc mixed together
The original poster mailed me this reply, but as far as I can see didn't
post it here:

Quote:
>Thank you for you help,
>I would be very happy to use the new LPM[],
>however How to I instanciate my sub classes, because as far as i
understand,
>the new LPM will only call teh default constructor, hwo do I call the
>constructor of the subclass I wish to instanciate?
>Thanks for your help
>Stephane

I think you need to post some real code so we can see what you're trying to
do. You can't call a constructor via a pointer. From what you've posted so
far the object creation code will remain unchanged i.e.:

Quote:
> >         GlobalPointer[i]=new C1(...);

NeilB


Quote:
> You can mix malloc and new provided you don't delete malloc'd memory or
free
> new'd memory, but it doesn't seem like a sensible thing to do. Why not
> replace:

> >     globalpointer=malloc(sizeof(LPM)*NbObjects );

> with

> globalpointer=new LPM[NbObjects];

> which is shorter, easier to read and less error prone.

> NeilB



> > Hi all,

> > That is probably a fairly dumb question however, I a not sure I am
allowed
> > to do what I do.

> > Here is the story.

> > I have a master class M that I subclass in several subclasses C1,
C2...Cn.

> > When instanciating my bunch of object I use a global type as below
> >     LPM *globalPointer.

> > When creating all the objects I do as follow

> >     globalpointer=malloc(sizeof(LPM)*NbObjects );

> > Then I instanciate my subclasses using the new allocator like

> >     for loop
> >     {
> >         GlobalPointer[i]=new C1(...);
> >     }

> > In my C1 classe I have the same kind of architecture with array of
pointer
> > on class. The array is created using malloc and the classes instanciated
> > using new.

> > All goes good until I get enough objects to{*filter*}something in the
memory.
> > And new or malloc happilly bombs out with a memory protection error. I
> guess
> > I must have done something wrong somewhere. Is there anything in what I
> > describe above that is utterly NOT ALLOWED and that I have overlooked.

> > That you fdor your help, feel free to ask for more information.

> > Stephane



Sat, 29 Jun 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Mixing (new/delete) and (free/malloc)

2. Pointers, malloc and typedefs together

3. C question : redefining a new malloc() calling standard malloc()

4. Linking old and new iostream together

5. New SDK and MFC don't work together

6. New SDK and MFC don't work together

7. Is mixing malloc(), free() and new, delete OK in the same code?

8. The MIX debugger - a MIXed bag of junk

9. malloc/new eats up memory

10. Definition of malloc(0) and ::operator new(0)

11. why malloc for a new node in linked list

12. malloc, new in a ISR, safe or not ?

 

 
Powered by phpBB® Forum Software