![]() |
| | #1 |
| Registered User Join Date: Apr 2009
Posts: 48
| length of dynamic array Code: #include <stdio.h>
#include <stdlib.h>
typedef struct a
{
int age;
char name[10];
} el;
int main (int argc, const char * argv[])
{
int i,n;
el *array;
int d;
printf("length :> ");
scanf("%d",&n);
array = (el *) calloc (n,sizeof(el));
printf("\nINPUT\n\n");
for (i=0; i<n; i++)
{
printf("name %d. el :> ",i);
scanf("%s", array[i]. name);
printf("age %d. el :> ",i);
scanf("%d",& array[i]. age);
}
printf("\nPRINTOUT\n\n");
for (i=0; i<n; i++)
{
printf("%d. el :> NAME:\t%s\tAGE:\t%d\n",i, array[i]. name, array[i]. age);
}
d = sizeof(array)/sizeof(el);
printf("Length is :> %d",d);
return 0;
}
Last edited by budala; 09-02-2009 at 03:48 AM. |
| budala is offline | |
| | #2 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,929
| Variable n is not initialized. The value of n * sizeof(el) will be how large array is - you have to keep track of that.
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. |
| Dino is offline | |
| | #3 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| Code: d = sizeof(array)/sizeof(el);
__________________ bit∙hub [bit-huhb] n. A source and destination for information. |
| bithub is offline | |
| | #4 |
| Registered User Join Date: Apr 2009
Posts: 48
| oops, I changed the order of instructions red and blue. I don't want to keep track of the size, I want to have a dynamic array and calculate its size |
| budala is offline | |
| | #5 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| I'll put it in simple words: There is no (standard) way to calculate the length of a dynamic array in C. If you really, really, really, really must do it, there are non-portable solutions. Microsoft's compilers gives you the option of _msize. Then you could use the OS's allocation APIs to allocate and find the size of it.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #6 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| You can't calculate the size of a malloc'ed piece of memory (unless you get your hands dirty with the grubby internals of malloc, but then that will only work on that one particular system). The whole point of malloc is that you know what you called it with. |
| tabstop is offline | |
| | #7 | |
| Registered User Join Date: Jun 2008
Posts: 1,134
| Quote:
In most cases tracking the size would be the best way though. Since it is easy and the extra calculations not time consuming compared to malloc(). If you plan on using malloc (or realloc) a lot of times and are afraid you might miss an update of its size you could wrap "the keep track of size" code and malloc in a single function | |
| C_ntua is offline | |
| | #8 |
| Guest Join Date: Aug 2001
Posts: 4,923
| Another option is to store the size information in the data itself. Code: #include <stdlib.h>
size_t* b_element_size_ptr_( void* buf )
{
return ( size_t* )( ( char* )buf - sizeof( size_t ) );
}
size_t* b_length_ptr_( void* buf )
{
return ( size_t* )( ( char* )buf - ( sizeof( size_t ) << 1 ) );
}
void* b_allocate( size_t siz, size_t len )
{
char*
buf = malloc( siz * len + ( sizeof( size_t ) << 1 ) );
if( buf )
{
buf += sizeof( size_t ) << 1;
*b_element_size_ptr_( buf ) = siz;
*b_length_ptr_( buf ) = len;
}
return buf;
}
void b_deallocate( void* buf )
{
if( buf )
free( b_length_ptr_( buf ) );
}
size_t b_length( void* buf )
{
return *b_length_ptr_( buf );
}
size_t b_bytes( void* buf )
{
return b_length( buf ) * *b_element_size_ptr_( buf );
}
Code: #include <stdio.h>
int main( void )
{
int*
data = b_allocate( sizeof( int ), 1024 );
if( data )
{
printf( "Length of data: %d\n", b_length( data ) );
printf( "Size (in bytes): %d\n", b_bytes( data ) );
}
b_deallocate( data );
return 0;
}
Last edited by Sebastiani; 09-02-2009 at 09:53 AM. |
| Sebastiani is offline | |
| | #9 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,929
| There's probably some OS specific system call you could make prior to the malloc, to determine your process's free memory, and then you could call it again after the malloc, and figure out the difference. Sounds like a contrived class exercise to me.
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. |
| Dino is offline | |
| | #10 | |
| Guest Join Date: Aug 2001
Posts: 4,923
| Quote:
| |
| Sebastiani is offline | |
![]() |
| Tags |
| array, dynamic, length, size |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dynamic Array | firetheGlazer | C Programming | 4 | 07-11-2008 11:57 AM |
| the length of my array is not what I expected | luca | C Programming | 7 | 12-05-2006 03:14 AM |
| Checking maximum values for dynamic array... | AssistMe | C Programming | 1 | 03-21-2005 12:39 AM |
| Dynamic 2d array | Mithoric | C++ Programming | 8 | 12-29-2003 09:19 AM |
| Help with an Array | omalleys | C Programming | 1 | 07-01-2002 08:31 AM |