A side-discussion developed in the Wierd Visual Studio Problem thread. Rather than continue to hijack that topic, I've created a new thread and copied over the relevant postings.
<text of reply snipped -- it was the following code that triggered the side-discussion>
Code:
////////////////////////////////////////////////////////////////////////// // // // Parent Project Name: sample project for bytes // // // Project file: boom.cpp // // // // Description: Demonstrate offset needed in allocaton // // // // // // Notes: - // // // // Date Edited: 2009/03/20 // // Version: 1.0r // // // // Coder: Andrew Grammenos (andreas.grammenos@gmail.com) // // // // License: BSD License // // // ////////////////////////////////////////////////////////////////////////// #include <stdlib.h> #include <string.h> #include <malloc.h> // set the codes #define ERROR_CODE 1 #define ALLOCATION_LIMIT -1 // disable this in nix and legacy versions of VS #pragma warning ( disable : 4996 ) // set true false macros #define TRUE 0; #define FALSE 1; // forward function declarations int createTable(); void freeChunks( char **ptr, int tabSize ); int addStringsToTable(char **ptr); int main(int argc, char* argv[]) { createTable(); return TRUE; } int createTable() { // example size int tabSize = 5; // now that we have our choice, let's create our dynamic string pointer // array using the specified size char **sptrTable = (char**)calloc( (tabSize+1), sizeof(int) ); // check for valid allocation if( sptrTable == NULL ) { fprintf( stdout,"\nNo memory was allocated due to insufficient memory, please free up some memory and try again\nThe program will now exit\n"); return ERROR_CODE; } // set our exit code for the allocation size sptrTable[tabSize] = (char*)calloc( 1, sizeof(int) ); sptrTable[tabSize][0] = ALLOCATION_LIMIT; // pass the address of the pointer table to our function // that adds the strings addStringsToTable( sptrTable ); // free up what we used freeChunks(sptrTable, tabSize); return TRUE; } void freeChunks( char **ptr, int tabSize ) { int index = 0; // free the columns while( index <= tabSize && (ptr[index] != NULL) ) { free( ptr[index] ); index++; } // free the rows free( ptr ); } int addStringsToTable(char **ptr) { // create a buffer to store the string that the user will input char buff[BUFSIZ]; char inB = 0; int allocCheck = FALSE; int index = 0; while ( allocCheck ) { // prompt the user for input fprintf(stdout, "\nPlease enter a string (max length is: %d)", BUFSIZ); // get the input fscanf( stdin, "%s", &(buff) ); // create the string in the table and // check if the memory was allocated ok if ( (ptr[index] = (char *)calloc( strlen(buff) /* + 1 uncomment to run fine ;) */, sizeof(char) ) ) == NULL ) { return ERROR_CODE; } // copy the actual string strcpy( ptr[index], buff ); // print diagnostic message fprintf(stdout, "\nString successfully inserted\nPlease type any key to continue or E to terminate and press enter\n\n"); // advance the pointer to next value ptr[index++]; // check for allocation limit if ( ptr[index] != NULL && ptr[index][0] == (ALLOCATION_LIMIT) ) { fprintf(stdout, "\n\nLimit reached exiting now"); allocCheck = TRUE; } } return TRUE; }
Comment