allocation

allocating for array and then using constructor

◇◆丶佛笑我妖孽 提交于 2019-12-02 02:20:43
问题 Person.java public class Person { public String firstName, lastName; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFullName() { return(firstName + " " + lastName); } } PersonTest.java public class PersonTest { public static void main(String[] args) { Person[] people = new Person[20]; //this line . for(int i=0; i<people.length; i++) { people[i] = new Person(NameUtils.randomFirstName(), NameUtils.randomLastName()); /

Dynamic Memory Allocation

天大地大妈咪最大 提交于 2019-12-02 00:47:00
I'm having trouble dynamically allocating memory for an array. I've been debugging for hours, any pointers? I posted the rest of the code. It is simply supposed to exchange the swap the first row with the second, and the third with the forth. I am getting strange results like: Enter string: hello Enter string: how are you Enter string: i'm good thanks Enter string: bye Enter string: bai Enter string: xx ========================= how are you !i'm good thanks hello !how are you bye !bai i'm good thanks !bye bai !xx int count = 0; char *lines[MAX_LINES]; char *tmp[50]; printf("Enter string: ");

Are the members of a heap allocated class automatically allocated in the heap?

无人久伴 提交于 2019-12-01 23:11:44
Let's say I have: class A{ public: int x; int y; }; And I allocate an A instance like: A *a = new A(); Does a.x and a.y are also allocated in the heap, since they are 'dependent' of a heap allocated object? Thank you. It is important to understand that C++ uses "copy semantic". This means that variables and structure fields do not contain references to values, but the values themselves. When you declare struct A { int x; double yarr[20]; }; each A you will create will contain an integer and an array of 20 doubles... for example its size in bytes will be sizeof(int)+20*sizeof(double) and

How do I use the Rust memory allocator for a C library that can be provided an allocator?

泪湿孤枕 提交于 2019-12-01 18:06:15
I'm writing Rust bindings to a C library which has the option to use a third-party memory allocator. Its interface looks like this: struct allocator { void*(*alloc)(void *old, uint); void(*free)(void*); }; The corresponding Rust struct is, I guess, the following: #[repr(C)] #[derive(Copy, Clone, Debug, PartialEq)] pub struct Allocator { alloc: Option<extern "C" fn(*mut c_void, c_uint) -> *mut c_void>, free: Option<extern "C" fn(*mut c_void)>, } How can I implement these two extern functions that should mimic the allocator? I did not find anything really looking like the allocator API in Rust

Are there any problems with using jagged-arrays in Fortran with multiple levels of allocation?

旧城冷巷雨未停 提交于 2019-12-01 18:03:46
问题 In my Fortran code, I want to use jagged arrays with multiple levels of allocation. An example code of what I mean is module nonsquare_matrix_mod implicit none type :: nonsquare_matrix integer :: d real*8, dimension(:), allocatable :: vector end type nonsquare_matrix type(nonsquare_matrix),dimension(:),allocatable :: mymatrix end module nonsquare_matrix_mod program nonsquare_matrix_test use nonsquare_matrix_mod implicit none integer, parameter :: max_size=50 integer :: i allocate(mymatrix(max

C++ Size Of Dynamic Memory at Runtime

泪湿孤枕 提交于 2019-12-01 17:48:58
This is something I've been wondering for a while and never found an answer for: Why is it that when you allocate something on the heap you cannot determine the size of it from just the pointer, yet you can delete it using just the pointer and somehow C++ knows how many bytes to free? Does this have something to do with the way it is stored on the heap? Is this information there but not exposed by C++? And perhaps this should be a separate question but I think it's pretty related so I'll ask it here: Why is it a dynamic array of elements must be deleted using delete [] as opposed to just the

The limited allocation size C++

▼魔方 西西 提交于 2019-12-01 07:13:58
问题 I use Visual Studio 2008. I have dynamically declared the variable big_massive: unsigned int *big_massive = new unsigned int[1073741824] But, when I tried to debug this program, I got following error: Invalid allocation size: 4294967295 bytes. I hope there are any path to avoid such error? Thank you! 回答1: That allocation is simply not possible on 32bit x86 systems with sizeof(int)==4 (you are requesting 4GB). A process's total address space is limited to 4GB, and the process itself is usually

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

六眼飞鱼酱① 提交于 2019-11-30 19:12:12
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(arr); auto[a, b] = minmax_element(arr.front().begin(), arr.back().end()); cout << "Swap minimum and

Difference between local allocatable and automatic arrays

痞子三分冷 提交于 2019-11-30 17:40:38
问题 I am interested in the difference between alloc_array and automatic_array in the following extract: subroutine mysub(n) integer, intent(in) :: n integer :: automatic_array(n) integer, allocatable :: alloc_array(:) allocate(alloc_array(n)) ...[code]... I am familiar enough with the basics of allocation (not so much on advanced techniques) to know that allocation allows you to change the size of the array in the middle of the code (as pointed out in this question), but I'm interested in

Why Vector's size() and capacity() is different after push_back()

眉间皱痕 提交于 2019-11-30 15:02:46
I just starting to learning vectors and little confused about size() and capacity() I know little about both of them. But why in this program both are different? even array(10) is making room for 10 elements and initializing with 0. Before adding array.push_back(5) So array.size(); is 10 that is ok. So array.capacity(); is 10 that is ok. After adding array.push_back(5) So array.size(); is 11 that is ok (already 10 time 0 is added and then push_back add one more element 5 ) . So array.capacity(); is 15 Why? ( is it reserving 5 blocks for one int? ) . #include <iostream> #include <vector> int