answersLogoWhite

0


Best Answer

There is no operator new in C.

In C++11, there are three versions of operator new, all of which can be overridden. Default behaviour is as follows:

(1) throwing allocation

void* operator new (std::size_t size);

If an allocation of the given size cannot be met, an exception will be thrown. There is no return value after an exception is thrown.

(2) nothrow allocation

void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) noexcept;

If an allocation of the given size cannot be met, nullptr is returned.

(3) placement

void* operator new (std::size_t size, void* ptr) noexcept;

Always returns ptr. You use this version when memory has already been allocated and you are merely providing placement for an object within that allocation.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the return value when new operator is not able to allocate memory in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Which is the unary operator used to dereference a pointer and return the value stored in it?

The asterisk (*) operator dereferences a pointer and returns the value stored in the memory pointed to by the pointer.


Is it possible to compare the return value of a function call with another value using a relational operator?

pancakes


How do you use a assignment statement?

Assignment is not a statement, it is an operator. You use the assignment operator in assignment expressions. int x = 100; // assigns the value 100 to the memory location referred to by x. int y = x; // assigns the value of x (100) to the memory location referred to by y. Classes can override and overload the assignment operator to ensure correct assignment behaviour, particularly important when the class includes a member pointer to memory owned by an instance of the class, or to ensure correct typecasting between objects of different types. class MyClass{ public: MyClass():m_pNum=new int(){} // default ctor MyClass(const MyClass):m_pNum=new int(*MyClass.m_pNum){} // copy ctor ~MyClass(){ delete( m_pNum ); } // dtor public: MyClass & operator= (int); // typecast assignment MyClass & operator= (MyClass &); // standard assignment private: int * m_pNum; // memory allocated by constructors. }; // Assignment operator override required to typecast an integer. MyClass & MyClass::operator= (int num){ *m_pNum = num; return( *this ); } // Assignment operator override required to ensure two instances // of MyClass do not share the same memory. MyClass & MyClass::operator= (MyClass & myClass){ *m_pNum = *myClass.m_pNum; return( *this ); }


What is a use of dereferencing operator?

A pointer variable contains the address to some memory location. "Dereferencing" the pointer means getting the value stored at that memory location.


What are subscripts in c language?

Subscripts are used when accessing arrays. An array is a contiguous block of memory containing two or more elements of the same type. Since each element is the same length, it is trivial to work out the offset address of one element relative to another using a zero-based index. For any type T, the type T[n] is an array of Ts with indices in the range 0 to n-1. We can also create arrays at runtime by allocating sufficient memory on the heap for the given type: void f(int n) { if (n<1) return; // cannot allocate 0 elements! int* ptr = malloc (n * sizeof(int)); // allocate memory for n integers if (ptr!=0) return; // ensure memory was allocated! ptr[0] = 42; // use subscript operator to assign a value to the first element // assign to other elements... // use array... free (ptr); // release memory as soon as we're finished with it }

Related questions

Which is the unary operator used to dereference a pointer and return the value stored in it?

The asterisk (*) operator dereferences a pointer and returns the value stored in the memory pointed to by the pointer.


Is it possible to compare the return value of a function call with another value using a relational operator?

pancakes


How do you use a assignment statement?

Assignment is not a statement, it is an operator. You use the assignment operator in assignment expressions. int x = 100; // assigns the value 100 to the memory location referred to by x. int y = x; // assigns the value of x (100) to the memory location referred to by y. Classes can override and overload the assignment operator to ensure correct assignment behaviour, particularly important when the class includes a member pointer to memory owned by an instance of the class, or to ensure correct typecasting between objects of different types. class MyClass{ public: MyClass():m_pNum=new int(){} // default ctor MyClass(const MyClass):m_pNum=new int(*MyClass.m_pNum){} // copy ctor ~MyClass(){ delete( m_pNum ); } // dtor public: MyClass & operator= (int); // typecast assignment MyClass & operator= (MyClass &); // standard assignment private: int * m_pNum; // memory allocated by constructors. }; // Assignment operator override required to typecast an integer. MyClass & MyClass::operator= (int num){ *m_pNum = num; return( *this ); } // Assignment operator override required to ensure two instances // of MyClass do not share the same memory. MyClass & MyClass::operator= (MyClass & myClass){ *m_pNum = *myClass.m_pNum; return( *this ); }


How you use malloc function in c?

void* malloc (size_t bytes); This means that malloc takes an argument which is the size of memory to allocate and returns a pointer to that memory which has been allocated. If the return value is NULL, then the request could not be satisfied. Each call to malloc must be balanced with a corresponding call to free, to release the memory. int pa = NULL; pa = (int*) malloc (sizeof(int) * 1000); /* allocate 1000 ints */ if (pa == NULL) throw exception... ... use pa free (pa); pa = NULL;


What is a use of dereferencing operator?

A pointer variable contains the address to some memory location. "Dereferencing" the pointer means getting the value stored at that memory location.


What are subscripts in c language?

Subscripts are used when accessing arrays. An array is a contiguous block of memory containing two or more elements of the same type. Since each element is the same length, it is trivial to work out the offset address of one element relative to another using a zero-based index. For any type T, the type T[n] is an array of Ts with indices in the range 0 to n-1. We can also create arrays at runtime by allocating sufficient memory on the heap for the given type: void f(int n) { if (n<1) return; // cannot allocate 0 elements! int* ptr = malloc (n * sizeof(int)); // allocate memory for n integers if (ptr!=0) return; // ensure memory was allocated! ptr[0] = 42; // use subscript operator to assign a value to the first element // assign to other elements... // use array... free (ptr); // release memory as soon as we're finished with it }


Associativity has no role to play unless the precedence of operator isHow much memory is required to store a value of type double?

yes


Delete memory release operator in c plus plus?

You can't physically delete memory, you can only delete a pointer to allocated memory, which subsequently releases the memory back to the system. The operator is delete, passing the pointer as the operand. If the pointer points to an array, then you must also use the index operator [] in front of the pointer name.int main(){// pointer to an int type with value 100int* ptr_int = new int(100);// ... use pointer ...// release the integerdelete ptr_int;// pointer to an array 100 int types (with undefined values)int* ptr_int_array = new int[100];// ... use array ...// release the arraydelete [] ptr_int_array;return(0);}


Which data type assigns values automatically?

None of the data types available in C assigns valur to the variable. Initially all the variables have a garbage value. But when we use calloc() to allocate memory dynamically only then it assigns NULL to the memory block assigned.


What is the value of relocation register?

what is resource allocate register why it is used


Which value is modified by an operator?

The value of the variable which is on the left side of the assignment operator. Example: a = 2


How is memory management achieved by pointers in c plus plus?

Memory management in C++ is achieved using the new and delete operators. These operators are synonymous with the malloc() and free() functions found in C, and which you can also use in C++ but then your code would not strictly be C++. However, the new and delete operators are implemented behind the scenes with malloc() and free(), so the distinction at this level is somewhat moot.Although the new operator and malloc() function effectively do the same job, the new operator greatly simplifies the process. Whereas malloc() requires that you specify the exact amount of memory required, the newoperator can determine the amount at compile time, based upon the type of memory required. Moreover, the new operator returns a pointer to the required type whereas the pointer returned by malloc() must be cast to the appropriate type.There's very little difference between the deleteoperator and the free() function, however they are not interchangeable. If memory was allocated with the newoperator, then it must be released with the delete operator, not the free() function.Apart from these differences, memory management in C++ is much the same as it was with C. Whether memory is allocated with the new operator or the malloc() function, you must maintain a pointer variable to hold the reference that is returned (or a NULL value if the allocation failed). When the memory is no longer required, it must be released back to the system by deleting or freeing the original pointer or a copy of the original pointer. At that point, all pointers to that memory are deemed invalid, and should be zeroed to ensure they no longer refer to invalid memory.Note that the malloc() function also has two variants: calloc() and realloc(). calloc() works much the same as malloc() but is used to allocate a count of contiguous memory blocks, like an array, while realloc() is used to release an existing allocation and allocate another to the same pointer. There is no equivalent operator for realloc()in C++.