memcpy

find bytesize w/o sizeof

Nov 28th, 2011
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.52 KB | None | 0 0
  1. /*
  2.  
  3. three ways to find out the byte-size of four C types, without sizeof()
  4.  
  5. */
  6.  
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13.  
  14. /************************************** SHIFT **************************************/
  15.  
  16. #define count(x,y) for(x=1; x; x <<= 1) y++
  17.  
  18. void verify_bits_count() {
  19.    
  20.     char    chr_init = 0;
  21.     short   sht_init = 0;
  22.     int     int_init = 0;
  23.     long    lng_init = 0;
  24.  
  25.     short chr_ret = 0, sht_ret = 0, int_ret = 0, lng_ret = 0;
  26.    
  27.     count(chr_init, chr_ret);
  28.     count(sht_init, sht_ret);
  29.     count(int_init, int_ret);
  30.     count(lng_init, lng_ret);
  31.    
  32.     printf("\n\nbit count\n\n");
  33.     printf("char has %d bytes\n",chr_ret/8);
  34.     printf("short has %d bytes\n",sht_ret/8);
  35.     printf("int has %d bytes\n",int_ret/8);
  36.     printf("long has %d bytes\n",lng_ret/8);
  37.    
  38.    
  39. }
  40.  
  41. /************************************** LOOP **************************************/
  42.  
  43. short loop_test(char *chr_char) {
  44.     short chr_val = 0;
  45.     int chr_ret = 0;
  46.     for (chr_val = 0; chr_val <= 100; chr_val++)
  47.         chr_ret += (chr_char[chr_val] == -1 ? 1 : 0);
  48.     return chr_ret;
  49. }
  50.  
  51. #define loop_tst(cc,cv,cr) for (cv = 0; cv <= 100; cv++) cr += (cc[cv] == -1 ? 1 : 0);
  52.  
  53. void verify_bits_loop() {
  54.     char *dst = (char *)malloc(100);
  55.     short chr_ret = 0;
  56.     short sht_ret = 0;
  57.     short int_ret = 0;
  58.     short lng_ret = 0;
  59.    
  60.     char chr_val = 0xFFFFFFFFFFFFFFFF;
  61.     memset(dst, 0x00, 100);
  62.     strcpy(dst, (char *)&chr_val);
  63.     loop_tst(dst,chr_val,chr_ret);
  64.    
  65.     short sht_val = 0xFFFFFFFFFFFFFFFF;
  66.     memset(dst, 0x00, 100);
  67.     strcpy(dst, (char *)&sht_val);
  68.     loop_tst(dst,sht_val,sht_ret);
  69.    
  70.    
  71.     int int_val = (int)0xFFFFFFFFFFFFFFFF;
  72.     memset(dst, 0x00, 100);
  73.     strcpy(dst, (char *)&int_val);
  74.     loop_tst(dst,int_val,int_ret);
  75.    
  76.    
  77.     long lng_val = 0xFFFFFFFFFFFFFFFF;
  78.     memset(dst, 0x00, 100);
  79.     strcpy(dst, (char *)&lng_val);
  80.     loop_tst(dst, lng_val, lng_ret);
  81.    
  82.     printf("\n\nloop test\n\n");
  83.     printf("char has %d bytes\n",chr_ret);
  84.     printf("short has %d bytes\n",sht_ret);
  85.     printf("int has %d bytes\n",int_ret);
  86.     printf("long has %d bytes\n",lng_ret);
  87.    
  88. }
  89.  
  90. /************************************** STRUCT **************************************/
  91.  
  92. #define d_bitsize_init(var) ( (int) (&var+1) - (int) &var) /* this macro not written by me but included for the sake of demonstration as a makeshift "sizeof" */
  93.  
  94. struct var_tst {
  95.     uint8_t t_char;
  96.     uint16_t t_shrt __attribute__((__packed__));
  97.     uint32_t t_uint __attribute__((__packed__));
  98.     uint64_t t_long __attribute__((__packed__));
  99.    
  100. };
  101.  
  102. void verify_bits_struct() {
  103.     struct var_tst *comp = calloc(1,d_bitsize_init(comp));
  104.     short sc = d_bitsize_init(*comp);
  105.     char dst[sizeof(*comp)];
  106.     memset(&comp->t_char, 0x0, (int)d_bitsize_init(comp->t_char));
  107.     memset(&comp->t_shrt, 0x1, (int)d_bitsize_init(comp->t_shrt));
  108.     memset(&comp->t_uint, 0x2, (int)d_bitsize_init(comp->t_uint));
  109.     memset(&comp->t_long, 0x3, (int)d_bitsize_init(comp->t_long));
  110.     memcpy(dst, comp, sizeof(*comp));
  111.     short lcnt = 0, icnt = 0, scnt = 0, ccnt = 0;
  112.     for (;sc >= 0;sc--) {
  113.         switch (dst[sc] ) {
  114.             case 0x0: ccnt++; break;
  115.             case 0x1: scnt++; break;
  116.             case 0x2: icnt++; break;
  117.             case 0x3: lcnt++; break;
  118.         }
  119.     }
  120.     printf("\n\nstruct packing\n\n");
  121.     printf("char has %d bytes\n",ccnt);
  122.     printf("short has %d bytes\n",scnt);
  123.     printf("int has %d bytes\n",icnt);
  124.     printf("long has %d bytes\n",lcnt);
  125. }
  126.  
  127. /************************************** MAIN **************************************/
  128.  
  129. int main (int argc, const char * argv[]){
  130.     verify_bits_loop();
  131.     verify_bits_struct();
  132.     verify_bits_count();
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment