• Log in or sign up for an account here: https://www.sythe.org/login

Let's talk about datatypes

Faskist

Tuxhead
Joined
Apr 25, 2005
Sythe Gold
0
Messages
1,869
So, this morning, I wrote a simple program to sate my curiosity (Such is all hacking) about something I'd never bothered to find out -- just how big is the memory allocated to the datatypes we use everyday?

Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
  int i;

  i = sizeof(char);
  
  printf( "A char is this big: |" );
  while( i > 0)
    {
      printf( "-" );
      --i;
    }
  printf( "|\n" );
  
  i = sizeof(int);
  
  printf( "An int is this big: |" );
  while( i > 0)
    {
      printf( "-" );
      --i;
    }
  printf( "|\n" );
  
  i = sizeof(short);
  
  printf( "A short is this big: |" );
  while( i > 0)
    {
      printf( "-" );
      --i;
    }
  printf( "|\n" );
  
  i = sizeof(long);
  
  printf( "A long is this big: |" );
  while( i > 0)
    {
      printf( "-" );
      --i;
    }
  printf( "|\n" );
  
  return 0;
}

I know the code seriously needs refactoring, and it could be cleaned up so much, but I just wanted a quick solution.

The answer you'll get depends on platform, but in mine... int is as big as long!! The horror! The bloat!
 
That's because with your compiler (and mine) integers and longs are able to take on the same values (-2147483648 to 2147483647). The more common integer we're used to (-32768 to 32767) is changed to be called short int.

 
That's so redundant, lol. How does I changed long to 8 bytes?
 
Le Monkey
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
__int64?
 
Yup, int = long.

I at least prefer this to VB making Integer = Short. So I always accidentally use Integer instead of Long. Or I used to at least, I don't use VB much these days.

I know how to do 64bit math with 32bit registers. At least I used to... it's all kind of blurry... I haven't used asm in like a year. :'(
 
Clefairy
Supporting Business
Discord Voice
Jigglypuff
Hell yeah boooi
Poképedia
Who did this to my freakin' car!
Dat Boi
Dolan Duck
Dolan Trump
I'm LAAAAAAAME
Live Free or Die
Mew
Mewtwo
Poké Prizebox
Rust Player
Sythe Awards 2016 Winner
Sythe Awards 2017 Winner
Sythe Awards 2019 Winner
Tier 3 Prizebox
Toast Wallet User
Of course an int is the same size as a long. Both are 32 bit full words on x86.

Incidentally, that code is incredibly roundabout. All you needed was one printf:

C:\DOCUME~1\RICHAR~1>type tmp.c
#include <stdio.h>

int main() {

printf("char %d, int %d, long %d, double %d", sizeof(char), sizeof(int), sizeof(long), sizeof(double));

}

C:\DOCUME~1\RICHAR~1>gcc tmp.c

C:\DOCUME~1\RICHAR~1>a
char 1, int 4, long 4, double 8
 
Of course an int is the same size as a long. Both are 32 bit full words on x86.

Incidentally, that code is incredibly roundabout. All you needed was one printf:
I actually wrote some code very similar to that when discussing it on a textboard, but my code produces a nice pretty (almost) graphical representation of it (And could still be improved and refactored).
 
Runescape Gamblingrunescape gambling
Back
Top