Skip to content

Instantly share code, notes, and snippets.

@KarlPurk
Created March 29, 2014 13:07
Show Gist options
  • Save KarlPurk/9854168 to your computer and use it in GitHub Desktop.
Save KarlPurk/9854168 to your computer and use it in GitHub Desktop.
Memory Investigation - Looking at how chars are stored in memory.
#include "stdafx.h"
#include <iostream>
using namespace std;
void printHeader()
{
cout << "Letter\t" << "ASCII\t" << "Hex\t\t" << "Address\t\n";
cout << "---------------------------------------\n";
}
/**
* Prints a given char out in it's many forms.
* 1. as an alphabetical character
* 2. as it's ASCII value
* 3. as it's Hexadecimal value
* 4. it's address in memory
*/
void printCharacter(char &letter)
{
cout << letter << "\t";
cout << (int)letter << "\t";
cout << (int*)letter << "\t";
cout << (int*)&letter << "\t\n";
}
/**
* Prints a string of text one character at a time.
*/
void printString()
{
string text = "Hello World";
for (size_t i = 0; i < text.length(); i++) {
printCharacter(text[i]);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
// Print out the character A in all it's forms
char letter = 'A';
cout << "Char Example\n";
cout << "------------\n";
printHeader();
printCharacter(letter);
// Print out each character of Hello World, in all their forms
cout << "\n";
cout << "String Examples\n";
cout << "------------\n";
printHeader();
printString();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment