new-operator

How does the standard new operator work in c++?

What are all the other things a new operator does other than allocating memory and calling a constructor? ...

Why do C# and Java bother with the "new" operator?

Why does the new operator exist in modern languages such as C# and Java? Is it purely a self documenting code feature, or does it serve any actual purpose? For instance the following example: Class1 obj = new Class1(); Class1 foo() { return new Class1(); } Is as easy to read as the more Pythonesque way of writing it: Class1 obj...

C++ operator new, object versions, and the allocation sizes

Hi. I have a question about different versions of an object, their sizes, and allocation. The platform is Solaris 8 (and higher). Let's say we have programs A, B, and C that all link to a shared library D. Some class is defined in the library D, let's call it 'classD', and assume the size is 100 bytes. Now, we want to add a few members ...

Am I using new operator correctly?

I have the following pointer. char **x = NULL; x is will point to an array of pointers. So is the following code correct? x = new (nothrow) (*char)[20]; and we will dealocate it using delete[] x; Is x = (char **) malloc(sizeof(char **) * 20); and x = new (nothrow) (*char)[20]; equivalent? ...

Access Silverlight's createObject via Javascript's new operator?

feel free to skip down to the question below I'm trying to create a bridge so a huge javascript application can be replaced by a new Silverlight-based rewrite as smoothly as possible. In the legacy application, there is this JS class: function LatLng(lat, lng, url) { /* snip... */ } And it is being used a lot throughout my customer'...

C++ new operator. Creating a new instance

I'm having some trouble creating an object in C++. I create a class called Instruction, and I am trying to create a new instance, but I get compiler errors. Class code: class Instruction{ protected: string name; int value; public: Instruction(string _name, int _value); ~Instruction(); void setName(string _name...

How to prevent a globally overridden "new" operator from being linked in from external library

In our iPhone XCode 3.2.1 project, we're linking in 2 external static C++ libraries, libBlue.a and libGreen.a. libBlue.a globally overrides the "new" operator for it's own memory management. However, when we build our project, libGreen.a winds up using libBlue's new operator, which results in a crash (presumably because libBlue.a is maki...

Recursive calls when trying to wrap and override the global operator new.

I have not programmed C++ for a while and encountered a strange behavior when toying with overloaded global operators new and delete. The essence of the problem seems to be that a wrapper build around the default global new and residing in a separate source file nevertheless calls an operator new overloaded in another (and separately c...

Using constructor without operator 'new'

Please help me to understand why the following code works: <script> var re = RegExp('\\ba\\b') ; alert(re.test('a')) ; alert(re.test('ab')) ; </script> In the first line there is no new operator. As far as I know, a contructor in JavaScript is a function that initialize objects created by the operator new and they are n...

Explicitly disallow heap allocation in C++...

I have a number of classes that I would like to explicitly disallow heap allocation for. It occurred to me this weekend that I could just declare operator new private (and unimplemented)... Sure enough, this results in compile errors when you attempt to new the class... My question is: Is there more to this? Am I missing something or is ...

c++ how to ? function_x ( new object1 )

Hi i want to do the next instead of MyClass object; function_x (object); i want to function_x ( new object ); so what will be the structure of the MyClass to be able to do that .. if i just compiled it , it gives me a compile time error answer function_x (MyClass() ) New Edit thanks for the quick answers.. i did ask the w...

How to initialise memory with new operator in C++?

I'm just beginning to get into C++ and I want to pick up some good habits. If I have just allocated an array of type int with the new operator, how can I initialise them all to 0 without looping through them all myself? Should I just use memset? Is there a C++ way to do it? ...

Dynamic memory and inherited structs in C++

Say I have some structs like this: struct A{ int someInt; } struct B : public A{ int someInt; int someOtherInt; } And a class: class C{ A *someAs; void myFunc(A *someMoreAs){ delete [] someMoreAs; } } would this cause a problem: B *b=new B[10]; C c; c.myFunc(b); Because it's deleting b, thinking that it's of type A, whi...

In a project that uses a DI framework, should you NEVER use the 'new' operator?

I'm trying to wrap my head around Dependency Injection. One of the things I'm confused about is whether all of your object instantiation needs to be controlled by the DI framework (Spring, Guice, etc). Or, if not, how do you determine which objects are instantiated by the framework and which objects are instantiated with the new oper...

Creation of an object in C++

Isn't A a = new A(); // A is a class name supposed to work in C++? I am getting: conversion from 'A*' to non-scalar type 'A' requested Whats wrong with that line of code? This works in Java, right? Also, what is the correct way to create a new object of type A in C++, then? ...

Correct way to initialize dynamic Array in C++

Hey guys, I'm currently working on a C++ project, where dynamic arrays often appear. I was wondering, what could be the correct way to initialize a dynamic array using the new-operator? A colleague of mine told me that it's a no-no to use new within the constructor, since a constructor is a construct that shouldn't be prone to errors or...

how to handle delete by illegal address

Suppose we have a situation like this. Suppose instead of "p = " we called some function(written by someone which invalidate our pointer). How to handle this problem? How to protect code from crashes? I know about and use boost smart pointers. But what to do if we have this situation. struct Test { int a; int b; int c; }; T...

When is #include <new> library required in C++?

Hi, According to this reference entry for operator new ( http://www.cplusplus.com/reference/std/new/operator%20new/ ) : Global dynamic storage operator functions are special in the standard library: All three versions of operator new are declared in the global namespace, not in the std namespace. The first and second...

Array of Structs Initialization....

Hi I am working on a program where I have to initialize a deck of cards. I am using a struct to represent a card. However I'm not filling it correctly as I get a bunch of zero's when I display the deck of cards. I believe my mistake is in this line but I'm not sure: struct card temp = {"Clubs", value, false}; The code: void initCard...

instantiate class from class object

In java, can I use a class object to dynamically instantiate classes of that type? i.e. I want some function like this. Object foo(Class type) { // return new object of type 'type' } ...