dynamic-arrays

When writing a large array directly to disk in MATLAB, is there any need to preallocate?

﹥>﹥吖頭↗ 提交于 2019-12-05 00:08:45
I need to write an array that is too large to fit into memory to a .mat binary file. This can be accomplished with the matfile function, which allows random access to a .mat file on disk. Normally, the accepted advice is to preallocate arrays, because expanding them on every iteration of a loop is slow. However, when I was asking how to do this , it occurred to me that this may not be good advice when writing to disk rather than RAM. Will the same performance hit from growing the array apply , and if so, will it be significant when compared to the time it takes to write to disk anyway? (Assume

Is a dynamic array automatically deallocated when it goes out of scope?

倖福魔咒の 提交于 2019-12-04 23:02:37
in this example procedure foobar; var tab:array of integer; begin setlength(tab,10); end; is the array destroyed or the memory is leaking? The memory is freed. (That is, no memory leak!) The array is automatically freed, but I've seen obscure cases where it isn't for some reason. I solved it by setting the array to nil. 来源: https://stackoverflow.com/questions/3113296/is-a-dynamic-array-automatically-deallocated-when-it-goes-out-of-scope

C program, pointer argument won't hold values

核能气质少年 提交于 2019-12-04 18:48:35
Hi guys I'm sorry to bother you with this but I'm starting to loose it here.. I have recently started programming in C again and I have run into some kind bug that just I can't figure out.. My C program is(should be) an easy one, so it needs to do the following: An undefined number of natural elements is read form the keyboard until a 0 is read. After that it has to compute the product of all elements and compute the number of 0-s on the end of that result.. int input(int* v) { int n = 0; do { n = n + 1; v = (int*) realloc(v,n*sizeof(int)); printf("Enter number %d: ",n); scanf("%d",&v[n-1]); }

Removing elements from dynamic arrays

孤街浪徒 提交于 2019-12-04 10:13:03
So, I have this: #include <stdio.h> #include <stdlib.h> #include <string.h> void remove_element(int* array, int sizeOfArray, int indexToRemove) { int* temp = malloc((sizeOfArray - 1) * sizeof(int*)); // allocate an array with a size 1 less than the current one memcpy(temp, array, indexToRemove - 1); // copy everything BEFORE the index memcpy(temp+(indexToRemove * sizeof(int*)), temp+((indexToRemove+1) * sizeof(int*)), sizeOfArray - indexToRemove); // copy everything AFTER the index free (array); array = temp; } int main() { int howMany = 20; int* test = malloc(howMany * sizeof(int*)); for (int

How to initialize a dynamic array in java?

怎甘沉沦 提交于 2019-12-04 03:02:18
If I have a class that needs to return an array of strings of variable dimension (and that dimension could only be determined upon running some method of the class), how do I declare the dynamic array in my class' constructor? If the question wasn't clear enough, in php we could simply declare an array of strings as $my_string_array = array(); and add elements to it by $my_string_array[] = "New value"; What is the above code equivalent then in java? You will want to look into the java.util package , specifically the ArrayList class. It has methods such as .add() .remove() .indexof() .contains(

How to “watch” a C++ dynamic array using gdb?

蓝咒 提交于 2019-12-03 11:09:27
Consider the following example: int size = 10, *kk = new int[size]; for (int i = 0; i < size; i++) { kk[i] = i; } delete [] kk; How can I add a watch for the whole array? I can add a watch one by one ( kk[0] , kk[1] ...), but since I know the array's length is there a way to do it automatically? I mean something like kk[0..size-1] or so. I'm using NetBeans IDE together with cygwin g++ and gdb. Try display *kk@<size> From the doc for the print command: @ is a binary operator for treating consecutive data objects anywhere in memory as an array. FOO@NUM gives an array whose first element is FOO,

Is it safe to type-cast TArray<X> to array of X?

半城伤御伤魂 提交于 2019-12-03 10:17:57
Today I discovered a compiler bug ( QC#108577 ). The following program fails to compile: program Project1; {$APPTYPE CONSOLE} procedure P(M: TArray<TArray<Integer>>); begin SetLength(M, 1, 2); end; begin end. The compiler gags on the SetLength line and says: [dcc32 Error] E2029 ')' expected but ',' found I know I could fix it like this: procedure P(M: TArray<TArray<Integer>>); var i: Integer; begin SetLength(M, 1); for i := low(M) to high(M) do SetLength(M[i], 2); end; but naturally I'm keen to avoid having to resort to this. The following variant compiles and seems to work: procedure P(M:

How can I change the length of an array? [duplicate]

我的未来我决定 提交于 2019-12-02 23:09:37
问题 This question already has answers here : Java dynamic array sizes? (18 answers) Closed 4 years ago . So I am assigned with a project where I have an array and as the user puts elements into this array it will have to double in length once it gets full. We are not permitted to use array lists or anything in the collections interface. What I was trying to do was to make a new array once the old one was full, and then I would copy the values over to the new array. The problem is I don't know how

how to allocate memory dynamically for a two dimensional array [duplicate]

筅森魡賤 提交于 2019-12-02 22:36:15
问题 This question already has answers here : How do I correctly set up, access, and free a multidimensional array in C? (5 answers) Closed 4 years ago . I had recently been for a interview and they had asked me to write a programe to allocate memory dynamically for a two dimensional array (i=3 and j=2) 回答1: int *a = (int*)malloc(i*j*sizeof(int)); You can get a[k][l] with a[k*i+l]; 回答2: Some of you will hate this, but it is a favorite of mine. The main advantage is that it can be de-allocated

Returning Vectors standard in C++

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 22:22:35
问题 Now, I know this is a common question, but I haven't been able to really find a straight answer on this. This is really a question about standards. I am working on a project involving the genetic algorithm. But I'm running into a bottleneck when it comes to returning a vector. Is there a "proper" way to do this. Normally I use dynamically allocated arrays, and return a pointer to a newly created array. obj* func(obj* foo); That way, everything is efficient and there is no copying of data. Is