online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS Code, Compile, Run and Debug online from anywhere in world. *******************************************************************************/ #include <stdint.h> // for uint8_t #include <stdio.h> // for printf() #include <stdlib.h> // for EXIT_SUCCESS #include <string.h> // for memcpy() /*---------------------------------------------------------------------------*/ // PROTOTYPES /*---------------------------------------------------------------------------*/ void * memcpy_safer ( void * destination, size_t sizeOfDestination, const void * source, size_t sizeOfSource, size_t num ); void * memcpy_safer2 ( void * destination, size_t sizeOfDestination, const void * source, size_t sizeOfSource, size_t num ); void initializeBuffer (void *dataPtr, size_t dataSize, uint8_t value); void dumpBuffer (const char* prefix, void *dataPtr, size_t dataSize); /*---------------------------------------------------------------------------*/ // MAIN /*---------------------------------------------------------------------------*/ int main() { uint8_t smallerBuffer[10]; uint8_t largerBuffer[15]; // Test 1: copy longer buffer into smaller buffer. printf ("\nInitialized buffers:\n\n"); // Initialize buffers with something we can identify later. initializeBuffer (smallerBuffer, sizeof(smallerBuffer), 0x1); dumpBuffer ("smallerBuffer", smallerBuffer, sizeof(smallerBuffer)); initializeBuffer (largerBuffer, sizeof(largerBuffer), 0x2); dumpBuffer ("largerBuffer ", largerBuffer, sizeof(largerBuffer)); printf ("\nTest 1: Copying largerBuffer into smallerBuffer...\n\n"); memcpy_safer (smallerBuffer, sizeof(smallerBuffer), largerBuffer, sizeof(largerBuffer), 42); // TODO: Also test with a smaller value. dumpBuffer ("smallerBuffer", smallerBuffer, sizeof(smallerBuffer)); // Test 2: copy smaller buffer into larger buffer. printf ("\nInitialized buffers:\n\n"); // Initialize buffers with something we can identify later. initializeBuffer (smallerBuffer, sizeof(smallerBuffer), 0x1); dumpBuffer ("smallerBuffer", smallerBuffer, sizeof(smallerBuffer)); initializeBuffer (largerBuffer, sizeof(largerBuffer), 0x2); dumpBuffer ("largerBuffer ", largerBuffer, sizeof(largerBuffer)); printf ("\nTest 2: Copying smallerBuffer into largerBuffer...\n\n"); memcpy_safer (largerBuffer, sizeof(largerBuffer), smallerBuffer, sizeof(smallerBuffer), 42); // TODO: Also test with a smaller value. dumpBuffer ("largerBuffer ", largerBuffer, sizeof(largerBuffer)); return EXIT_SUCCESS; } /*---------------------------------------------------------------------------*/ // FUNCTIONS /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ // My ridiculous "safer" memcpy. /*---------------------------------------------------------------------------*/ void * memcpy_safer ( void * destination, size_t sizeOfDestination, const void * source, size_t sizeOfSource, size_t num ) { // Use whichever size is the smallest. if ((num > sizeOfDestination) || (num > sizeOfSource)) { if (sizeOfDestination < sizeOfSource) { num = sizeOfDestination; } else { num = sizeOfSource; } } return memcpy ( destination, source, num); } /*---------------------------------------------------------------------------*/ // Bing CoPilot changes. /*---------------------------------------------------------------------------*/ void * memcpy_safer2 ( void * destination, size_t sizeOfDestination, const void * source, size_t sizeOfSource, size_t num ) { if (!destination || !source) { fprintf(stderr, "Error: NULL pointer passed to memcpy_safer.\n"); return NULL; } if (num > sizeOfDestination || num > sizeOfSource) { size_t adjusted = (sizeOfDestination < sizeOfSource) ? sizeOfDestination : sizeOfSource; fprintf(stderr, "Warning: Truncating copy from %zu to %zu bytes.\n", num, adjusted); num = adjusted; } return memcpy(destination, source, num); } /*---------------------------------------------------------------------------*/ // Utility function to initialize a buffer to a set value. /*---------------------------------------------------------------------------*/ void initializeBuffer (void *dataPtr, size_t dataSize, uint8_t value) { if (NULL != dataPtr) { memset (dataPtr, value, dataSize); } } /*---------------------------------------------------------------------------*/ // Utility function to dump bytes in a buffer, with an optional prefix. /*---------------------------------------------------------------------------*/ void dumpBuffer (const char* prefix, void *dataPtr, size_t dataSize) { if (NULL != dataPtr) { if (NULL != prefix) { printf ("%s: ", prefix); } for (size_t idx=0; idx<dataSize; idx++) { printf ("%02x ", ((uint8_t*)dataPtr)[idx]); } printf ("\n"); } } // End of memcpy_safer.c

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue