sizeof an array of structure without using 'sizeof' operator | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

sizeof an array of structure without using 'sizeof' operator

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-30-2009
rvan rvan is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 27 June 2011, 5:55 AM EDT
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
sizeof an array of structure without using 'sizeof' operator

Hi All,

is it possible to find out the size of an array of structures ( without using 'sizeof' operator). The condition is we have the array of structure instant but we are not aware of the elements inside the structure.

Can someone help me out?


Thanks in advance.
Sponsored Links
    #2  
Old 03-30-2009
pshaikh pshaikh is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 5 October 2010, 2:07 AM EDT
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Yes, I guess you can do; something like


Code:
#pragma pack(1)
#include <header.h> // Header file which contain opaque structure definition
int main()
{
     char begin;
     struct unknown_structure array[100];
     char end;

     printf("Size of structure = %u ", (&begin > &end) ? (&begin - &end - 1)/100 : (&end - &begin - 1)/100);

     return 0;
}

Let me know the output.
This may not be accurate, refer to alignment and structure padding
Sponsored Links
    #3  
Old 03-30-2009
pshaikh pshaikh is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 5 October 2010, 2:07 AM EDT
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
I was hoping it should produce a correct number, but that would happen in absence of processors not needing any padding at the end of structures
    #4  
Old 03-30-2009
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 21 December 2014, 1:06 AM EST
Location: Saskatchewan
Posts: 20,040
Thanks: 855
Thanked 3,458 Times in 3,230 Posts
Not aware of the size of the structure, as in, it contains elements that the compiler has not seen defined?

sizeof doesn't work on those because it really, genuinely doesn't know the size. You can't trick it into giving you information it does not possess; you can't make anything but pointers to those kind of types, and it certainly won't know the size of anything but the pointer.

Otherwise, why not use sizeof? That's what its there for.
Sponsored Links
    #5  
Old 03-30-2009
pshaikh pshaikh is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 5 October 2010, 2:07 AM EDT
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by rvan View Post
Hi All,

is it possible to find out the size of an array of structures ( without using 'sizeof' operator). The condition is we have the array of structure instant but we are not aware of the elements inside the structure.

Can someone help me out?


Thanks in advance.
Try this -


Code:
int main()
{
     struct whatever w;
     char s;

      if( (char*)&w > & z)
        printf("Size of whatever %d\n",(char*)&w-&z)
      else
        printf("Size of whatever %d\n",(char*)&w-&z)
}

Sponsored Links
    #6  
Old 03-30-2009
rvan rvan is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 27 June 2011, 5:55 AM EDT
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you pshaikh. it exactly gives the sizeof the structure. While executing your first sample piece of code... it is mostly &begin is greater than &end. Here all the local variables begin, end and array[100] gets stored in the stack. When you subtract the begin address and end address and divide it by the sizeof the array it gives the sizeof structure.

But if i try to subtract address of say &array[1] - &array[0] doesn't give the sizeof the array instead it gives 1 always. I just tried printing &array[1] and &array[0] which gives the address. When we subtract the address of begin and end variable it gives the number of bytes occupied between those two and why not in &array[1] - &array[0]. Kindly clarify.


Thanks
Sponsored Links
    #7  
Old 03-30-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 20 December 2014, 10:44 PM EST
Location: NM
Posts: 10,313
Thanks: 297
Thanked 822 Times in 764 Posts
This is because the compiler has incorrect knowledge about the structure.

Are you casting the array from void * to the right structure type?
Is the structure datatype declared somewhere that you can reference it?

Please post the first few lines of the function that is trying to find the size of the struct.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem in static structure array in C ahjiefreak Programming 1 12-11-2008 02:13 PM
Search attributes in one structure using the values from another structure dhanamurthy Programming 3 03-27-2008 04:37 AM
MV files from one directory structure(multiple level) to other directory structure srmadab UNIX for Advanced & Expert Users 4 09-13-2006 05:01 PM
sizeof ramneek Programming 7 10-13-2005 08:22 AM
Copying a Directory Structure to a new structure jhansrod UNIX for Dummies Questions & Answers 8 07-27-2005 07:24 AM



All times are GMT -4. The time now is 02:08 AM.