-
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!
-
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
-
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.
-
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.
-
make them dynamic instead of static... do you know how to use new or malloc to do this?
-
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.
-
strcpy them into it after you get memory?
-
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.
-
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
-
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.
-
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.
-
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
-
By megamadhu1 in forum Java
Replies: 1
Last Post: 02-03-2006, 07:29 AM
-
Replies: 0
Last Post: 10-30-2002, 04:40 AM
-
Replies: 0
Last Post: 11-02-2001, 04:22 PM
-
By Holm Puder in forum VB Classic
Replies: 6
Last Post: 04-26-2000, 04:23 AM
-
By John Grandy in forum VB Classic
Replies: 0
Last Post: 04-08-2000, 08: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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|