Thread: Out of memory already

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    161

    Out of memory already

    I programed my program to give an "out of memory" message when ever it cannot find more memory to store a string. In debeg mode I recieved an out of memory message very quickly. I don't think I used 2k yet. I have 512 megabytes and over 400mg free. With all that testing, createing variables, forgetting to delete them, I have now over 200mg. That is still alot. Why when I use the new char[len] command it returns NULL with all that memory available?

    Thanx in advance!

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    possibly fragmentation of the process heap stops an allocation of that particular size.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    Then how does other Text editers do this. Others can hold larger strings. What I do is first alocate a bunch of bytes about 100. If the amount of memory I need exeeds that I go look for another 100.

    What do Text editers do when allocating memory to hold strings. Most text editers use BSTR witch is 2 bytes instead of 1 as char.


    Thanx in advance!

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Are you working in the console?

    Console programs (in Windows at least) have heavy restrictions on their memory usage.

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    No im working with win32. I'm creating a text editor todo syntax coloring. I got the printing to work. And text editing, Just need to fix the memory things. If someone can tell me how other text editers like the edit control do this that would be great.

    Thanx in advance!

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You'll have to post some code. Are you sure you are not allocating in a loop or something? There should be no problem allocating the piddly amount of memory needed for a text editor on the machine you specify.

    How much memory does Task Manager report that your process is using? (Ctrl+Alt+Del --> Task Manager --> Processes)

    What compiler are you using?

    With all that testing, createing variables, forgetting to delete them, I have now over 200mg

    Could you clarify this sentence. Are you saying that your program is using 200MB of memory?

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If you are requesting memory from Windows via the API, it sounds like you have not released a previous memory object and thus have corrupted the memory.

    What are you using to allocate memory? Windows or malloc()/new?

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    I am using MSVC++ 6 and VB 6 to test my dll.
    >>Could you clarify this sentence. Are you saying that your program is using 200MB of memory?

    No its not using so much memory, Because my program sometimes crahses it doesn't get a chance to delete my variables thus they are still in memory. I had 200mg left for storage.

    this is my macrodefinition. It is the minimum to search for in memory;
    #define STR_ADD 30

    This next part is the initialization of the variable.
    Code:
    	Text.string   = new char[STR_ADD];
    	Text.count    = STR_ADD;
    	Text.len      = 2;
    	Text.string[0] = (char) 13;
        Text.string[1] = (char) 10;
    This is the part when I search for more memory. It is in a function and "by" is a variable of the amount to increase;

    Code:
    	char *temp;
    	temp = new char[Text.count + by];
    	if(temp == NULL){msgbox("Out of Memory on While Resizing Text"); return OUT_OF_MEMORY;}
    	memcpy(temp, Text.string, Text.len +1); // move everything to new space
    	delete [] Text.string; // delete the old space
    	Text.string = temp; // assigne it to new space
    	Text.count = Text.count + by;
    	int put =  Text.string - Lines.lpLines[0];
    	int i;
    The only time the text size is resized is when there is more needed. If the user using my text editor deletes text, the text size is allocated for storage is not reallocated.

    I pushed CTRL+ALT+DEL while my program was running and the "out of memory" message popuped up. I it was using up 3mg. Maby Iam not using a good method for allocating memory for text. Any Idea's are welcome,

    Thanx in advance!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  5. What's the best memory (RAM) type?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 12-15-2001, 12:37 AM