Share via

How can I disable automatic object initialization

slipbits 16 Reputation points
2022-09-24T20:17:45.96+00:00

I overloaded the 'new' operator in my list processing API. As part this overload I initialize some fields in the created object.

VS treats 'new' as a space allocator. When the object constructor is called, VS automatically initializes the object contents, overwriting my initializations. Is there any way to disable this feature?

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


3 answers

Sort by: Most helpful
  1. Cristian SPIRIDON 4,486 Reputation points Volunteer Moderator
    2022-09-25T06:05:57.817+00:00

    Hi,

    I think VS is not doing what you are saying.
    In your overloaded new operator you allocate the memory where and how you want and you can modify the memory after allocation.

    It must be in the constructor of the class where the memory get updated.

    Did you debug this? Have a breakpoint after your initialization in new operator and in class constructor and observe the memory content.

    Hope this helps!

    Was this answer helpful?

    0 comments No comments

  2. slipbits 16 Reputation points
    2022-09-24T22:37:57.933+00:00

    The issue is what is demanded by the problem. Let me spell it out and maybe you can suggest an alternate approach.

    My list processor allocates (new) and deallocates (delete) from a private pool of cells, called the AVSL. There are three sources of cell creation:

    1. From AVSL.
    2. From the stack.
    3. From the heap.

    Cells created from either the stack or the heap can not be put onto a list or deleted and put in the AVSL, or be a list header containing data. Further, a user with a stale pointer to a deallocated cell, now on the AVSL, must be prevented from using the cell. I handle these conditions by (1) making cell links private, and (2) posting data to the cell links which allow me to distingush the three cases., and to throw an exception when they are found.

    Without the ability to post 'special' link values during cell creation (new) and deletion I know of no way that I can detect how the cell was created and limit the use of the cells. The effect is to create an opportunity to corrupt lists and the AVSL in such a way that random errors will occor.

    There was a suggestion on another forum that contructors (misconstructors?) be made private, and that factories be created to take care of the issue. That can be a lot of work. On the other hand, the easiest way to mark the cells appropriately is to do so during cell creation, but this requires that the automatic initialzing of objects during constructor use be disabled.

    The software is some 16,000 lines long with an additional 50,000 to 60,000 lines of comments. It can be viewed at https://www.gnu.org/software/gslip/. The software was successfully tested in about 2014 using gcc.

    thanks

    Was this answer helpful?


  3. RLWA32 52,571 Reputation points
    2022-09-24T20:47:10.287+00:00

    Why try to alter the proper behaviors of new and constructors?

    Let the new operator do what it's supposed to do (allocate memory and call an object constructor) and perform initialization in the constructor.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.