allocation

Declare slice or make slice?

天大地大妈咪最大 提交于 2019-12-18 10:01:52
问题 In Golang, what is the difference between var s []int and s := make([]int, 0) ? I find that both works, but which one is better? 回答1: In addition to fabriziom's answer, you can see more examples at "Go Slices: usage and internals", where a use for []int is mentioned: Since the zero value of a slice ( nil ) acts like a zero-length slice , you can declare a slice variable and then append to it in a loop: // Filter returns a new slice holding only // the elements of s that satisfy f() func

type requirements for std::vector<type>

拜拜、爱过 提交于 2019-12-18 09:06:40
问题 I am still confused about the requirements for a type to be used with a std::vector in C++11, but this may be caused by a buggy compiler (gcc 4.7.0). This code: struct A { A() : X(0) { std::cerr<<" A::A(); this="<<this<<'\n'; } int X; }; int main() { std::vector<A> a; a.resize(4); } works fine and produces the expected output, indicating that the default ctor (explicitly given) is called (and not an implicit copy ctor). However, if I add a deleted copy ctor to the class, viz struct A { A() :

Does std::array of std::array have contiguous memory?

天大地大妈咪最大 提交于 2019-12-18 04:47:07
问题 Seems, that I found how to easily get normal 2D Array with contiguous memory in 2 lines of code: template<int N, int M> using Array2D = array<array<int, M>, N>; Let's solve easy task of swapping min and max in Array2D (a little of c++17): template<int N, int M> void printArray2D(const Array2D<N, M> &arr); int main() { const int N = 5; const int M = 5; Array2D<N, M> arr; // random init of Array2D generate(arr.front().begin(), arr.back().end(), []()->int { return rand() % 100; }); printArray2D

Boost Pool experience requested. Is it useful as allocator with preallocation?

被刻印的时光 ゝ 提交于 2019-12-17 20:14:54
问题 Recently i have been looking for a pool/allocator mechanism. Boost Pool seems to provide the solution, but there is still things, which it have not been able to deduce from the documentation. What need to be allocated Several small classes (~30 chars) std::map (i want to ensure it do not perform dynamic allocator by itself) allocation within pugi::xml std::strings How to control of address space for allocation (or just amount) The object_pool seem the to provide a good way for allocating need

Linux Allocator Does Not Release Small Chunks of Memory

我的未来我决定 提交于 2019-12-17 05:04:46
问题 The Linux glibc allocator seems to be behaving weirdly. Hopefully, someone can shed some light on this. Here is the source file that I have: first.cpp: #include <unistd.h> #include <stdlib.h> #include <list> #include <vector> int main() { std::list<char*> ptrs; for(size_t i = 0; i < 50000; ++i) { ptrs.push_back( new char[1024] ); } for(size_t i = 0; i < 50000; ++i) { delete[] ptrs.back(); ptrs.pop_back(); } ptrs.clear(); sleep(100); return 0; } second.cpp: #include <unistd.h> #include <stdlib

Optimizing data types to increase speed

淺唱寂寞╮ 提交于 2019-12-14 02:38:55
问题 In order to write efficient code, you should use the simplest possible data types. This is even more true for Renderscript, where the same calculation is repeated so many times in a kernel. Now, I wanted to write a very simple kernel, that takes an (color) bitmap as input and produces an int[] array as output : #pragma version(1) #pragma rs java_package_name(com.example.xxx) #pragma rs_fp_relaxed uint __attribute__((kernel)) grauInt(uchar4 in) { uint gr= (uint) (0.21*in.r + 0.72*in.g + 0.07

Pinned memory OpenCL, has anybody successfully used it?

南笙酒味 提交于 2019-12-14 01:28:49
问题 I used the CL_MEM_ALLOC_HOST_PTR flag with my clCreateBuffer calls, but the Compute Profiler shows all my "host mem transfer type" as being Pageable. I tried it in two different kernel setups, but the profiler wouldn't show that I was using pinned memory. Is it just really random when a kernel gets to use pinned memory? Is it constrained by something? I am guessing the size of the buffer matters. I tried one buffer of a size of 10,000 floats and I still got Pageable memory. Let me know what

Array of Structs memory realloc stderr

醉酒当歌 提交于 2019-12-13 21:08:08
问题 I am trying to realloc an array of structs when it runs out of space as I'm putting elements into it but I keep receiving a realloc stderr. The array of struct will eventually have 235,000 elements in it. When I set the initial start size to 100,000 I receive the stderr when trying to realloc. If I se the initial start size to 300,000 it does not give the error because it never reaches the realloc statement. #define _XOPEN_SOURCE #include <stdlib.h> #include <stdio.h> #include <assert.h>

road map from location A to location B by latitude and longitude

夙愿已清 提交于 2019-12-13 10:22:49
问题 I am doing work with map and location. I want to have road map from one location to another location. I am providing latitude and longitude of both the location(destination and source). So please help me to have this map. I read about Google map api but I didn't find perfect answer. 回答1: Use below URL of google map API to find the road map route between two locations http://maps.google.com/?saddr=STARTINGADDRESS&daddr=DESTINATIONADDRESS 回答2: First you have to take the directions from Google.

Multi-dimensional Array Allocation

耗尽温柔 提交于 2019-12-13 09:05:37
问题 I am almost a beginner in C and I want to allocate a 2 dimensional array, change it, reallocate it and print it. Both of the answers with the code were useful.Now the code is: main() { int i, j, L , **lp ; scanf("%i" , &L ); lp = calloc(L , sizeof(*lp) ); for(i=0 ; i<L ; i++) lp[i] = calloc( L , sizeof( *(lp[i])) ); for(i=0 ; i<L ; i++) { for(j=0 ; j<L ; j++ ) { lp[i][j]=0; printf("%i\t" , lp[i][j] ); } printf("\n"); } free( lp ); return(0); } 回答1: A lot of things are wrong. 1. The for loop {