468,503 Members | 2,160 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,503 developers. It's quick & easy.

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 4506

<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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Wayne Wengert | last post: by
9 posts views Thread by John A Grandy | last post: by
2 posts views Thread by Fredrik Strandberg | last post: by
9 posts views Thread by Anil Gupte | last post: by
1 post views Thread by Freddy Coal | last post: by
2 posts views Thread by eBob.com | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.