Solved

Array size

Posted on 2004-04-21
17
654 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:p_sriniv
  • 5
  • 3
  • 3
  • +3
17 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10877344
You can't. You have to store that in a separate variable and update it when you malloc/realloc.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10877362
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
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10877409
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(tokbuf,maxtokbuf);
}
tokbuf[curtokbuf++]=tok;

You could easily wrap this in a declaration/use macro pair.

Cheers,
Stefan
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10877488
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.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10877508
ankuratvb,
> storing the size in the first element
Pascal strings? :-)

Stefan
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10877556
Stefan,
Never Studied Pascal. :~))

My first language was good old BASIC.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10877670
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);
}

0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10877692
>void *realloc(void *block, size_t size);

should be:

void *myrealloc(void *block, size_t size);
in both declaration and definition.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 45

Accepted Solution

by:
Kdo earned 100 total points
ID: 10877715
Hi Sunny,

I don't like the idea of putting the "array size" in the first element.  With C's inherent "arrays start at index 0" philosophy using index 0 as the size alters the characteristics of the array.  And it only works for int type variables.

for (i=0; i < array_size; i++);  /*  universally accepted way to traverse an array  */

for (i=1; i <= array_size; i++); /*  traverse a relative 1 array  */

The problem will come later when you try and maintain the code.  You'll see the second for(;;) statement and be forced to investigate it as if it were an error.


I usually use a small structure to maintain dynamic arrays.  That way I can have as many as the program needs and keep the control data for each isolated from the other arrays and use a common set of code to maintain them.

typedef struct
{
  int  ReservedCount;  /*  Number of integers in the array  */
  int  InUse;          /*  Highest index in use  */
  int  *Data;          /*  Integer array  */
} IntArray_t;


IntArray_t  IntArray = { 0,0,0 };





Good Luck,
Kent
0
 
LVL 22

Expert Comment

by:grg99
ID: 10878110
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 );
}
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10878304
The return from myalloc isn't correct, should be

    return ++p;

Beware: the array cannot be longer than 32767 bytes...
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10878341
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?
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10878495
It is even better to
    return p+1
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10885606
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
0
 
LVL 45

Expert Comment

by:Kdo
ID: 10887822
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
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10888086
>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
0
 
LVL 45

Expert Comment

by:Kdo
ID: 10888141

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
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now