Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Psycho_68

size of int* ??

This topic is 5585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I got bored so i made this program that clears up system memmory, it works realy well and clers 224MB out of my 256. It creates a lot of int* on the heap/free store. The only problem is that an int* should be 4 bytes but the program seems to be eating up memmory a lot faster, about 8 times faster, weird, heres the code:
#include <iostream.h>

int main()
{
	int* stand = 0;
	
	int i = 0;
	int megcount = 0;

	while(1)
	{
		stand = new int;
		i++;
		if(i==(256*1024)
		{
			i=0;
			megcount++;

			cout << megcount << "\n";
			cout << flush;
		}
		if(stand==0)
		{
			cout << "cleared " << megcount << " MB of memmory ;)\n";
			return 0;
		}
	}

	return 0;
}
anybody know why/what did i do wrong? [edited by - Psycho_68 on July 28, 2003 4:51:34 AM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
didn''t look at your code, but be aware that there''s a certain overhead in allocating data as the heap manager keeps track of the size of each allocated block. Also, when you allocate many small blocks like this, your memory will be fragmented and it''s unlikely that you can allocate as much as you wish. When allocating an int, it''s also possible that you will get more than just 4 bytes, your only guarantee is that it''s *at least* sizeof(int) bytes, but you could get more.

Share this post


Link to post
Share on other sites
holy shit

that is the biggest abomination of a program i've ever seen

edit: you realize thats basically a 224 meg memory leak? and done 4 bytes at a time? not only do you leak all your memory, you fragment it to hell, if you weren't using a modern os that cleans up after your process...

[edited by - billybob on July 28, 2003 5:49:42 AM]

Share this post


Link to post
Share on other sites
heh my initial plan for the program was to crash the system

yes its a memmory leak, but when the program stops, I get my memmory back

i have win 98 and ive run the program about 10 times 2 hours ago and my system hasnt crashed yet

Q: is there some way how not to fragment it?

Share this post


Link to post
Share on other sites
oh in that case, ist not so bad
allocate in 1 k-meg blocks, (stand = new int[1024] for a kilobyte)

saying 224/256 allocated isn''t exactly accurate, since the os might have swapped your memory/some other program''s memory to make room for it.

you should make it so it allocates in 1 meg blocks until new returns null, then 1 k blocks until new returns null, then 512 maybe, or 256, reduce the size again, until one byte at a time.

Share this post


Link to post
Share on other sites
quote:
Original post by mputters
quote:
Original post by billybob
(stand = new int[1024] for a kilobyte)



Ehm an int is rarely one byte

Heh, of course. same idea though, can''t believe I said that, I''m getting retardeder.

Share this post


Link to post
Share on other sites
quote:
Original post by billybob
that is the biggest abomination of a program i''ve ever seen

edit: you realize thats basically a 224 meg memory leak?
The iostream.h was more of an abomination.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!