new-operator

Size of array (new[])

和自甴很熟 提交于 2020-01-11 10:58:06
问题 In debug heap I can get size of array, which was created by new[] : int size = *((int *)((char *)ptr - 16)); It is working correctly if the size of the array is less than 28 (but I don't know why? 0_0). Does this trick working in release mode (not using debug heap)? How I can get size of the array (100% working and stable)? 回答1: You are relying on an implementation detail. That's how your particular implementation stores the size of the memory region where the array is placed. As you already

Convention in java - “new” outside of constructor / method?

橙三吉。 提交于 2020-01-10 19:34:23
问题 Simple question. A friend of mind wrote code similar to this one (which is just to explain you my question, it's not useful at all....) class Example{ private int[] tab = new int[10]; public Example() { for(int i = 0 ; i < 10 ; i++) tab[i] = (int)(Math.random()*100); for(int i = 0 ; i < 10 ; i++) System.out.println(tab[i]); } public static void main(String[] arg) { Example ex = new Example(); } } I told him he should put the new inside the constructor class Example{ private int[] tab; public

Convention in java - “new” outside of constructor / method?

扶醉桌前 提交于 2020-01-10 19:33:08
问题 Simple question. A friend of mind wrote code similar to this one (which is just to explain you my question, it's not useful at all....) class Example{ private int[] tab = new int[10]; public Example() { for(int i = 0 ; i < 10 ; i++) tab[i] = (int)(Math.random()*100); for(int i = 0 ; i < 10 ; i++) System.out.println(tab[i]); } public static void main(String[] arg) { Example ex = new Example(); } } I told him he should put the new inside the constructor class Example{ private int[] tab; public

C++ Is constructing object twice using placement new undefined behaviour?

拈花ヽ惹草 提交于 2020-01-09 07:48:30
问题 I have come across some code which has horrified me. Essentially it follows this pattern : class Foo { public: //default constructor Foo(): x(0), ptr(nullptr) { //do nothing } //more interesting constructor Foo( FooInitialiser& init): x(0), ptr(nullptr) { x = init.getX(); ptr = new int; } ~Foo() { delete ptr; } private: int x; int* ptr; }; void someFunction( FooInitialiser initialiser ) { int numFoos = MAGIC_NUMBER; Foo* fooArray = new Foo[numFoos]; //allocate an array of default constructed

C++ Is constructing object twice using placement new undefined behaviour?

南笙酒味 提交于 2020-01-09 07:48:26
问题 I have come across some code which has horrified me. Essentially it follows this pattern : class Foo { public: //default constructor Foo(): x(0), ptr(nullptr) { //do nothing } //more interesting constructor Foo( FooInitialiser& init): x(0), ptr(nullptr) { x = init.getX(); ptr = new int; } ~Foo() { delete ptr; } private: int x; int* ptr; }; void someFunction( FooInitialiser initialiser ) { int numFoos = MAGIC_NUMBER; Foo* fooArray = new Foo[numFoos]; //allocate an array of default constructed

How to open in default browser in C#

醉酒当歌 提交于 2020-01-08 12:01:48
问题 I am designing a small C# application and there is a web browser in it. I currently have all of my defaults on my computer say google chrome is my default browser, yet when I click a link in my application to open in a new window, it opens internet explorer. Is there any way to make these links open in the default browser instead? Or is there something wrong on my computer? My problem is that I have a webbrowser in the application, so say you go to google and type in "stack overflow" and

Eclipse indigo PDT 3.0 gotcha

风格不统一 提交于 2020-01-06 13:51:36
问题 I understand PDT is not a part of Eclipse Indigo. I installed from Help > Install new software and restarted. Still, I do not see PHP as an option in Perspective. Only Java is seen. Can anyone help in getting me on PHP development with Indigo? 回答1: it should really be that simple. download "Eclipse Classic" unzip/install to a new folder location (e.g. c:\eclipse-indigo-win32\ go to help->install new software select Indigo - http://download.eclipse.org/releases/indigo from the "Work with" drop

new ConcurrentHashMap of new ConcurrentHashMap

孤街醉人 提交于 2020-01-06 12:42:13
问题 I'm trying to initialize a ConcurrentHashMap of ConcurrentHashMap s with private final ConcurrentHashMap< String, ConcurrentHashMap<String, Double> > myMulitiConcurrentHashMap = new ConcurrentHashMap< String, new ConcurrentHashMap<String, Double>() >(); but javac gives HashMapper.java:132: error: illegal start of type new ConcurrentHashMap<String, Double>() ^ HashMapper.java:132: error: '(' or '[' expected new ConcurrentHashMap<String, Double>() ^ HashMapper.java:132: error: ';' expected new

Runtime allocation of multidimensional array

别说谁变了你拦得住时间么 提交于 2020-01-06 06:09:41
问题 So far I thought that the following syntax was invalid, int B[ydim][xdim]; But today I tried and it worked! I ran it many times to make sure it did not work by chance, even valgrind didn't report any segfault or memory leak !! I am very surprised. Is it a new feature introduced in g++? I always have used 1D arrays to store matrices by indexing them with correct strides as done with A in the program below. But this new method, as with B, is so simple and elegant that I have always wanted. Is

make_shared with custom new operator

你。 提交于 2020-01-06 02:30:33
问题 This is probably a duplicate, but I cannot find the solution anywhere. I have source code like this: struct Blob{ //... static void *operator new(size_t size_reported, size_t size) { return ::operator new(size); } }; I use it like this: std::shared_ptr<Blob> blob; // ... size_t size = calcSize(); // it returns say 231 Blob *p = new(size) Blob(); blob.reset(p); Can I change the code somehow so I can use std::make_shared or std::allocate_shared so I have single allocation instead of two