new-operator

Will the new expression ever return a pointer to an array?

主宰稳场 提交于 2019-12-04 19:50:05
问题 In this excellent answer by AndreyT, he explains that in C, when a function needs an array whose dimension is known at compile-time, it's a major technique-level error to declare void process_array(int *ptr, size_t plen); instead of void process_array(int (*arr_ptr)[10]); Furthermore, he opines that many programmers are oblivious to the second option and know only about the first. One of the reasons, he writes, for this behaviour is when an array needs to be dynamically allocated and passed

Pros and cons of 'new' properties in C# / .Net?

三世轮回 提交于 2019-12-04 19:12:33
问题 Considering the following sample code: // delivery strategies public abstract class DeliveryStrategy { ... } public class ParcelDelivery : DeliveryStrategy { ... } public class ShippingContainer : DeliveryStrategy { ... } and the following sample Order class: // order (base) class public abstract class Order { private DeliveryStrategy delivery; protected Order(DeliveryStrategy delivery) { this.delivery = delivery; } public DeliveryStrategy Delivery { get { return delivery; } protected set {

Using delete on pointers passed as function arguments

岁酱吖の 提交于 2019-12-04 19:01:40
问题 Is it okay( and legal) to delete a pointer that has been passed as a function argument such as this: #include<iostream> class test_class{ public: test_class():h(9){} int h; ~test_class(){std::cout<<"deleted";} }; void delete_test(test_class* pointer_2){ delete pointer_2; } int main(){ test_class* pointer_1; while(true){ pointer_1 = new test_class; //std::cout<<pointer_1->h; delete_test(pointer_1); } } This compiles fine now, but I just want to make sure it'll always be that way. 回答1: It will

Why new[-1] generates segfault, while new[-2] throws bad_alloc?

若如初见. 提交于 2019-12-04 17:31:29
问题 I tried to test bad_alloc exception by passing some negative arguments to new[] . When passing small negative numbers I get what I hoped for - a bad_alloc . However, when passing -1 , I can see that my object is constructed thousands of times (I print static counter in constructor) and the application terminates with segfault. new[] converts signed integer to size_t , so -1 is the max of size_t and -2 is the maximum - 1 and so on. So why new[] throws exception when receiving some huge number,

overloading new and delete

白昼怎懂夜的黑 提交于 2019-12-04 16:49:01
I try to follow this article: http://flipcode.com/archives/How_To_Find_Memory_Leaks.shtml to overload my new and delete functions in order to track memory leaks. however - if I try to compile, I get a C2365: "operator new": redefinition; previous definition was a "function" in the file xdebug xdebug gets included in xlocale - however, i can't find where my project is including xlocale I'm using MFC for multithreading in my project. Can someone tell me how I can get my memory leak tracking to work? //edit: So this is my findMemoryLeak.h which i include in the end of stdafx.h #ifndef

new operator in function call

谁说胖子不能爱 提交于 2019-12-04 15:17:52
My question is what happens to the object allocated with the new operator that is inside a function call. A specific example: I have a private vector pV which I want to send to a object/function outside of the class, foo->func(std::vector<int> *vec) . I first tried to write foo->func( new std::vector<int>(pV) ) but this resulted in a memory leak (when said function is called repeatedly inside a loop). When I specifically created a new object, called the function and then deleted that object, the whole thing worked, without the memory leak. Shouldn't the newly created object 'expire' and be

SimpleXML enum case-sensitivity

前提是你 提交于 2019-12-04 14:46:29
I have been trying to create an XML using the simplexml library (v2.6.2) http://simple.sourceforge.net/home.php The XML I want to create has to hold an enum value, which should be case-sensitive. Following is the POJO : package pojos; public enum MyEnum { NEW("new"), OLD("old"); private final String value; MyEnum(String v) { value = v; } public String value() { return value; } public static MyEnum fromValue(String v) { for (MyEnum c: MyEnum.values()) { if (c.value.equals(v)) { return c; } } throw new IllegalArgumentException(v); } } Following is the serializer code : import java.io.File;

new and delete operator overloading for dll

大兔子大兔子 提交于 2019-12-04 12:39:41
How to overload new and delete operator for a dll . I have written overloaded operators as part of the dll , but client linking with this dll does not use overloaded new and delete Here is what the C++ standard has to say about this, in section 17.6.4.6/3: The program's definitions (of the new / delete operators) are used instead of the default versions supplied by the implementation. Such replacement occurs prior to program startup. The program's definitions shall not be specified as inline . No diagnostic is required. If you read that carefully, it exactly explains the trouble you are having

Why do I need to call new? [duplicate]

孤者浪人 提交于 2019-12-04 12:39:09
Possible Duplicates: When to use “new” and when not to, in C++? When should I use the new keyword in C++? It seems like I could program something without ever using the word new , and I would never have to worry about deleting anything either, so why should I ever call it? From what I understand, it's because I would run out of stack memory. Is this correct? I guess my main question is, when should I call new ? It's a matter of object lifetime: if you stack-allocate your objects, the objects destructors will be called when these objects go out of scope (say, at the end of the method). This

Getting dynamically allocated array size

穿精又带淫゛_ 提交于 2019-12-04 12:30:40
In "The C++ Programming Language" book Stroustrup says: "To deallocate space allocated by new , delete and delete[] must be able to determine the size of the object allocated. This implies that an object allocated using the standard implementation of new will occupy slightly more space than a static object. Typically, one word is used to hold the object’s size. That means every object allocated by new has its size located somewhere in the heap. Is the location known and if it is how can I access it? In actual fact, the typical implementation of the memory allocators store some other