Visit Our UNIX and Linux User Community


calculating size of int


 
Thread Tools Search this Thread
Top Forums Programming calculating size of int
# 1  
Old 10-12-2006
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  
Old 10-12-2006
You cannot really calculate it; it is a given for each implementation. What platform/compiler are you running?
# 3  
Old 10-12-2006
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  
Old 10-12-2006
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)?
# 5  
Old 10-12-2006
Dear Hitori,
The platform is solaris and there is some problem due to which we cannot use sizeof ( i am not very clear about the reason, somebody from my team needs this. So we can think this has been lost ).

Dear Vijay,
The basic of pointer airthmetic tells me that i will get t=1 by doing
t=(ptr+1)-(ptr).

Anyway guys, thanks a lot for sparing time to look at this question.

with regards
Apoorva Kumar
# 6  
Old 10-12-2006
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);

# 7  
Old 10-12-2006
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;                      
}


Previous Thread | Next Thread
Test Your Knowledge in Computers #890
Difficulty: Easy
In sed, the REGEX dot (.) matches exactly one character.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Calculating size of backups

hi everyone i have posted this query but i dont where it has gone i am posting again . my issue is that i have many backup files in a backup nfs share and i need to calculate it size just by adding their individual file sizes which is repetitive jobs. so is there any command or variation of du... (1 Reply)
Discussion started by: janakors
1 Replies

2. UNIX for Beginners Questions & Answers

Calculating size of backups

hi everyone i have a backup share mounted with me on solaris 10 which have daily backups. everyday i need to calculate all individual sizes of files and need to report somewhere. e.g i have .rman 7backup files daily . what i need that is there any commnd or variation of du command which can add... (1 Reply)
Discussion started by: janakors
1 Replies

3. Shell Programming and Scripting

Calculating average of 100 different files of same size

Hey guys..... I have many files (lets say 100 or more) of same size, and I want to create a new output file and calculate the average of first row fifth column in all files and print it in first row of output file, then 2nd row fifth col in all 100 files and print it in 2nd row of output... (1 Reply)
Discussion started by: CAch
1 Replies

4. AIX

calculating the size of the luns using script

Hi This is the lspv output of my server : cbspsdb01 #lspv hdisk0 00c7518d2d512fd4 cdgvg active hdisk1 00c7518d2dcbc9d6 cdgvg active hdisk2 00c7518dcda9199a appvg active hdisk3... (1 Reply)
Discussion started by: samsungsamsung
1 Replies

5. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

6. Shell Programming and Scripting

Help with calculating size of files.

Hi All, I am very new to shell scripting. I have a text file which is the output of another utility. This text file has a list of SAF files which is basically a list of orphan attachments in the attachments directory. Now I want to read each file name from the file, get its size and repeat this... (6 Replies)
Discussion started by: RajeshReddy
6 Replies

7. UNIX for Dummies Questions & Answers

CAlculating page file size

hello dont know if this is the correct forum for this post but i have a question. i am revising for a exam on operating systems and i have the question... A system has a 32bit virtual address divided into 2kbyte pages and each entry in the page table is 6 bytes in length. what is the size... (0 Replies)
Discussion started by: Fortune
0 Replies

8. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies

9. Shell Programming and Scripting

Scripts for calculating size and remaining space of a directory automatically.

I would like to create a script for calculating size and remaining space of a directory automatically every 24 hours, then send an email to report to the admin. * POSIX and PERL are preferred. Can anyone help, please? (1 Reply)
Discussion started by: leonall
1 Replies

10. Programming

difference between int ** func() and int *& func()

What is the difference between int** func() and int*& func(). Can you please explain it with suitable example. Thanks, Devesh. (1 Reply)
Discussion started by: devesh
1 Replies

Featured Tech Videos