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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

no errors but crashes

Status
Not open for further replies.

ascikey

Programmer
Feb 18, 2004
127
0
0
GB
when i call the following function i get the windows error message saying program needs to close bla bla, can anybody tell me why this could be? the compiler says 0 errors and 0 warnings, if i dont call this function all works well. i am new 2 c, so could be something silly.
Code:
void trackMemory(int m)
 
{
	fibsize += m;
	if (fibsize>60000)
		printf("%s%dthis number is too big ", fibsize);

	if (fibsize<60000)
		printf("%s%dthis number is not too big ", fibsize);
	
}
 
The second argument of printf should be a char *, since you have a %s first. The value of fibsize looks like it is just a number, so that when printf tries to access the address "pointed to" by fibsize, it accesses random memory.

Vincent
 
Yes, %s specifier in your format is redundant. Add blank after %d. How about:
Code:
printf("%d this number is %stoo big ", fibsize,
        fibsize<60000?"not ":"");
if (fibsize == 60000?;)...
It's not the best idea to modify global fibsize var in the function. Avoid using magic numbers such as 60000 in your codes. Try const int maxsize = 60000 or what else...
 
thanks for your replies i will alter the format of the if statement. I think i need a few pointers here on what you are saying though. the idea of the function was to modify a global variable that was used to keep track of the memory size consumed in a different recursive function so how is this best done? also ArkM you call 60000 a magic number could you please explain magic number and what is the reason for putting %s before the too which i thought was inside a string literal
thx again
 
I cant tell why that worked and mine did not?
and using this method how can i make the program terminate if it is too big. I sort of understand that the %s allows a swap with the word not (is that right)
thanks


[atom]
 
We are in C++ (don't you?;):
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
// Only this class dcl need for your code
class MemGuard
{
static long fibsize;
static long maxsize;
public:
	MemGuard(long maxsz = 60000) { maxsize = maxsz; }
	class Crash {}; // Memory level exception.
static long add(long m);
static long curr() { return fibsize; }
}; // Add other useful members if you wish

// Place this MemGuard implementation in the separate file.
long MemGuard::maxsize = 60000;
long MemGuard::fibsize = 0;

long MemGuard::add(long m)
{
   fibsize += m;
   if (fibsize > maxsize)
   {
      throw MemGuard::Crash();
   }
   return fibsize;
}
// For example only...
int main()
{
  MemGuard::add(10000); // OK
  cout << MemGuard::curr() << endl;
  try
  {
    MemGuard::add(100000);
  }
  catch (MemGuard::Crash)
  {
    cout << "Mem " << MemGuard::curr() << endl;
    exit(1); // Let's die...
  }
  return 0;
}
See as static member functions play as (safety;) globals...
See as STL function exit() terminates your program immediatly...

Magic numbers is a motto from good old times: any constant except 0, 1 (may be 2 too;) placed directly in your code.
Yes, %s in my format spec is a placeholder for "not" or ""...

If you don't want any classes (in C, for example) it's more better to place local static fibsize in the trackMemory() body. What for global var in that case? Let me confide a secret to you: I never used global vars last 20 (or 30;) years (in 10 to 300000 source lines projects;)...
Good luck!
 
thanks for your help you have given me plenty to think about
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top