Archived

This topic is now archived and is closed to further replies.

Daishim

C/C++ Arrays

Recommended Posts

In VB you could declare an array like so
   Dim intArray(1 to 4) As Integer   
Can this be done in C/C++ so that keeping track of things (especially objects) would be easier by starting with 1?

Share this post


Link to post
Share on other sites
No ... although if you really want your arrays to start at one, I suppose you could just make them one bigger, and not use the 0th item. I don''t think that''s a good idea, though.

~~~~~~~~~~
Martee

Share this post


Link to post
Share on other sites
  
#include <stdio.h>

int main(int argc, char *argv[])
{
int* intarray = new int[4];
--intarray;

for (int x=1; x<5; ++x)
{
intarray[x] = x*x;
}

printf("Array[1] %d\n", intarray[1]);
printf("Array[2] %d\n", intarray[2]);
printf("Array[3] %d\n", intarray[3]);
printf("Array[4] %d\n", intarray[4]);

return 0;
}

Not at all recommended, by the way Bad programming practice, really. This example was just provided to further understanding of the language

Share this post


Link to post
Share on other sites
You could always create a template array class and overload the [] operator to whatever you wanted it to. You could also include bounds checking for safety....


  
// Template array class. Note... this was written off the top of

// my head, may require debuggering.


template <class T>
class CArray
{
public:
CArray(int size)
{
data = new T[size];
maxItems = size-1;
}
~CArray()
{
delete[] data;
}
T& operator[] (int index)
{
//index--; // uncomment this line to make the array 1 based

if(index > maxItems)
return data[maxItems];
elseif (index < 0)
return data[0];
else
return data[index];
}
private:
T* data;
int maxItems;
};



Then you can instantiate an array of any type like this...

  
CArray<int> myIntegerArray;
CArray<char> myCharArray;
CArray<float> myFloatArray;


etc...

Share this post


Link to post
Share on other sites
Is it possible to redimension an array like VB''s ReDim statement? Or must I manually store the data temporarily and delete and remake the array to what is needed?

Share this post


Link to post
Share on other sites
1. You cannot resize a normal array at all. Ever.
2. You cant resize a dynamically allocated array* but you can make a new array and copy the contents across, then destroy the old one.
3. If you use something like the template class I wrote above, you could include methods that take care of this array copying resizing stuff internally.
4. Ideally, your arrays should be big enough to fit what you need to put in them. If your array is likely to change size a lot, and is likely to get very large, you may be better off using a linked list.

*ignoring functions like realloc() which are dangerous, and they just do the samething anyway under the hood.

Share this post


Link to post
Share on other sites
Sandman:

How would i go about creating the same tamplate for a 2d array?

i need a place to store outside inputs and as you anderstand there can be any number of inputs so i can not limit the array size.
I''m just not sure how to overload the second []!

Thanks

Share this post


Link to post
Share on other sites
quote:
Original post by Daishi
Is it possible to redimension an array like VB''s ReDim statement? Or must I manually store the data temporarily and delete and remake the array to what is needed?


use a std::vector

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...

Share this post


Link to post
Share on other sites
quote:
Original post by Daishi
Is it possible to redimension an array like VB''s ReDim statement? Or must I manually store the data temporarily and delete and remake the array to what is needed?
you can use realloc(), or better yet, std::vector

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
sadly saying it once is not enough: use std::vector

Share this post


Link to post
Share on other sites
quote:
Original post by Kylotan
Not at all recommended, by the way Bad programming practice, really.

Not only that, but it''s illegal (undefined behaviour). An arithmetic expression taking a pointer as operand is not allowed to yield a pointer that points anywhere other than within the array or one past the end.


[C++ FAQ Lite | ACCU | Boost | Learning C++]

Share this post


Link to post
Share on other sites
You sure about that Sabreman? Because in order to use any of the algorithmsin "Numerical Recipies", I always pass in pointers - 1 (since the algs are all 1-based instead of 0-based).

Share this post


Link to post
Share on other sites
The C99 Standard says this:

quote:
6.5.6 Additive operators
If both the pointer operand and the result point to elements of teh same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise the behaviour is undefined.

In the general case, it''s undefined behaviour to perform pointer arithmetic outside the bounds of a single contiguous block of memory, since C (or C++) cannot make guarantees about that memory. That''s not to say it doesn''t give you the desired behaviour.

I can''t comment on "Numerical Recipes" as I don''t have it.


[C++ FAQ Lite | ACCU | Boost | Stroustrup on Learning C++]

Share this post


Link to post
Share on other sites
You never access the memory outside the allocated block. All you are doing is modify the base address you use for the array. So long as you add the proper offset to compensate for it, you stay within the legal block. Given that integral overflows are (usually) silent, substracting one then adding one back in ( ptr[k] == *(ptr+k) )is a no-op. There is no issue of block contiguity here.

Thus (ptr-1)[k+1] == *(ptr-1+k+1) == *(ptr+k) which is the desired result.


[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI's STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...


[edited by - Fruny on April 18, 2002 3:06:20 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Daishi
Can [one-based array offset computation] be done in C/C++ so that keeping track of things (especially objects) would be easier by starting with 1?

Who said it''s easier? It''s only confusing for a minute; hop to it and you''ll soon find you like it a lot. You might even start to get mad at VB for being one-based.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites