Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //:SVS:String_Vs_String: Version#2: DANGEROUS_CASTING_MAGIC
- //:PASTEBIN_URL_TO_THIS_FILE: https://pastebin.com/ZUQG10Dz
- #include <stdio.h> //:for: printf(...)
- #include<stdlib.h> //: <-- for malloc(...)
- //: https://en.cppreference.com/w/c/language/operator_precedence
- //: You were correct:
- //: [] binds tighter than &
- //:
- //: But I just realized that the effective result
- //: of both expressions is the same.
- //:Moved outside of function so we can reset.
- static int times_called=0;
- void TotallyNot_clGetPlatformInfo(
- void* str_out
- )
- {
- times_called++;
- char* str=(char*)str_out;
- //:Setting different results for each pair
- //:just to verify we are in fact working with
- //:different memory addresses.
- if( 1 *2 >= times_called ){
- str[0]='A';
- str[1]='B';
- str[2]='C';
- str[3]=( 0 ); //:null_terminator.
- }else
- if( 2 *2 >= times_called ){
- str[0]='D';
- str[1]='E';
- str[2]='F';
- str[3]=( 0 ); //:null_terminator.
- }else
- if( 3 *2 >= times_called ){
- str[0]='G';
- str[1]='H';
- str[2]='I';
- str[3]=( 0 ); //:null_terminator.
- }else
- if( 4 *2 >= times_called ){
- str[0]='J';
- str[1]='K';
- str[2]='L';
- str[3]=( 0 ); //:null_terminator.
- }else
- if( 5 *2 >= times_called ){
- str[0]='M';
- str[1]='N';
- str[2]='O';
- str[3]=( 0 ); //:null_terminator.
- }else
- {
- str[0]='_';
- str[1]='_';
- str[2]='_';
- str[3]=( 0 ); //:null_terminator.
- };;
- }
- int main( void )
- {
- printf("[BEG:FILE:whatever:FUNC:main]\n");
- { //:SCOPE
- char str_sta_01[256]={0};
- char* str_hea_01 = malloc( 256 );
- char str_sta_02[256]={0};
- char* str_hea_02 = malloc( 256 );
- char str_sta_03[256]={0};
- char* str_hea_03 = malloc( 256 );
- char str_sta_04[256]={0};
- char* str_hea_04 = malloc( 256 );
- char str_sta_05[256]={0};
- char* str_hea_05 = malloc( 256 );
- TotallyNot_clGetPlatformInfo( str_sta_01 );
- TotallyNot_clGetPlatformInfo( str_hea_01 );
- //: WRONG unless function is emulating passing by
- //: reference by taking a void** (double void pointer)
- //: TotallyNot_clGetPlatformInfo( &str_sta_01 );
- //: TotallyNot_clGetPlatformInfo( &str_hea_01 );
- //:Equivalent end results: ( I did not realize this )
- //:Both of these get the base address of array.
- ////////////////////////////////////////////////////////
- //:De-reference first element, then take address.
- //:Effectively: Address of head of array.
- TotallyNot_clGetPlatformInfo( &(str_sta_02[0]) );
- TotallyNot_clGetPlatformInfo( &(str_hea_02[0]) );
- //:Get double pointer (char**) then de-reference
- //:at the base using [0], getting char*
- TotallyNot_clGetPlatformInfo( (&str_sta_03)[0] );
- TotallyNot_clGetPlatformInfo( (&str_hea_03)[0] );
- //:For good measure, let's write the above
- //:two examples slightly differently:
- ////////////////////////////////////////////////////
- //:Dereference, then take address:
- TotallyNot_clGetPlatformInfo( &str_sta_04[0] );
- TotallyNot_clGetPlatformInfo( &str_hea_04[0] );
- //:Take address, then dereference:
- TotallyNot_clGetPlatformInfo( &(str_sta_05)[0]);
- TotallyNot_clGetPlatformInfo( &(str_hea_05)[0]);
- ////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////
- printf( "[str_sta_01]:%s\n", str_sta_01 );
- printf( "[str_hea_01]:%s\n", str_hea_01 );
- printf( "[str_sta_02]:%s\n", str_sta_02 );
- printf( "[str_hea_02]:%s\n", str_hea_02 );
- printf( "[str_sta_03]:%s\n", str_sta_03 );
- printf( "[str_hea_03]:%s\n", str_hea_03 );
- printf( "[str_sta_03]:%s\n", str_sta_04 );
- printf( "[str_hea_03]:%s\n", str_hea_04 );
- printf( "[str_sta_03]:%s\n", str_sta_05 );
- printf( "[str_hea_03]:%s\n", str_hea_05 );
- ////////////////////////////////////////////////////////
- //:D:Dangerous.For: "Lets get dangerous"
- //:DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD://
- //:Maybe these statements become different when you
- //:start playing loose and dangerously by subverting
- //:the type system even more. Let's try it out.
- //:Typical generic function pointer type I use when
- //:fetching function addresses from a DLL
- //:using "GetProcAddress"
- typedef void(*GENERIC_FUNCTION)( void );
- //:Generic function "01":
- //:A generic function taking one argument and
- //:returning whatever.
- typedef void* (*F_01)( void* );
- GENERIC_FUNCTION
- pfn=(GENERIC_FUNCTION)&(TotallyNot_clGetPlatformInfo);
- times_called=0;
- #define U64 unsigned long long int //:///////////////://
- #define DANGEROUS_CASTING_MAGIC (void*)(U64)
- ((F_01)pfn)( DANGEROUS_CASTING_MAGIC str_sta_01 );
- ((F_01)pfn)( DANGEROUS_CASTING_MAGIC str_hea_01 );
- ((F_01)pfn)( DANGEROUS_CASTING_MAGIC &(str_sta_02[0]) );
- ((F_01)pfn)( DANGEROUS_CASTING_MAGIC &(str_hea_02[0]) );
- ((F_01)pfn)( DANGEROUS_CASTING_MAGIC (&str_sta_03)[0] );
- ((F_01)pfn)( DANGEROUS_CASTING_MAGIC (&str_hea_03)[0] );
- ((F_01)pfn)( DANGEROUS_CASTING_MAGIC &str_sta_04[0] );
- ((F_01)pfn)( DANGEROUS_CASTING_MAGIC &str_hea_04[0] );
- ((F_01)pfn)( DANGEROUS_CASTING_MAGIC &(str_sta_05)[0] );
- ((F_01)pfn)( DANGEROUS_CASTING_MAGIC &(str_sta_05)[0] );
- #undef U64 //:///////////////////////////////////////://
- #undef DANGEROUS_CASTING_MAGIC //:///////////////////://
- ////////////////////////////////////////////////////////
- printf( "[str_sta_01]:%s\n", str_sta_01 );
- printf( "[str_hea_01]:%s\n", str_hea_01 );
- printf( "[str_sta_02]:%s\n", str_sta_02 );
- printf( "[str_hea_02]:%s\n", str_hea_02 );
- printf( "[str_sta_03]:%s\n", str_sta_03 );
- printf( "[str_hea_03]:%s\n", str_hea_03 );
- printf( "[str_sta_03]:%s\n", str_sta_04 );
- printf( "[str_hea_03]:%s\n", str_hea_04 );
- printf( "[str_sta_03]:%s\n", str_sta_05 );
- printf( "[str_hea_03]:%s\n", str_hea_05 );
- ////////////////////////////////////////////////////////
- //:DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD://
- free( str_hea_01 );
- free( str_hea_02 );
- free( str_hea_03 );
- free( str_hea_04 );
- free( str_hea_05 );
- } //:SCOPE
- printf("[END:FILE:whatever:FUNC:main]\n");
- }
- //:Bash( GitBash ) build script for this code:
- /**-********************************************************-**+
- ## SC[ hkmf-mini] #########################################
- ## SEE[ hkmf-c11 ] for reference: #########################
- ############################################################
- gcc \
- -x c \
- -c "SVS.C11" \
- -o object_file.o \
- \
- -Werror \
- -Wfatal-errors \
- -Wpedantic \
- -Wall \
- -Wextra \
- \
- -fstrict-aliasing \
- -Wstrict-aliasing \
- \
- -D MACRO_COMPILE_ONLY_THIS_FILE_AS_DEMO \
- \
- -std=c11 \
- -m64 ###############################################
- #### ####
- gcc -o EXE.exe object_file.o #### ####
- rm object_file.o #### ####
- ./EXE.exe #### ####
- rm EXE.exe #### ####
- #### ####
- read -p "[ENTER_TO_EXIT]:" #### ####
- ############################################################
- +**-********************************************************-**/
Add Comment
Please, Sign In to add comment