Memory problem with Borland C3.1


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: Memory problem with Borland C3.1

Hybrid View

  1. #1
    Join Date
    Nov 2007
    Posts
    6

    Memory problem with Borland C3.1

    HiÖ.
    Sorry for my English and for my question but Iíve very big problem!!

    This is my first question in this forum, but Iíve a very big problem. Iím writing a code for Borland C 3.1 that has a function with the following prototype:

    Void Retrun_string (int byte1, int byte2, char *ptr)

    The function take in input the 2 byte and wrote, in output, to a char pointed by ptr pointer a string:

    Char *other_string[25];

    My problem is that in this fuction there are lots of string allocation (more than 64K), all string vector are of the type:

    const char *string[] = {
    ďstring1Ē,
    ďstring2Ē,
    ÖÖ
    ďstringNĒ,
    }

    Iíve lots of this vector because I need to make lots of correspondence between the two bytes and the string. And I use this way for making an easier:

    Strcpy (ptr, string[n]);

    The problem is that I must work in a windows 98 space with a Borland c 3.1 (1991). I found on the web that with this kind of workspace there is lots of memory problem because this compiler (that works in MSdos emulation on win98) doesnít allowed more than 64k of memory. When I run my program Iíve a runtime error in the memory that every times changed and is not periodic but very random. Sometimes my program run without problems, some times all the system crashed, like a MATRIX screensaver. I note that I wrote (random) in lots of parts of memory system for example so0metimes ďIĒ wrote in the keyboard memory, or in the video memory or in the system memory. If Iím lucky the system tell me that Iím trying to access to a reserve and critical part of the memory.

    I canít use an extern file for downloading on runtime the needed string. If I run the program with out my fuction (all the fuction commented) Iíve not problems, nothing. If I run with the entire string vector commented Iíve not running problem. I think the problem are the strings!

    When I run my executable file on a winxp workstation Iíve fewer problemsÖ but I must work on 98 systemsÖ. I tried to working with out a pointer in my fuction, but Iíve the same problems. I tried changing with this other fuction:


    Static char string2[25];
    Retrun_string (int byte1, int byte2)
    {
    Ö.
    Strcpy (string2, string[n]);
    }

    I found on te web lots of people with memory troubles in Borland C, very similar to mine. Lots of people migrate to Borland 5 or made a file where put all the string, but I canít!

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    You can allocate the memory from the heap, using malloc, calloc or new[] and thus be rid of the small stack size. Alternatively, try to optimize storage usage. For example, instead of storing strings such as
    "string1"
    "string2"
    etc. simply store the values 1,2...N in an array and then concatenate "string" with that numeric value (after converting it to a string of course).
    BTW, are you sure that your code is completely bug free? This could also be the cause of those erratic freezes and crashes.
    Danny Kalev

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    You also have to avoid "dos memory hacks" -- in dos it was acceptable to write directly to the video card memory, to system memory, to anythign anywhere at any time. Modern windows thinks you are a virus if you do this and denys it -- you can only write to things that you own or have rights to. Therefore many old example programs for 3.1 are invalid and care must be taken to create working programs.

  4. #4
    Join Date
    Nov 2007
    Posts
    6
    I know that if i can i have to avoid dos memory hacks, but i don't know how to allowed all this strings! Becouse i cant use an external file... I've put inside my program lots of strings.

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    make them dynamic instead of static... do you know how to use new or malloc to do this?

  6. #6
    Join Date
    Nov 2007
    Posts
    6
    yes i know how using malloc but i must put inside the code this my string.. Becouse i need to put, before running, my string inside the code!

    For example if i must put in the code this 3 string:

    char string[]={"Pippo", "Paperino", "Topolino"};

    i can made

    ptr=malloc(sizeof(string));

    and after made the copy of the entire buffer in the memory pointed by ptr. It's ok, but where i can put, before made malloc, my string pippo, paperino and topolino??

    That's why i don't use malloc.

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    strcpy them into it after you get memory?

  8. #8
    Join Date
    Nov 2007
    Posts
    6
    Ok, but i "get memory" also when i wrote this:

    char string[]={"Pippo", "Paperino", "Topolino"};

    and there is the problem.. becouse i've more than 2000 strings to put inside, and i've not space for all this.

  9. #9
    Join Date
    Nov 2003
    Posts
    4,118
    These strings are allocated on the stack memory:

    char string[]={"Pippo", "Paperino", "Topolino"};

    And that's the problem. You need to replace these strings with heap allocated memory. The problem is that your initializers also occupy memory so it's best to store the strings in a file or leave the strings uninitialized.
    Danny Kalev

  10. #10
    Join Date
    Nov 2007
    Posts
    6
    I guessed to have this problem, but i've not the possibility to acces to an external file. i would like to know if you have any other solution for helping me.. Becouse i could work for beeing my program able to read an external file, but i've lots of work to to in this direction. Becouse in the end my .exe file will be executed in an other close space by an other executable file that i don't know whats done.

  11. #11
    Join Date
    Dec 2003
    Posts
    3,366
    Just replace it with the dynamic memory and copy functions instead of the stack memory and it will work fine without the files.

    char ** stringlist;
    stringlist = new char*[numstrings];
    for(i = 0; i < numstrings; i++)
    stringlist[i] = new char[max_string_size];

    ...
    strcpy(stringlist[0], "whatever");
    strcpy(stringlist[1], "billybob");
    ...

    etc.

  12. #12
    Join Date
    Nov 2007
    Posts
    6
    I found what was the problem...

    My fuction and all my part of cade was called by an interrupt fuction. And this was the problem, becouse in the old machine and especially with my old compiler is very dangerous to call a fuction (a big fuction like mine) with lots of memory allocated and with lots of matematics operation.. But was not easy to found that my code was called by an interrupt fuction!

Similar Threads

  1. Memory problem while printing png images
    By megamadhu1 in forum Java
    Replies: 1
    Last Post: 02-03-2006, 08:29 AM
  2. Reliability Problem
    By elise in forum Java
    Replies: 0
    Last Post: 10-30-2002, 05:40 AM
  3. help memory
    By kathy in forum Java
    Replies: 0
    Last Post: 11-02-2001, 05:22 PM
  4. DAO 3032 + 2GByte Memory -> Problem ?
    By Holm Puder in forum VB Classic
    Replies: 6
    Last Post: 04-26-2000, 05:23 AM
  5. Re: MS Provider For Oracle : Memory Leak ?
    By John Grandy in forum VB Classic
    Replies: 0
    Last Post: 04-08-2000, 09:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center