p_sriniv
asked on
Array size
Hi,
I have an array like long *x for which memory is allocated thru malloc. Depending on requirement the size will be increased using realloc. At some later stage I need to check if a particular number is present in the array. How can I get the number of elements present in the array at any given point of time.
I have an array like long *x for which memory is allocated thru malloc. Depending on requirement the size will be increased using realloc. At some later stage I need to check if a particular number is present in the array. How can I get the number of elements present in the array at any given point of time.
You can't. You have to store that in a separate variable and update it when you malloc/realloc.
Hi p_sriniv,
There is no way to get the number of elements in the array unless you explicitly store it somewhere ... How about using first location on the array as number of elements ?
Sunnycoder
There is no way to get the number of elements in the array unless you explicitly store it somewhere ... How about using first location on the array as number of elements ?
Sunnycoder
Hi p_sriniv,
Probably you already have the size somewhere already, since you do realloc.
Here is a small code snippet which does realloc with a slop factor of 2, used to store input lines of arbitrary size:
static char* tokbuf=NULL;
static size_t maxtokbuf=0;
static size_t curtokbuf=0;
if(!tokbuf){
tokbuf = (char*)malloc(100);
maxtokbuf = 100;
}
if(curtokbuf == maxtokbuf){
maxtokbuf *= 2;
tokbuf=(char*)realloc(tokb uf,maxtokb uf);
}
tokbuf[curtokbuf++]=tok;
You could easily wrap this in a declaration/use macro pair.
Cheers,
Stefan
Probably you already have the size somewhere already, since you do realloc.
Here is a small code snippet which does realloc with a slop factor of 2, used to store input lines of arbitrary size:
static char* tokbuf=NULL;
static size_t maxtokbuf=0;
static size_t curtokbuf=0;
if(!tokbuf){
tokbuf = (char*)malloc(100);
maxtokbuf = 100;
}
if(curtokbuf == maxtokbuf){
maxtokbuf *= 2;
tokbuf=(char*)realloc(tokb
}
tokbuf[curtokbuf++]=tok;
You could easily wrap this in a declaration/use macro pair.
Cheers,
Stefan
You could use wrappers for malloc/realloc and update the value of a size variable to store the size.
Sunny's idea of storing the size in the first element is a good idea.
Sunny's idea of storing the size in the first element is a good idea.
ankuratvb,
> storing the size in the first element
Pascal strings? :-)
Stefan
> storing the size in the first element
Pascal strings? :-)
Stefan
Stefan,
Never Studied Pascal. :~))
My first language was good old BASIC.
Never Studied Pascal. :~))
My first language was good old BASIC.
Here is an example of the wrappers:
#include<alloc.h>
int size=0;
void *mymalloc(size_t s);
void *realloc(void *block, size_t size);
int main()
{
int *x;
x=(int *)mymalloc(100);
printf("%d",size);
return 0;
}
void *mymalloc(size_t s)
{
size=s;
return malloc(size);
}
void *realloc(void *block, size_t s)
{
size=s;
return realloc(block,size);
}
#include<alloc.h>
int size=0;
void *mymalloc(size_t s);
void *realloc(void *block, size_t size);
int main()
{
int *x;
x=(int *)mymalloc(100);
printf("%d",size);
return 0;
}
void *mymalloc(size_t s)
{
size=s;
return malloc(size);
}
void *realloc(void *block, size_t s)
{
size=s;
return realloc(block,size);
}
>void *realloc(void *block, size_t size);
should be:
void *myrealloc(void *block, size_t size);
in both declaration and definition.
should be:
void *myrealloc(void *block, size_t size);
in both declaration and definition.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can keep the array size in the [-1]st array index, with a little footwork:
int * myalloc( int Len )
{
p = malloc( ++Len * sizeof(int) );
*p = Len;
return( p++ );
}
void myfree( int * p )
{
free( --p );
}
int * myalloc( int Len )
{
p = malloc( ++Len * sizeof(int) );
*p = Len;
return( p++ );
}
void myfree( int * p )
{
free( --p );
}
The return from myalloc isn't correct, should be
return ++p;
Beware: the array cannot be longer than 32767 bytes...
return ++p;
Beware: the array cannot be longer than 32767 bytes...
Just out of curiosity,
What would be the result of return p++;
coz by the time the next sequence point occurs,p would be out of scope.
Or does ( ) define a sequence point?
What would be the result of return p++;
coz by the time the next sequence point occurs,p would be out of scope.
Or does ( ) define a sequence point?
It is even better to
return p+1
return p+1
Hi Kent,
I too personally use the same struct as you do, though I do not use such descriptive names (mine are i,j and p ... I am still learning :) ) ... However, here question said array of longs ... So it gets a bit more compact (or atleast I feel so) ... But that the struct approach is more elegant still stands
>using index 0 as the size alters the characteristics of the array
May be you have programmed too long with C :o) ... Its all numbers!! ... one or one more does not matter unless you are stuck with one as a matter of habit ;o)
sunny
I too personally use the same struct as you do, though I do not use such descriptive names (mine are i,j and p ... I am still learning :) ) ... However, here question said array of longs ... So it gets a bit more compact (or atleast I feel so) ... But that the struct approach is more elegant still stands
>using index 0 as the size alters the characteristics of the array
May be you have programmed too long with C :o) ... Its all numbers!! ... one or one more does not matter unless you are stuck with one as a matter of habit ;o)
sunny
Hi Sunny,
Actually, I use the structure here:
#include "storage.h"
typedef struct
{
UInt32 entries; /* Number of entries in use */
UInt32 reserved; /* Number of entries in the reserved memory */
UInt16 increment; /* Number of items to add each extent */
UInt16 entry_size;
boolean entries_are_pointers;
char *buffer;
} TABLE_t;
storage.h contains the machine dependent types so that the structure is a consistent size. I have an entire library that manages the data by managing this structure. A long time ago I got tired of debugging pointer errors so I built the library. Almost every C program that I've written in the last 10+ years uses it for something.
>>using index 0 as the size alters the characteristics of the array
>May be you have programmed too long with C :o) ... Its all numbers!! ... one or one more does not matter unless you are stuck with one as a matter of habit ;o)
Reread my entire comment. Using index 0 as anything other than the first data element alters the way that you index the array. Debugging aquires its own set of complications.
Kent
Actually, I use the structure here:
#include "storage.h"
typedef struct
{
UInt32 entries; /* Number of entries in use */
UInt32 reserved; /* Number of entries in the reserved memory */
UInt16 increment; /* Number of items to add each extent */
UInt16 entry_size;
boolean entries_are_pointers;
char *buffer;
} TABLE_t;
storage.h contains the machine dependent types so that the structure is a consistent size. I have an entire library that manages the data by managing this structure. A long time ago I got tired of debugging pointer errors so I built the library. Almost every C program that I've written in the last 10+ years uses it for something.
>>using index 0 as the size alters the characteristics of the array
>May be you have programmed too long with C :o) ... Its all numbers!! ... one or one more does not matter unless you are stuck with one as a matter of habit ;o)
Reread my entire comment. Using index 0 as anything other than the first data element alters the way that you index the array. Debugging aquires its own set of complications.
Kent
>Debugging aquires its own set of complications.
Because we have been trained to think that way ... "0 is the first location for data"
My struct is a bit different ... I uniformly use void *s and keep track of capacity and used locations only ... dirty but convinient
Because we have been trained to think that way ... "0 is the first location for data"
My struct is a bit different ... I uniformly use void *s and keep track of capacity and used locations only ... dirty but convinient
I should use void* for buffer, but pointer math requires a length of the object. char* simply makes life easier. I recast before returning data addresses so the returned address is of the expected type.
Kent