home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ User's Journal & Wi…eveloper's Journal Tools
/
C-C__Users_Journal_and_Windows_Developers_Journal_Tools_1997.iso
/
windbase
/
memslc.3
/
memory.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-07-13
|
7KB
|
170 lines
/*****************************************************************************\
** **
** WW WW IIIIIIII NNN NN DDDDDDD BBBBBBB AA SSSSSS EEEEEEEE **
** WW W WW II NNNN NN DD DD BB BB AA AA SS EE **
** WW W WW II NN NN NN DD DD BBBBBBB AAAAAAAA SSSSSS EEEEEE **
** WW W WW II NN NNNN DD DD BB BB AA AA SS EE **
** WWWWW IIIIIIII NN NNN DDDDDDD BBBBBBB AA AA SSSSSS EEEEEEEE **
** **
** SSSSSS OOOOOO FFFFFFFF TTTTTTTT WW WW AA RRRRRRR EEEEEEEE **
** SS OO OO FF TT WW W WW AA AA RR RR EE **
** SSSSS OO OO FFFFF TT WW W WW AAAAAAAA RRRRRRR EEEEEE **
** SS OO OO FF TT WW W WW AA AA RR RR EE **
** SSSSSS OOOOOO FF TT WWWWW AA AA RR RR EEEEEEEE **
** **
*********** NOTICE ************************************************************
** This file contains valuable trade secrets and proprietary **
** assets of Windbase Software Inc. Embodying substantial **
** creative efforts and confidential information. Unauthorized **
** use, copying, decompiling, translating, disclosure or **
** transfer, of any kind, is strictly prohibited. **
** **
** COPYRIGHT (C) 1992, 1993, 1994. Windbase Software Inc. **
** ALL RIGHTS RESERVED. **
\*****************************************************************************/
#include <stdio.h>
#ifdef HAS_UNISTD_H
# include <unistd.h>
#else
# include <stdlib.h>
#endif
#include <string.h>
#include <malloc.h>
#include <ctype.h>
#define WBMEMTRACE
#include "../memsl.h"
#if defined(WBTRC_LEVEL1) || defined(WBTRC_LEVEL2)
#ifdef WBSTDC
int main(int argc, char **argv)
#else
int main(argc, argv)
int argc;
char **argv;
#endif
#else
#ifdef WBSTDC
int main(void)
#else
int main()
#endif
#endif
{
WBERROR *errhdl;
FILE *file;
char str[21], *strptr[201];
int i = 0, i2, ch;
WBTrcMainEntry();
memset(strptr,0,sizeof(strptr));
if ((errhdl = WBErrorOpen(0,"error.txt")) != NULL)
{
WBMallocErrH(errhdl);
if ((char *)getenv("WBMEMCHK") == NULL)
{
printf("\n\n************* WARNING *******************************\n\n");
printf("This program will crash, and likely crash your system because the two\n");
printf("environment variables are not defined. The environment variables are:\n\n");
printf(" WBMEMCHK - Turns memory checking on\n");
printf(" WBOVRCHK - Turns overwrite checking on\n\n");
printf("With the two environment variables defined, the 'memory.exe' program\n");
printf("will not crash, but will instead display information regarding the\n");
printf("improper use of memory. The above environment variables do not need\n");
printf("to be set to any particular value they just need to be set. The\n");
printf("following example will suffice for setting the environment variables:\n\n");
printf(" set WBMEMCHK=\"\"\n");
printf(" set WBOVRCHK=\"\"\n");
printf(" or\n");
printf(" WBMEMCHK=\"\"; export WBMEMCHK (for UNIX)\n");
printf(" WBOVRCHK=\"\"; export WBOVRCHK\n\n");
printf("Be sure to unset the 'WBOVRCHK' and 'WBMEMCHK' environment variables\n");
printf("when you are done with the memory demo. Leaving the 'WBOVRCHK' and\n");
printf("'WBMEMCHK' environment variable set will cause other MemSL demo\n");
printf("programs to run slowly.\n\n");
printf("Would you like to continue WITHOUT the environment\n");
printf("variables set (y/n)? ");
do { ch = getchar(); } while (strchr("yYnN",ch) == NULL);
if (strchr("nN",toupper(ch)))
{
printf("\n\nAborting...\n");
exit(0);
}
printf("Are you sure you want to continue without\n");
printf("memory checking (y/n)? ");
do { ch = getchar(); } while (strchr("yYnN",ch) == NULL);
if (strchr("nN",toupper(ch)))
{
printf("\n\nAborting...\n");
exit(0);
}
}
if ((file = fopen("data.dat","r")) != NULL)
{
while (i < 200 && fgets(str,20,file))
{
if (str[strlen(str)-1] == '\n')
str[strlen(str)-1] = 0;
if ((strptr[i] = malloc(strlen(str)+1)) != NULL)
strcpy(strptr[i],str);
i++;
}
fclose(file);
/* Starting at 1 should generate a still allocated error. */
/* Extra free should generate an error. */
for (i2 = 1; i2 < i+1; i2++)
free(strptr[i2]);
/*
** Common error of not including an extra byte for the
** NULL terminator ending a string.
*/
strptr[0] = calloc(1,9);
strcpy(strptr[0],"Overwrite");
free(strptr[0]);
/* Overwrite at beggining */
strptr[0] = malloc(9);
strcpy(strptr[0]-1,"Overwrite");
free(strptr[0]);
/* Zero allocation */
strptr[0] = malloc(0);
/* NULL Free */
free(NULL);
/* Normal call to realloc */
strptr[0] = realloc(NULL,10);
/*
** This call to realloc should generate
** a still allocated message.
*/
strptr[0] = realloc(strptr[0],20);
/*
** This realloc should generate both an
** invalid free and a still allocated message.
*/
strptr[0] = realloc(strptr[1],30);
}
}
if (getenv("WBMEMCHK"))
printf("\nThe error output can be found in 'error.txt'\n\n");
WBTrcReturn(0,0,("0"));
}