How to calculate size of an array of dynaically malloc'd structs?

A

Angus Comber

Hello

If I do this:

struct mystruct
{
long nKey;
char szItem[20];
};

long numitemstocreate = GetItems();
struct mystruct* devlist = (mystruct*)malloc(numlines * sizeof(mystruct));

// Then populate some values

Now I want to find number of array elements - ie get numitemstocreate. I
know I already have it - but may want to get again.

How do I get the number of array items?

Angus Comber
(e-mail address removed)
 
J

Jeremy Yallop

Angus said:
struct mystruct* devlist = (mystruct*)malloc(numlines * sizeof(mystruct));

// Then populate some values

Now I want to find number of array elements - ie get numitemstocreate. I
know I already have it - but may want to get again.

See section 7 of the C FAQ.

Jeremy.
 
P

pete

Angus said:
Hello

If I do this:

struct mystruct
{
long nKey;
char szItem[20];
};

long numitemstocreate = GetItems();
struct mystruct* devlist = (mystruct*)malloc(numlines * sizeof(mystruct));

// Then populate some values

Now I want to find number of array elements
- ie get numitemstocreate.
I know I already have it - but may want to get again.

Keep track of it.
Pass it as an argument to every function in the sequence
of calls that leads to the function that actually uses it.
 
A

Al Bowers

Angus said:
Hello

If I do this:

struct mystruct
{
long nKey;
char szItem[20];
};

long numitemstocreate = GetItems();
struct mystruct* devlist = (mystruct*)malloc(numlines * sizeof(mystruct));

// Then populate some values

Now I want to find number of array elements - ie get numitemstocreate. I
know I already have it - but may want to get again.

How do I get the number of array items?

typedef struct mystructArr
{
struct mystruct *element;
size_t size;
}mystructArr;

Declare and use a datatype that has a member that points to the array
and a member that keeps a count of the number of array elements allocate.

Example of use:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define ITEMSZ 20

typedef struct mystruct
{
long nKey;
char szItem[ITEMSZ];
}mystruct;

typedef struct mystructArr
{
mystruct *element;
size_t size;
}mystructArr;

mystruct *AddElement(mystructArr *p,long key, const char *s);
void FreeElements(mystructArr *p);

int main(void)
{
mystructArr names = {NULL,0};
size_t i;

AddElement(&names,7L,"George Washington");
AddElement(&names,45L, "George Bush");

puts("The array contents");
for(i = 0; i < names.size;i++)
printf("names.element[%u].szItem = \"%s\"\n"
"names.element[%u].nKey = %ld\n\n",i,
names.element.szItem,i, names.element.nKey);
FreeElements(&names);
return 0;
}

mystruct *AddElement(mystructArr *p, long key, const char *s)
{
mystruct *tmp;
size_t cnt;

if( !p || ITEMSZ < 1) return NULL;
cnt = p->size;
if((tmp = realloc(p->element,(cnt+1)*(sizeof *tmp))) == NULL)
return NULL;
p->element = tmp;
p->element[cnt].nKey = key;
strncpy(p->element[cnt].szItem, s, ITEMSZ);
p->element[cnt].szItem[ITEMSZ-1] = '\0';
p->size++;
return &p->element[cnt];
}

void FreeElements(mystructArr *p)
{
if(p)
{
free(p->element);
p->element = NULL;
p->size = 0;
}
return;
}
 
M

Martin Ambuhl

Angus said:
Hello

If I do this:

struct mystruct
{
long nKey;
char szItem[20];
};

long numitemstocreate = GetItems();
struct mystruct* devlist = (mystruct*)malloc(numlines * sizeof(mystruct));

Doesn't anyone follow the newsgroup or check its FAQs before posting. The
invocation of malloc above is extremely ugly. Just use
struct mystruct *devlist = malloc(numlines * sizeof *devlist);
// Then populate some values

No, that is _not_ what you do next. You check the value of devlist to see
if the malloc failed and, if it failed, handle it.
Now I want to find number of array elements - ie get numitemstocreate. I
know I already have it - but may want to get again.

Then save it. What idiot wouldn't be able to figure that out? One that
doesn't follow the newsgroup or check the FAQs before posting, that kind of
idiot.

How do I get the number of array items?

You have it. "My feet are on the ground. How do I get my feet on the ground?"
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top