Skip to content

Instantly share code, notes, and snippets.

@masterl
Last active August 29, 2015 14:19
Show Gist options
  • Save masterl/a4a1d6cc98a84a4eaf9b to your computer and use it in GitHub Desktop.
Save masterl/a4a1d6cc98a84a4eaf9b to your computer and use it in GitHub Desktop.
C Error (alignment)
// The MIT License (MIT)
// Copyright (c) 2015 Leonardo de Oliveira Lourenço
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char c;
int d;
} dummy_t;
void print_struct( void );
void print_sizes( void );
void print_title( char const *title );
void wait_enter( void );
int main( void )
{
dummy_t dummy;
dummy_t *error = (dummy_t *)malloc( sizeof( char ) + sizeof( int ) );
unsigned long address;
print_title( "Struct definition and sizes:" );
print_struct();
print_sizes();
wait_enter();
print_title( "Correct use:" );
printf( "dummy_t dummy;\n" );
printf( "\n &dummy: %p", &dummy );
printf( "\n &dummy.c: %p", &dummy.c );
printf( "\n &dummy.d: %p", &dummy.d );
printf( "\n (&dummy)+1: %p", ( &dummy ) + 1 );
printf( "\n(&dummy.d)+1: %p", ( &dummy.d ) + 1 );
printf( "\n\n" );
wait_enter();
print_title( "Error use (ignoring alignment):" );
printf( "dummy_t *error = (dummy_t *)malloc( sizeof( char ) + sizeof( int ) );\n" );
address = (unsigned long)&error->d;
printf( "\n error: %p", error );
printf( "\n &error->c: %p", &error->c );
printf( "\n &error->d: %p", &error->d );
printf( "\nAllocated space: from %p to %p",
error,
(void *)( ( (unsigned long)error ) + sizeof( char ) + sizeof( int ) - 1 ) );
printf( "\n\nSo, using d on this wrongfully allocated struct"
"\nwould access memory that doesn't belong to it,"
"\nsince it would use:"
"\n%p , %p , %p and %p\n",
(void *)address,
(void *)address + 1,
(void *)address + 2,
(void *)address + 3 );
free( error );
printf( "\n" );
wait_enter();
return 0;
}
void print_struct( void )
{
printf( "typedef struct\n"
"{\n"
" char c;\n"
" int d;\n"
"} dummy_t;\n" );
}
void print_sizes( void )
{
printf( "\ndummy_t size: %2lu bytes", sizeof( dummy_t ) );
printf( "\n char size: %2lu bytes", sizeof( char ) );
printf( "\n int size: %2lu bytes", sizeof( int ) );
printf( "\n" );
}
void print_title( char const *title )
{
printf( "---------------------------------------\n"
" %s\n"
"---------------------------------------\n",
title );
}
void wait_enter( void )
{
printf( "\nPress enter to continue\n" );
getchar();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment