redim an array

  • Thread starter Stephen.Schoenberger
  • Start date
S

Stephen.Schoenberger

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

Malcolm McLean

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

James Fang

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

James Fang

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?

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

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

Richard Heathfield

James Fang said:

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

James Fang

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
 
S

santosh

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.

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

pete

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,832
Latest member
UtaHetrick

Latest Threads

Top