Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory Problem?

Status
Not open for further replies.

gravity72

Programmer
Jun 11, 2005
3
0
0
US
I have looked over all the commands in the C language and tried many ways to structure this program, but I am having one heck of a time getting it to stop running out of memory.

What the program does: Asks you for n inputs. Then, for each input, you designate a character set. The program figures out every possible combination.

For example: on the first prompt, input a "3" for a string three characters long, then "press return". Then, for each character set, input a "1" and a "0" and press return. After the third character set is entered, the program will immediately start. It will give you unique instances for each possible outcome.

Now for my error, try putting in "12" for elements. It won't allow me to input my character sets.

What is going on? How do I make this program do large outcomes?

Here is my program, it's pretty short. I tried to write it tight as possible, so hopefully you find it clean and easy to understand, because I have no comments. ( If you really, really want comments, I will write them, but only if you need them.)

#include <stdio.h>

FILE *f;

struct s_base
{
int alphachar;
int omegachar;
int character;
} ex[25];

int carry = 0;

int unique = 0;
int v = 0;
int gp;
int e;

int emax = 1;

int g = 1;

void Print()
{
int i;

char c;

for( i = (e-1); i > -1; i-- )
{
fseek( f, ex.character, 0L );
fread( &c, 1, 1, f );
printf("%c", c );
}
g++;
printf(" ");
printf("%i ", g );
unique++;
return;
}

void Function()
{
char c;

if( ex[v].character == ex[v].omegachar )
{
ex[v].character = ex[v].alphachar;
v++;
carry += 1;
if( carry > 1 )
{
ex[v-1].character++;
carry = 1;
}
Function();
}

ex[v].character++;

fseek( f, ex[v].character, 0L );
fread( &c, 1, 1, f );

fseek( f, gp, 0L );
fwrite( &c, 1, 1, f );

gp++;

v = 0;

if( unique < emax )
{
if( !carry )
Print();
carry = 0;
Function();
}

return;
}

void Init()
{
int i;
int count = 0;

char s[100];

char c;

printf( "\nEnter Elements: " );
scanf( "%i", &e );

for( i = 0; i < e; i++ )
{
printf( "\nEnter Character Set %i: ", i );
scanf( "%s", s );

fwrite( &s, strlen(s), 1, f );
count += strlen(s);

ex.alphachar = ( count - strlen(s) ) - 1;
ex.omegachar = count - 1;
ex.character = count - strlen(s);

emax *= strlen(s);
}

gp = count;

for( i = (e-1); i > -1; i-- )
{
fseek( f, ex.alphachar + 1, 0L );
fread( &c, 1, 1, f );
printf("%c", c );
fseek( f, gp, 0l );
fwrite( &c, 1, 1, f );
gp++;
}
printf(" ");
unique++;
}

int main ()
{
f = fopen( "f", "w+" );

Init();
Function();
fclose(f);

return 0;
}

 
problem is in function ()

thats as far as I got so far.

I get kinda twisted with unformatted
code.

Code:
#include <stdio.h>

FILE *f;

struct s_base
{
	int alphachar;
	int omegachar;
	int character;
} ex[25];

int carry = 0;

int unique = 0;
int v = 0;
int gp;
int e;
int emax = 1;
int g = 1;

void Print()
{
	int i;

	char c;

	for( i = (e-1); i > -1; i-- )
	{
		fseek( f, ex[i].character, 0L );
		fread( &c, 1, 1, f );
		printf("%c", c );
	}

	g++;
	printf(" ");
	printf("%i ", g );
	unique++;
	return;
}

void Function()
{
	char c;

	if( ex[v].character == ex[v].omegachar )
	{
		ex[v].character = ex[v].alphachar;
		v++;
		carry += 1;

		if( carry > 1 )
		{
			ex[v-1].character++;
			carry = 1;
      }

		Function();
	}

	ex[v].character++;

	fseek( f, ex[v].character, 0L );
	fread( &c, 1, 1, f );

	fseek( f, gp, 0L );
	fwrite( &c, 1, 1, f );

	gp++;

	v = 0;

	if( unique < emax )
	{
		if( !carry )
			Print();
		carry = 0;
		Function();
	}

	return;
}

void Init()
{
	int i;
	int count = 0;

	char s[100];

	char c;

	printf( "\nEnter Elements: " );
	scanf( "%i", &e );

	for( i = 0; i < e; i++ )
	{
		printf( "\nEnter Character Set %i: ", i );
		scanf( "%s", s );

		fwrite( &s, strlen(s), 1, f );
		count += strlen(s);

		ex[i].alphachar = ( count - strlen(s) ) - 1;
		ex[i].omegachar = count - 1;
		ex[i].character = count - strlen(s);

		emax *= strlen(s);
	}

	gp = count;

	for( i = (e-1); i > -1; i-- )
	{
		fseek( f, ex[i].alphachar + 1, 0L );
		fread( &c, 1, 1, f );
		printf("%c", c );
		fseek( f, gp, 0l );
		fwrite( &c, 1, 1, f );
		gp++;
	}

	printf(" ");
	unique++;
}

int main ()
{
	f = fopen( "f", "w+" );

   printf("init/n");
	Init();
   printf("Function/n");
	//Function();
	fclose(f);

   getchar();

	return 0;
}
 
I took this code out and the crash
stopped. to save me some time Ill
ask what is the purpose of this code.

if( unique < emax )
{
if( !carry )
Print();
carry = 0;
Function();
}
 
How do you make the formatting look nice?

Thanks for your comments. I figured it out how not to make it crash, but I don't know why it doesn't crash now. Although it has to do with:

I'm calling Function() from inside itself. I.e.:

void Function()
{
if( 1 )
Function();

return();
}

Each call to Function() is never finishing, because of a new call to itself. I think the problem is that it is allocating variables for each new occurence of Function(). Each allocation of memory for Function() builds up in memory. Once the maximum amount of memory is reached, the program crashes.

Is this right? Does this have something to do with heap?
 
Function as defined in your last post is obviously recursive.
Any recursive function that has no way to limit the recursion will hammer every bit of memory available on any machine.
I have to ask a dumb question here. What the hell is that supposed to do?


Trojan.

 
... it's stack, not heap (probably). All functions take stack space for their local variables and return addresses, and stack is much smaller than heap. Recursive functions pile up more and more return addresses and variables on the stack, a new set for each recursion.

Recursive things need a limit condition that stops them going on for ever.
 
How do you make the formatting look nice?

lots of white space and tab for indentation.

I really do have a hard time reading code that
I did not write simply because of the formatting
issue.

tomcruz.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top