Using new for pointer to object


Hi all,

I would like to dynamically allocate space for an class instance and have its constructor called. However, it seems I am unable to get this right.

Basically, I have a class X which has other private member variables including classes Y and Z (where Y != Z). I'm getting segmentation faults and when I run my program through valgrind, it is when X is created (at the first line of main ()).

At first, I was doing this:

y_instance = new Y

which in valgrind was giving me an error about accessing data 0 bytes after 136 bytes -- so, I thought that maybe this wasn't allocating enough space for Y. I looked at:

http://www.cplusplus.com/reference/std/new/operator%20new/

and thought that when I use malloc () under C, I had to do a sizeof (). So, I tried this instead:

Y * y_instance = (Y*) operator new (sizeof(Y));

The error in X's constructor was gone, but I then got another error later on. I added a cerr inside Y's constructor, and indeed, it's not being called.

So...I'm kind of confused. How should I be doing it? Was I on the wrong track by doing "operator new"?

Prior to making Y and Z as pointers in X, they were normal variables (i.e., non-pointers). Also had a segfault and the error was less understandable. I tested Y and Z by themselves and there were no memory problems (using valgrind). So, I think it is a problem with how X is creating them, but I'm not so sure anymore...

I'd appreciate any suggestions... Thank you!

Ray


y_instance = new Y;

This is correct. If you're getting a seg fault it's due to something else, not this.

Y * y_instance = (Y*) operator new (sizeof(Y));

Using "operator new" and just using "new" are two very different things. operator new simply allocates space and does not call the ctor (similar to malloc).

Using "new" as above does both -- it allocates the space and calls the ctor.

Prior to making Y and Z as pointers in X, they were normal variables (i.e., non-pointers). Also had a segfault and the error was less understandable.


You should keep them as objects (non-pointers) unless you have a good reason to allocate them dynamically. Dynamically allocating stuff can add additional complications and memory management issues.

So, I think it is a problem with how X is creating them, but I'm not so sure anymore...


It probably has nothing to do with any of this. You probably have heap corruption somewhere else in the program. Triple check and make sure you aren't stepping outside of any array bounds anywhere, or dereferencing any bad pointers.
Using "operator new" and just using "new" are two very different things. operator new simply allocates space and does not call the ctor (similar to malloc).


Uh wait, isn't new(int) placement new? That should NOT allocate space, but call the constructor, no?
Placement new() takes a pointer, so no. See http://www.cplusplus.com/reference/std/new/operator%20new/ for clarification.

@OP: Please post minimal (but correct) code that reproduces the problem. It will be the fastest way to diagnose. See how Disch is trying to guess. If you provide code, it will be much faster.
Hi Disch and all,

Thank you all for your help!

Sorry for not posting any minimal code -- the problem is I don't know where the problem is and where valgrind is saying is perhaps not correct (of course, not valgrind's fault). I've been working on this new () line for a couple of days and perhaps was barking up the wrong tree. The line that valgrind pointed out was later in my program when I used "operator new", so that is why I mistakenly thought I was on the right track...

Let me go back a few steps, make them non-pointers, use new () [it was like this a few days ago...], and see if I can find out where the problem is... Maybe I can either solve my problem or be able to formulate a better question...

Thank you!

Ray


Topic archived. No new replies allowed.