new-operator

Why there is no placement delete expression in C++?

戏子无情 提交于 2019-12-17 04:28:43
问题 Why C++ hasn't placement delete that directly corresponds to the placement new, i.e. calls the destructor and calls appropriate placement delete operator? For example: MyType *p = new(arena) MyType; ... //current technique p->~MyType(); operator delete(p, arena); //proposed technique delete(arena) p; 回答1: operator delete is unique in being a non-member or static member function that is dynamically dispatched. A type with a virtual destructor performs the call to its own delete from the most

How do you 'realloc' in C++?

℡╲_俬逩灬. 提交于 2019-12-17 04:27:19
问题 How can I realloc in C++? It seems to be missing from the language - there is new and delete but not resize ! I need it because as my program reads more data, I need to reallocate the buffer to hold it. I don't think delete ing the old pointer and new ing a new, bigger one, is the right option. 回答1: Use ::std::vector! Type* t = (Type*)malloc(sizeof(Type)*n) memset(t, 0, sizeof(Type)*m) becomes ::std::vector<Type> t(n, 0); Then t = (Type*)realloc(t, sizeof(Type) * n2); becomes t.resize(n2); If

In Ruby, what's the relationship between 'new' and 'initialize'? How to return nil while initializing?

烈酒焚心 提交于 2019-12-17 04:18:51
问题 What I want is: obj = Foo.new(0) # => nil or false This doesn't work: class Foo def initialize(val) return nil if val == 0 end end I know in C/C++/Java/C#, we cant return a value in a constructor. But I'm wondering whether it is possible in Ruby. 回答1: In Ruby, what's the relationship between ' new ' and ' initialize '? new typically calls initialize . The default implementation of new is something like: class Class def new(*args, &block) obj = allocate obj.initialize(*args, &block) # actually

In Ruby, what's the relationship between 'new' and 'initialize'? How to return nil while initializing?

拈花ヽ惹草 提交于 2019-12-17 04:17:34
问题 What I want is: obj = Foo.new(0) # => nil or false This doesn't work: class Foo def initialize(val) return nil if val == 0 end end I know in C/C++/Java/C#, we cant return a value in a constructor. But I'm wondering whether it is possible in Ruby. 回答1: In Ruby, what's the relationship between ' new ' and ' initialize '? new typically calls initialize . The default implementation of new is something like: class Class def new(*args, &block) obj = allocate obj.initialize(*args, &block) # actually

What is difference between instantiating an object using new vs. without

霸气de小男生 提交于 2019-12-17 01:35:15
问题 In C++, Aside from dynamic memory allocation, is there a functional difference between the following two lines of code: Time t (12, 0, 0); //t is a Time object Time* t = new Time(12, 0, 0);//t is a pointer to a dynamically allocated Time object I am assuming of course that a Time(int, int, int) ctor has been defined. I also realize that in the second case t will need to be deleted as it was allocated on the heap. Is there any other difference? 回答1: The line: Time t (12, 0, 0); ... allocates a

Why does the use of 'new' cause memory leaks?

只愿长相守 提交于 2019-12-16 19:53:36
问题 I learned C# first, and now I'm starting with C++. As I understand, operator new in C++ is not similar to the one in C#. Can you explain the reason of the memory leak in this sample code? class A { ... }; struct B { ... }; A *object1 = new A(); B object2 = *(new B()); 回答1: What is happening When you write T t; you're creating an object of type T with automatic storage duration . It will get cleaned up automatically when it goes out of scope. When you write new T() you're creating an object of

Why does the use of 'new' cause memory leaks?

只愿长相守 提交于 2019-12-16 19:51:20
问题 I learned C# first, and now I'm starting with C++. As I understand, operator new in C++ is not similar to the one in C#. Can you explain the reason of the memory leak in this sample code? class A { ... }; struct B { ... }; A *object1 = new A(); B object2 = *(new B()); 回答1: What is happening When you write T t; you're creating an object of type T with automatic storage duration . It will get cleaned up automatically when it goes out of scope. When you write new T() you're creating an object of

What is the meaning of the below sentence in c++ [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-14 04:16:53
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: C++'s “placement new” in the below code what does Line 3 represents, is it the way of typecasting? or what void someCode() { char memory[sizeof(Fred)]; // Line #1 void* place = memory; // Line #2 Fred* f = new(place) Fred(); // Line #3 // The pointers f and place will be equal ... } 回答1: This is a typical usage of Placement new . It allows you to allocate memory and then construct objects at that particular

How do I know if a pointer has been assigned data via 'new'?

丶灬走出姿态 提交于 2019-12-14 03:15:25
问题 Say I have a pointer like this: int *thingy; At some point, this code may or may not be called: thingy=new int; How do I know if I can do this: delete thingy; I could use a bool for every pointer and mark the bool as true whenever the I use new , but I have many pointers and that would get very unwieldy. If I have not called new on thingy , calling delete on it would likely cause a crash, right? I searched around quite a bit but could find no answer that clearly fit my situation. EDIT: I need

New and delete command of c++ in obj-c

穿精又带淫゛_ 提交于 2019-12-13 22:37:34
问题 I have an NSMutablearray of objects. the number of objects is set by user. in c++ I would use a for cycle and the 'new' command.something like this: int fromuser, a; for(a=0;a<fromuser;a++){ array addobject:(new class obj) } what do I need to do in obj c since there is no new? 回答1: You would utilize the alloc and init (or more specialized initializer) provided by NSObject. For example, something like the following should work: int fromuser, a; NSMutableArray objectArray = [[NSMutableArray