C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-01-2009, 08:37 AM   #1
Registered User
 
Join Date: Apr 2009
Posts: 48
length of dynamic array

how do I get the length (or size) of a dynamic array? tried this but I get 0

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   Reply With Quote
Old 09-01-2009, 09:05 AM   #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   Reply With Quote
Old 09-01-2009, 09:13 AM   #3
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Code:
d =  sizeof(array)/sizeof(el);
This will not work since sizeof(array) is just the size of a pointer on your machine (probably 4 or 8 bytes depending on your CPU architecture). As Dino said, the only way to get the size is to use what you passed to calloc() or malloc().
__________________
bit∙hub [bit-huhb] n. A source and destination for information.
bithub is offline   Reply With Quote
Old 09-02-2009, 03:50 AM   #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   Reply With Quote
Old 09-02-2009, 05:46 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 09-02-2009, 05:47 AM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by budala View Post
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
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   Reply With Quote
Old 09-02-2009, 06:05 AM   #7
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,134
Quote:
Originally Posted by budala View Post
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
If it would work of what you have in mind you could put an ending character to enable you to calculate its size.
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   Reply With Quote
Old 09-02-2009, 09:48 AM   #8
Guest
 
Sebastiani's Avatar
 
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 );
}
Example:

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   Reply With Quote
Old 09-02-2009, 10:05 AM   #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   Reply With Quote
Old 09-02-2009, 10:13 AM   #10
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
Quote:
Originally Posted by Dino View Post
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.
Except, at that point you obviously know how large the block is going to be.
Sebastiani is offline   Reply With Quote
Reply

Tags
array, dynamic, length, size

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:23 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22