dynamic-arrays

How to implement dynamic arrays in Delphi

I originally had an array[1..1000] that was defined as a global variable. But now I need that to be n, not 1000 and I don't find out n until later. I know what n is before I fill the array up but I need it to be global therefore need a way to define the size of a global array at run time. Context is filling an array with a linear transf...

Max length for a dynamic array in Delphi?

I was curious how long a dynamic array could be so I tried SetLength(dynArray, High(Int64)); That has a value of 9,223,372,036,854,775,807 and I figure that would be the largest number of indexes I could reference anyway. It gave me a: ERangeError with message 'Range check error'. So I tried: SetLength(dynArray, MaxInt); and...

What is the ideal growth rate for a dynamically allocated array?

C++ has std::vector and Java has ArrayList, and many other languages have their own form of dynamically allocated array. When a dynamic array runs out of space, it gets reallocated into a larger area and the old values are copied into the new array. A question central to the performance of such an array is how fast the array grows in siz...

Problems deleting a 2D dynamic array in C++ (which is eventually store in a vector)

So I have this 2d dynamic array which content I want to free when I am done with it. However I keep running into a heap corruption after the destructor. The code works fine (of course with memory leaks) if I comment out the destructor. (Visual Studio 2005) FrameData::FrameData(int width, int height) { width_ = width; height_ = h...

C++ dynamically allocated array of statically dimensioned arrays

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 co...

C++ Dynamic Array Access Violation

** Sorry for the confusion regarding numCars in the original post. I modified the code to be consistent with the original **** The following academic program is a simplified version of the original problem but it focuses on the issue that I have yet to resolve. There are 2 classes and a main method to this problem and the 2 classes cons...

C++ Array size x86 and for x64

Simple question, I'm writting a program that needs to open huge image files (8kx8k) but I'm a little bit confused on how to initialize the huge arrays to hold the images in c++. I been trying something like this: long long SIZE = 8092*8092; ///8096*8096 double* array; array = (double*) malloc(sizeof(double) * SIZE); if (array == NU...

How to "watch" a C++ dynamic array using gdb?

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]...

C++ - is a pointer to a single value same as a size 1 dynamic array?

I have this snippet of code which I am considering to simplfy: if (numberOfResults > 1) { trackResult_ = new TrackResult[numberOfResults]; for (int i=0; i < numberOfResults; i++) { // Make a deep copy TrackResult tempResult = result[i]; TrackResult * clone = new TrackResult(tempResult); trackResult_[i] = *clone; ...

How to use _spawn or _exec for bootstrapping?

After writing the following program, it does not appear to pass arguments to the called application. While researching _spawnv and what it can do, _execvp was found as what appeared to be a suitable alternative. Does anyone see the problem in the source code and know what needs to be done to fix it? #include <stdio.h> #include <stdlib.h...

What bookkeeping data does a Delphi dynamic array contain?

Here's a simple program to check memory allocation. Checking before and after values with Task Manager suggests that each dynamic array takes up 20 bytes of memory at size = 1. The element size is 4, which means 16 bytes of overhead for bookkeeping data. From looking through system.pas, I can find an array length field at -4 bytes, an...

Java Null Pointer Exception

I attempted to adapt a class I had found on the web for a dynamic array of ints for a dynamic array of "Entities," but now I am getting a "NullPointerException." The code raising the exception is: public void initialize() { buffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB); Entities.put(Entities.getCurrentPos()+1, ...

C# Increasing an array by one element at the end

In my program I have a bunch of growing arrays where a new element is grown one by one to the end of the array. I identified Lists to be a speed bottleneck in a critical part of my program due to their slow access time in comparison with an array - switching to an array increased performance tremendously to an acceptable level. So to gro...

Finding dimensions of a 2D array in C using pointers

I create a 2D array in C as follows: int **arr; arr = malloc(rows * sizeof(int *)); for (i = 0; i < rows; i++) arr[i] = malloc(cols * sizeof(int)); Now, I call: func(arr) In the function func, how do I calculate the row and column dimensions? ...

Cleaning up a dynamic array of Objects in C++

I'm a bit confused about handling an array of objects in C++, as I can't seem to find information about how they are passed around (reference or value) and how they are stored in an array. I would expect an array of objects to be an array of pointers to that object type, but I haven't found this written anywhere. Would they be pointers,...

How to define a static array without a contant size in a constructor of a class? (C++)

I have a class defined as: class Obj { public: int width, height; Obj(int w, int h); } and I need it to contain a static array like so: int presc[width][height]; however, I cannot define within the class, so it it possible to create a pointer to a 2D array (and, out of curiosity, 3, 4, and 5D arrays), have that as a member ...

Why does C++ allow variable length arrays that aren't dynamically allocated?

I'm relatively new to C++, and from the beginning it's been drilled into me that you can't do something like int x; cin >> x; int array[x]; Instead, you must use dynamic memory. However, I recently discovered that the above will compile (though I get a -pedantic warning saying it's forbidden by ISO C++). I know that it's obviously a b...

Access Violation When Writing Dynamic 2D Array... Sometimes

This program is meant to generate a dynamic array, however it gives an access violation error when writing when given certain dimensions. Eg: R = 6, C = 5 crashes, but then R = 5, C = 6 doesn't. In case your wondering, it isn't my homework to "fix" this broken program, this is the method we were taught in class. Also part of my assessmen...

Issue with dynamic array Queue data structure with void pointer

Hi, Maybe there's no way to solve this the way I'd like it but I don't know everything so I better ask... I've implemented a simple Queue with a dynamic array so the user can initialize with whatever number of items it wants. I'm also trying to use a void pointer as to allow any data type, but that's the problem. Here's my code: type...

TStringList, Dynamic Array or Linked List in Delphi?

I have a choice. I have a number of already ordered strings that I need to store and access. It looks like I can choose between using: A TStringList A Dynamic Array of strings, and A Linked List of strings (singly linked) and Alan in his comment suggested I also add to the choices: TList<string> In what circumstances is each of the...