The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Calculating quantiles in SQL figaro UNIX for Dummies Questions & Answers 3 11-20-2007 05:55 PM
Calculating the average kekanap Shell Programming and Scripting 2 01-26-2007 09:36 AM
Scripts for calculating size and remaining space of a directory automatically. leonall Shell Programming and Scripting 1 12-13-2005 02:30 PM
calculating a check digit jim9418 UNIX for Dummies Questions & Answers 1 02-16-2005 03:14 PM
Calculating the day of the week ligs UNIX for Dummies Questions & Answers 4 10-27-2001 09:28 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-12-2006
apoorvasharma80 apoorvasharma80 is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 69
calculating size of int

Hi,
Is there any way to calculate the size of a built in data type without using 'sizeof' operator? I also don't have the option to read it from std .h file.

regards
Apoorva Kumar
  #2 (permalink)  
Old 10-12-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
You cannot really calculate it; it is a given for each implementation. What platform/compiler are you running?
  #3 (permalink)  
Old 10-12-2006
rs_vijay rs_vijay is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 1
try this..

declare a pointer of that type , say like this..

TYPE *ptr;
t=(ptr+1)-(ptr);

now this value of t gives the sizeof TYPE in bytes i haven't tried it but it should work

regards
- vijay
  #4 (permalink)  
Old 10-12-2006
Hitori's Avatar
Hitori Hitori is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2006
Posts: 360
Quote:
Originally Posted by rs_vijay
declare a pointer of that type , say like this..

TYPE *ptr;
t=(ptr+1)-(ptr);

now this value of t gives the sizeof TYPE in bytes i haven't tried it but it should work

regards
- vijay
May be this will work
Code:
int t;
t=(int)(ptr+1)-(int)(ptr);
  #5 (permalink)  
Old 10-12-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
This code works correctly on 3 different platforms. I think your co-worker is mis-informed somehow. The problem with this is that it is run-time only, not compile-time like sizeof().
Code:
#include <stdlib.h>
/* struct example change it for each struct */
typedef struct
{
    char a[10];
    int  b;
} mystruct_t;

size_t dbl_size=0;
size_t int_size=0;
/* ISO C has CHAR_BIT fixed to eight bits.  If it's an 8 bit per byte machine, char = 1 byte */
size_t char_size=0; 
size_t mystruct_t_size=0;

unsigned long diff(const void *a, const void *b)
{
	unsigned long diff=((unsigned char *)b - (unsigned char *)a);
	return diff;
}

void set_sizes(void)
{
	unsigned char *p=NULL;
	int arr1[2];
	double arr2[2];
	mystruct_t arr3[2];
	char arr4[2];
	dbl_size=diff(&arr2[0],&arr2[1]);
	int_size=diff(&arr1[0],&arr1[1]);
	mystruct_t_size=diff(&arr3[0],&arr3[1]);
	char_size=diff(&arr4[0],&arr4[1]);
}

int main()
{
	set_sizes();
	printf("dbl_size=        %u\n",dbl_size);
	printf("int_size=      	 %u\n",int_size);     
	printf("char_size=       %u\n",char_size);
	printf("mystruct_t_size= %u\n",mystruct_t_size);
	return 0;                      
}
  #6 (permalink)  
Old 10-12-2006
Hitori's Avatar
Hitori Hitori is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2006
Posts: 360
Code:
#include <stdio.h>

int main(int argc, char **argv) {

        int i;
        int j = (int)(&i+1)-(int)&i;
        printf ("(int)(&i+1)-(int)&i= %d\n", j);
        printf ("sizeof(i)= %d\n", sizeof(i));

        return 0;
}
output (for instance only):
Code:
(int)(&i+1)-(int)&i= 4
sizeof(i)= 4
  #7 (permalink)  
Old 10-12-2006
Hitori's Avatar
Hitori Hitori is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2006
Posts: 360
Quote:
Originally Posted by apoorvasharma80
Hi,
Is there any way to calculate the size of a built in data type without using 'sizeof' operator? I also don't have the option to read it from std .h file.

regards
Apoorva Kumar
What happened with sizeof (just interesting)?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0