472,803 Members | 1,022 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,803 software developers and data experts.

redim an array

I have declared a bunch of arrays and now I need to change the
dimensions on some of the arrays. For instance

int size = 1000;
int array1[size];

array1 is then filled with some values

now array1 dimensions need to be changed to size by 2 (2 columns) but
the orginial data needs to be retained, array just resized/
redimensioned. In some instances the dimensions need to be changed to
3.

Can this be done without completely recreating the array/deleting the
old one?

Thanks.
Nov 26 '07 #1
7 5310

<St******************@gmail.comwrote in message
>I have declared a bunch of arrays and now I need to change the
dimensions on some of the arrays. For instance

int size = 1000;
int array1[size];

array1 is then filled with some values

now array1 dimensions need to be changed to size by 2 (2 columns) but
the orginial data needs to be retained, array just resized/
redimensioned. In some instances the dimensions need to be changed to
3.

Can this be done without completely recreating the array/deleting the
old one?
Yes and no.
If you make the array flat

int *array1 = malloc(size * sizeof(int));

you can manage as many dimensions as you like e.g

array[y*size +x];
would access the the x,yth element of a size by N array.

realloc() will resize the array for you. You can add extra rows but not,
easily, extra columns.
However realloc() will almost certainly do internal copying.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Nov 26 '07 #2
On 11ÔÂ27ÈÕ, ÉÏÎç4ʱ51·Ö, Stephen.Schoenber...@gmail..com wrote:
I have declared a bunch of arrays and now I need to change the
dimensions on some of the arrays. For instance

int size = 1000;
int array1[size];

array1 is then filled with some values

now array1 dimensions need to be changed to size by 2 (2 columns) but
the orginial data needs to be retained, array just resized/
redimensioned. In some instances the dimensions need to be changed to
3.

Can this be done without completely recreating the array/deleting the
old one?

Thanks.
================================================== =================================================

Hi Stephen,

You cannot do direct convertion from one dimension to two.
See the below example:

int * array1 = new int[100];
int array3[10][10];
memset(array1,0,sizeof(int)*100);
int **array2 = (int **)array1;
array2 = (int**)array3;
array3[1][1]=10;
array2[1][1]=10;

You can see the assembly in VC05 compiler:
array3[1][1]=10;
00413BD2 mov dword ptr [ebp-174h],0Ah
array2[1][1]=10;
00413BDC mov eax,dword ptr [array2]
00413BE2 mov ecx,dword ptr [eax+4]
00413BE5 mov dword ptr [ecx+4],0Ah

The compiler will directly translate the two dimension array into
the single dimension array at compiling time. And for the pointer to
pointer array(**array2), if you treat it as a two dimension array, you
should manually copy the address of the 10 rows into the first 10
element of the array2[10][10].


Nov 27 '07 #3
On 11ÔÂ27ÈÕ, ÏÂÎç5ʱ19·Ö, James Fang <fangshang...@gmail.comwrote:
On 11ÔÂ27ÈÕ, ÉÏÎç4ʱ51·Ö, Stephen.Schoenber...@gmail.com wrote:
I have declared a bunch of arrays and now I need to change the
dimensions on some of the arrays. For instance
int size = 1000;
int array1[size];
array1 is then filled with some values
now array1 dimensions need to be changed to size by 2 (2 columns) but
the orginial data needs to be retained, array just resized/
redimensioned. In some instances the dimensions need to be changed to
3.
Can this be done without completely recreating the array/deleting the
old one?
Thanks.

================================================== =================================================

Hi Stephen,

You cannot do direct convertion from one dimension to two.
See the below example:

int * array1 = new int[100];
int array3[10][10];
memset(array1,0,sizeof(int)*100);
int **array2 = (int **)array1;
array2 = (int**)array3;
array3[1][1]=10;
array2[1][1]=10;

You can see the assembly in VC05 compiler:
array3[1][1]=10;
00413BD2 mov dword ptr [ebp-174h],0Ah
array2[1][1]=10;
00413BDC mov eax,dword ptr [array2]
00413BE2 mov ecx,dword ptr [eax+4]
00413BE5 mov dword ptr [ecx+4],0Ah

The compiler will directly translate the two dimension array into
the single dimension array at compiling time. And for the pointer to
pointer array(**array2), if you treat it as a two dimension array, you
should manually copy the address of the 10 rows into the first 10
element of the array2[10][10].
================================================== ==========================================
Forgot to mention that, the above example will introduce an runtime
memory error, because the first 10 elements in array2[10][10] is not
properly initialized.
Nov 27 '07 #4
James Fang said:

<snip>
>
You cannot do direct convertion from one dimension to two.
See the below example:

int * array1 = new int[100];
This is not legal C. I think you meant:

int *array1 = malloc(100 * sizeof *array1);
if(array1 != NULL)
{

etc.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 27 '07 #5
On 11ÔÂ27ÈÕ, ÏÂÎç5ʱ27·Ö, Richard Heathfield <r...@see.sig.invalidwrote:
James Fang said:

<snip>
You cannot do direct convertion from one dimension to two.
See the below example:
int * array1 = new int[100];

This is not legal C. I think you meant:

int *array1 = malloc(100 * sizeof *array1);
if(array1 != NULL)
{

etc.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
================================================== =========================================
Hi Richard,

I rewrote the code in pure C and compiled it in the stardard ms
compiler. The same results was introduced.

BRs
James
Nov 27 '07 #6
In article
<26**********************************@b40g2000prf. googlegroups.com>,
James Fang <fa**********@gmail.comwrote on Tuesday 27 Nov 2007 3:07
pm:
On 11?27?, ??5?27?, Richard Heathfield <r...@see.sig.invalidwrote:
>James Fang said:

<snip>
You cannot do direct convertion from one dimension to two.
See the below example:
int * array1 = new int[100];

This is not legal C. I think you meant:

int *array1 = malloc(100 * sizeof *array1);
if(array1 != NULL)
{

etc.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
The text folowing a '-- ' character sequence is a "signature" which you
should prefferably remove before posting, unless you are specifically
commenting on them.

<snip>
Hi Richard,

I rewrote the code in pure C and compiled it in the stardard ms
compiler. The same results was introduced.
Please show us the exact code. Copy and Paste, not retype. Note that
almost all implementations need specific command line options to
compile under strict conformance to the Standard. Their default
behaviour is to compile a non-Standard "C with implementation specific
extras" language.

For MS Visual C++ use the '/Za' option for C90 Standard conformance.

Nov 27 '07 #7
St******************@gmail.com wrote:
>
I have declared a bunch of arrays and now I need to change the
dimensions on some of the arrays. For instance

int size = 1000;
int array1[size];

array1 is then filled with some values

now array1 dimensions need to be changed to size by 2 (2 columns) but
the orginial data needs to be retained, array just resized/
redimensioned. In some instances the dimensions need to be changed to
3.

Can this be done without completely recreating the array/deleting the
old one?
In cases where arrays may be growing,
it's possible that a linked list may do the job better
until the data stops coming in,
at which time an array may be created from the list,
if it's important to have an array.

--
pete
Nov 28 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Wayne Wengert | last post by:
I am trying to add one column to an existing array (code below). The ReDim command gives the error: ----------------------------------------------- Microsoft VBScript runtime error '800a0009' ...
2
by: | last post by:
Is it correct to think that after reducing the populated array's size from say, 10 to 5 with redim preserve myArray(i) an attempt to access an element above the fifth does not cause a...
5
by: Zenobia | last post by:
Hello, I want to keep a list references to database records being accessed. I will do this by storing the record keys in a list. The list must not contain duplicate keys. So I check the...
9
by: John A Grandy | last post by:
In VB6 you could get away with the following code: Dim Index As Integer Dim ItemsCount As Integer Dim StringArray() As String Dim StringValue As String '....
5
by: Paul | last post by:
Off the cuff, does anyone know if arraylist is more efficeint at adding items to an array than redim preserve? Paul <begin loop> Dim c As Integer = SomeArray.GetUpperBound(0) + 1 ReDim...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
2
by: Fredrik Strandberg | last post by:
I have not been able to find the solution of this problem anywhere: I am building a class PrivateHelper that provides methods to access private members and invoke private methods, to be used for...
9
by: Anil Gupte | last post by:
I am having a problem using Multidim arrays. I want to create an array which as I understand it is dimensioned as: dim xyz (rows,columns) as String I want to populate it with rows from a...
1
by: Freddy Coal | last post by:
Hi, I don't know how redim an array, My problem whit an example: I define my array Dim Ary as array I put three elements inside my array Ary = Split("one,two,three", ",")
2
by: eBob.com | last post by:
I was changing some code in a multi-threaded application today and noticed that it was not locking where it really needed to be locking. The Sub was already working with an array so I just stuck a...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.