0 registered members (),
1,397
guests, and 7
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: count of array elements
[Re: oliver2s]
#430810
10/02/13 16:51
10/02/13 16:51
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
afaik no.
but if you really need it you can use a struct to wrap the array and store an integer with the index-count inside that struct.
POTATO-MAN saves the day! - Random
|
|
|
Re: count of array elements
[Re: Kartoffel]
#430813
10/02/13 17:09
10/02/13 17:09
|
Joined: Aug 2002
Posts: 3,258 Mainz
oliver2s
OP
Expert
|
OP
Expert
Joined: Aug 2002
Posts: 3,258
Mainz
|
afaik no.
but if you really need it you can use a struct to wrap the array and store an integer with the index-count inside that struct. I already did that, but I have many different structs and each array in each struct have a different size. So I was hoping that there's a general way to find out the element count to save a lot of typing work. A simple solution for var and int arrays and the like obviously is to save the size in the zero index. Keep in mind that you lose one index/ have to allocate one more (and I would save "size-1" in the zero index then). That would be a general solution, but the problem is, most of my arrays are not variable arrays (mostly struct arrays).
|
|
|
Re: count of array elements
[Re: oliver2s]
#430816
10/02/13 17:47
10/02/13 17:47
|
Joined: Apr 2005
Posts: 4,506 Germany
fogman
Expert
|
Expert
Joined: Apr 2005
Posts: 4,506
Germany
|
Last edited by fogman; 10/02/13 17:56.
no science involved
|
|
|
Re: count of array elements
[Re: oliver2s]
#430825
10/02/13 19:49
10/02/13 19:49
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
afaik no.
but if you really need it you can use a struct to wrap the array and store an integer with the index-count inside that struct. I already did that, but I have many different structs and each array in each struct have a different size. So I was hoping that there's a general way to find out the element count to save a lot of typing work. The usual solution for this is to write a generic array container which can automatically updates the storage and does sanity check on passed indices to make sure they stay within the bounds. Let it store void pointers, which are typeless pointers and get automatically promoted to the right type (without explicit cast). If you need scalar types, you have to explicitly cast though, and there is no way around that (besides writing an extra array container for scalar types). Anyhow, you should really consider doing that, because it has the added benefit that you only have a single point of failure: You don't copy and paste code around, and if there is a bug, you can fix all arrays at once. Besides, doing sanity checks on arrays won't hurt and might allow you to catch out of bugs, if you have anyways.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: count of array elements
[Re: WretchedSid]
#430833
10/02/13 21:00
10/02/13 21:00
|
Joined: Dec 2008
Posts: 1,218 Germany
Rackscha
Serious User
|
Serious User
Joined: Dec 2008
Posts: 1,218
Germany
|
Here is a simple TList "class" i wrote some time ago for my needs of lists: List.h
typedef struct TList
{
int count;
int* items;
}TList;
TList* list_create();
void list_free(TList* AList);
void list_add(TList* AList, void* AItem);
void list_remove(TList* AList, int AIndex);
int list_index_of(TList* AList, void* AItem);
void list_delete(TList* AList, void* AItem);
void list_clear(TList* AList);
List.c
TList* list_create()
{
TList* LList = (TList*)sys_malloc(sizeof(TList));
LList.count = 0;
LList.items = NULL;
return(LList);
}
void list_free(TList* AList)
{
if (AList)
{
list_clear(AList);
sys_free(AList);
}
}
void list_add(TList* AList, void* AItem)
{
AList.items = sys_realloc(AList.items, sizeof(int), AList.count, AList.count + 1);
AList.count += 1;
(AList.items)[AList.count - 1] = AItem;
}
void list_remove(TList* AList, int AIndex)
{
int i;
if(clamp(AIndex, 0, AList.count-1) == AIndex)
{
for (i = AIndex; i < AList.count-1; i++)
{
(AList.items)[i] = (AList.items)[i+1];
}
AList.items = sys_realloc(AList.items, sizeof(int), AList.count, AList.count-1);
AList.count -= 1;
}
}
int list_index_of(TList* AList, void* AItem)
{
int i;
int LResult = -1;
for(i = 0; i < AList.count; i++)
{
if (AItem == (AList.items)[i])
{
LResult = i;
break;
}
}
return(LResult);
}
void list_delete(TList* AList, void* AItem)
{
int LIndex = list_index_of(AList,AItem);
if (LIndex >= 0)
{
list_remove(AList,LIndex);
}
}
void list_clear(TList* AList)
{
if(AList.items)
{
sys_free(AList.items);
AList.items = NULL;
AList.count = 0;
}
}
and the declaration of sys_realloc:
int* sys_realloc(void** AIn, int ASize, int AOldCount, int ANewCount)
{
int i = 0;
void** LOut;
LOut = sys_malloc(ASize*ANewCount);
for(i = 0; i < minv(AOldCount, ANewCount); i++)
{
(LOut)[i] = (AIn)[i];
}
sys_free(AIn);
return(LOut);
}
this usually fullfills my needs of lists^^ Greetings Rackscha
MY Website with news of my projects: (for example my current Muliplayer Bomberman, GenesisPrecompiler for LiteC and TileMaster, an easy to use Tile editor) Sparetime-Development
|
|
|
|