Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find the size of a dynamic array 1

Status
Not open for further replies.

stuartd

Programmer
Jan 8, 2001
146
0
0
US
Hi,

I have a dynamic array which i need to find the size of to re-allocate more space to:
Code:
char **dynamic_array;
char *ptr;

ptr=(char *)malloc(10);
memcpy(ptr,"something\0",10);

dynamic_array=(char **)malloc(1);
dynamic_array[0]=ptr;

ptr=(char *)malloc(10);
memcpy(ptr,"nothing  \0",10);

dynamic_array=(char **) _
   realloc(dynamic_array,sizeof(dynamic_array)+1);   /* ????? */
dynamic_array[1]=ptr;

the sizeof(dynamic_array) always returns a vaule related to the wordsize, not the size of the allocated array.

How do i get this allocated size?

SD
 
You can't with standard routines. Options

1) Use a variable to keep track of it
2) Write your own malloc/realloc that will keep track of it for you.
3) Write a set of vector routines like C++ <vector>

Option 2 is easier than it sounds. All you have to do is allocate sizeof(long) more bytes. Save the size in the first long and return the address of the first byte after the first long. When you want to find out the size, take one step back and the long will tell you the size.
 
Will option 2 be easy if i am using a dynamic array of structures?

SD
 
Yes - all you will be storing is the size allocated.
 
> dynamic_array=(char **)malloc(1);
This is wrong, you've only allocated one byte, not one pointer.
You also don't need to cast the result of malloc in C

Code:
dynamic_array = malloc( sizeof *dynamic_array );

> dynamic_array=(char **) _
> realloc(dynamic_array,sizeof(dynamic_array)+1); /* ????? */
Likewise,
Code:
void *temp = realloc ( dynamic_array, num * sizeof *dynamic_array);
if ( temp != NULL ) {
  dynamic_array = temp;
} else {
  // failed to extend, but we still have what
  // we had originally
}

--
 
Salem - i agree about the byte rather than pointer allocation.

As for casting - I was under the impression that casting is required to ensure the return a pointer of the correct type (for pointer arithmatic), and it is best practice to cast.

SD
 
You do not need to cast from malloc if you include <stdlib.h> .

The link that Salem provides explains this in simple terms.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Bottom line:

Casting the result of malloc is always unnecessary in C, it has no benefit (there's no need to "ensure" anything as you believe), and doing so can hide real problems with your code.

So never do it.


If you're using C++, it's a different story, but in that case, you should generally be using new instead of malloc.
 
Ok, so cast is unnecessary


However, if you do a google search for 'malloc', (almost) every code usage sample uses casting, as does my K&R C book.

It is only if you search for 'cast malloc' you find how to really use malloc.

Thanks to Salem for spotting the original error



SD
 
> However, if you do a google search for 'malloc', (almost) every code usage sample uses casting, as does my K&R C book.
Yes, sadly this is true.
However, you should paste this into your K&R book.

Books, like other things are not immune to bugs creeping into them.

At least fixing the book is only a couple of pages. Some rather bad books are best fixed via the shredder ;-)

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top