dynamic-arrays

User defined types with dynamic size in C

烂漫一生 提交于 2020-01-24 05:01:25
问题 I want to define a new data type consisting of an array with a size inputted by the user. For example if the user inputs 128, then my program should make a new type which is basically an array of 16 bytes. This structure's definition needs to be global since I am going to use that type thereafter in my program. It is necessary to have a dynamic size for this structure because I will have a HUGE database populated by that type of variables in the end. The code I have right now is: struct user

Copying from One Dynamically Allocated Array to Another C++

亡梦爱人 提交于 2020-01-22 12:41:46
问题 This seems like it should have a super easy solution, but I just can't figure it out. I am simply creating a resized array and trying to copy all the original values over, and then finally deleting the old array to free the memory. void ResizeArray(int *orig, int size) { int *resized = new int[size * 2]; for (int i = 0; i < size; i ++) resized[i] = orig[i]; delete [] orig; orig = resized; } What seems to be happening here is that resized[i] = orig[i] is copying values by reference rather than

Dynamic array allocation in fortran90

半世苍凉 提交于 2020-01-17 06:11:30
问题 I am writing a generic subroutine in fortran90 that will read in a column of data (real values). The subroutine should first check to see that the file exists and can be opened, then it determines the number of elements (Array_Size) in the column by reading the number of lines until end of file. Next the subroutine rewinds the file back to the beginning and reads in the data points and assigns each to an array (Column1(n)) and also determines the largest element in the array (Max_Value). The

Dynamic array add at the end?

浪尽此生 提交于 2020-01-16 08:44:07
问题 Starting with a dynamic array of initially length = 4 and numElements = 0, show the array when we add the followings numbers at the end: 5, 19, 4, 6, -1. The checkpoint (answer) I receive is [5, 19, 4, 6, -1, X, X, X], where X denotes the entries which can be ignored. I have 2 silly questions: I thought insert at the end would make it [X, X, X, 5, 19, 4, 6, -1] instead of what it looks like right now in the answer? I initially though every time we add something in the array, the array would

How to add new element to dynamical array in Fortran90

萝らか妹 提交于 2020-01-13 19:26:44
问题 [SOLVED] by francescalus. Working code for double precision dynamical arrays is: module DynamicalArrays contains subroutine AddToList(list, element) IMPLICIT NONE integer :: i, isize double precision, intent(in) :: element double precision, dimension(:), allocatable, intent(inout) :: list double precision, dimension(:), allocatable :: clist if(allocated(list)) then isize = size(list) allocate(clist(isize+1)) do i=1,isize clist(i) = list(i) end do clist(isize+1) = element deallocate(list) call

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

半腔热情 提交于 2020-01-12 06:53:46
问题 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. 回答1: Try display *kk@<size> From the doc for the print command: @ is a binary operator for treating

3D array C++ using int [] operator

余生颓废 提交于 2020-01-10 18:21:05
问题 I'm new to C/C++ and I've been cracking my head but still got no idea how to make an "structure" like this It's supposed to be a 3D dynamic array using pointers. I started like this, but got stuck there int x=5,y=4,z=3; int ***sec=new int **[x]; It would be enough to know how to make it for a static size of y and z; Please, I'd appreciate that you help me. Thanks in advance. 回答1: To create dynamically 3D array of integers, it's better you understand 1D and 2D array first. 1D array : You can

vb6: redimensioning of 2D dynamic array

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 02:44:06
问题 I am using arrays to store properties of steam according to it's pressure. Right now I have properties of exactly 9 pressures so I'm using static array. I'd like to be more flexible so I'd like to switch to dynamic arrays. When I use ReDim foo(1 to i, 1 to 10) in loop I completely loose all data except last line. When I use ReDim Preserve foo(1 to i, 1 to 10) or ReDim Preserve(i,10) Program throws error of "Runtime error '9': subscript out of range" . i goes from 1 to 9. How can I add line

C++ dynamically allocated array of statically dimensioned arrays

自作多情 提交于 2020-01-02 15:48:03
问题 I need to create a structure that holds a variable number of 'char[2]'s, i.e. static arrays of 2 chars. My question is, how do I allocate memory for x number of char[2]. I tried this (assuming int x is defined): char** m = NULL; m = new char[x][2]; ... delete [] m; (it didn't work) I realise I could use std::vector<char[2]> as a container, but I'm curious as to how it would be done with raw pointers. I am very new to C++ and trying to learn. 回答1: In your code, the type of 'm' doesn't match

C++ dynamically allocated array of statically dimensioned arrays

不羁岁月 提交于 2020-01-02 15:47:30
问题 I need to create a structure that holds a variable number of 'char[2]'s, i.e. static arrays of 2 chars. My question is, how do I allocate memory for x number of char[2]. I tried this (assuming int x is defined): char** m = NULL; m = new char[x][2]; ... delete [] m; (it didn't work) I realise I could use std::vector<char[2]> as a container, but I'm curious as to how it would be done with raw pointers. I am very new to C++ and trying to learn. 回答1: In your code, the type of 'm' doesn't match