new-operator

Isn't `delete[]` the counterpart to `new[]`?

半世苍凉 提交于 2020-06-01 05:32:26
问题 I'm reading to brush up on C++ knowledge that is almost 2 decades old in order to understand online info on the factory pattern. The final usage context will likely be in a different 3rd generation language (3GL), but because of my past experience, I think it's easier to follow C++ than (say) Java, even though the latter may be less intricate in syntax. A bigger reason, however, is that the only code example I can find of the problem being addressed, i.e., in the absence of the factory

Isn't `delete[]` the counterpart to `new[]`?

隐身守侯 提交于 2020-06-01 05:32:03
问题 I'm reading to brush up on C++ knowledge that is almost 2 decades old in order to understand online info on the factory pattern. The final usage context will likely be in a different 3rd generation language (3GL), but because of my past experience, I think it's easier to follow C++ than (say) Java, even though the latter may be less intricate in syntax. A bigger reason, however, is that the only code example I can find of the problem being addressed, i.e., in the absence of the factory

Why isn't “new” used while making the current variable in the linkedlist?

我是研究僧i 提交于 2020-05-31 04:49:21
问题 This is the solution to printing elements of a linked list. Why isn't it Node *current = new Node; and then current = head; ? void printLinkedList(Node* head) { Node *current = head; while(current!=NULL){ cout << current -> data << endl; current = current -> next; } } 回答1: This is a great spot to draw pictures! Imagine we have a linked list pointed at by head : head | v +------+ +-----+ +-----+ +-----+ | i'm | -> | the | -> | bad | -> | guy | -> null +------+ +-----+ +-----+ +-----+ If we use

C++ reading 16bit binary data from raw img and store them in vector

烈酒焚心 提交于 2020-04-18 06:32:32
问题 These days I am struggling at malloc memory. I try to read 16-bit binary data from raw img and store them in buffer or vector. The total size of these binary data is about 15M. For this kind of size, which way is better between buf or vector? Here is my code below: #include <imebra/imebra.h> #include <stdio.h> #include <iostream> #include <fstream> #include <string.h> #include <vector> #include<iterator> #include <algorithm> #define img_height 2816 #define img_width 2816 //#define img_size

instantiate class from class object

拟墨画扇 提交于 2020-03-18 10:27:30
问题 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' } 回答1: In Java 9 and afterward, if there's a declared zero-parameter ("nullary") constructor, you'd use Class.getDeclaredConstructor() to get it, then call newInstance() on it: Object foo(Class type) throws InstantiationException, IllegalAccessException, InvocationTargetException { return type

C++ new int[0] — will it allocate memory?

。_饼干妹妹 提交于 2020-03-18 08:53:59
问题 A simple test app: cout << new int[0] << endl; outputs: 0x876c0b8 So it looks like it works. What does the standard say about this? Is it always legal to "allocate" empty block of memory? 回答1: From 5.3.4/7 When the value of the expression in a direct-new-declarator is zero, the allocation function is called to allocate an array with no elements. From 3.7.3.1/2 The effect of dereferencing a pointer returned as a request for zero size is undefined. Also Even if the size of the space requested

Interface implements overriding its own methods to create an object of itself as DEFAULT

白昼怎懂夜的黑 提交于 2020-02-04 20:46:09
问题 I've read many threads with questions which looks like questions I am going to ask. However, I can not find satisfactory answers which could be applied to my questions, since there are more than one fold in my questions, seperated in three aspects. This is the interface SubtitleDecoderFactory in https://github.com/google/ExoPlayer/blob/release-v2/library/core/src/main/java/com/google/android/exoplayer2/text/SubtitleDecoderFactory.java As I understand, an interace is abstraction and can not

How to delete object constructed via placement new operator?

▼魔方 西西 提交于 2020-01-27 15:56:02
问题 char * buf = new char[sizeof(T)]; new (buf) T; T * t = (T *)buf; //code... //here I should destruct *t but as it is argument of template and can be //instantiated via basic types as well (say int) so such code /*t->~T();*/ //is incorrect (maybe correct? Strange, but it works on VS 2005 for basic types.) //and this code /*delete t;*/ //crashes the program. delete [] buf; So what is correct way to destruct t ? P.S. The code above is only for describing my problem, and have not real relationship

How to delete object constructed via placement new operator?

陌路散爱 提交于 2020-01-27 15:54:29
问题 char * buf = new char[sizeof(T)]; new (buf) T; T * t = (T *)buf; //code... //here I should destruct *t but as it is argument of template and can be //instantiated via basic types as well (say int) so such code /*t->~T();*/ //is incorrect (maybe correct? Strange, but it works on VS 2005 for basic types.) //and this code /*delete t;*/ //crashes the program. delete [] buf; So what is correct way to destruct t ? P.S. The code above is only for describing my problem, and have not real relationship

How to delete object constructed via placement new operator?

空扰寡人 提交于 2020-01-27 15:54:04
问题 char * buf = new char[sizeof(T)]; new (buf) T; T * t = (T *)buf; //code... //here I should destruct *t but as it is argument of template and can be //instantiated via basic types as well (say int) so such code /*t->~T();*/ //is incorrect (maybe correct? Strange, but it works on VS 2005 for basic types.) //and this code /*delete t;*/ //crashes the program. delete [] buf; So what is correct way to destruct t ? P.S. The code above is only for describing my problem, and have not real relationship