![]() |
malloc?
Does MPLAB not support the malloc() family, or am I just missing a library?
|
Re: malloc?
Quote:
|
Re: malloc?
What I'm writing is just a terminal emulator, to work with the serial ports. What it needs to do, though, is let the rest of the program continue to run in the background. For example.....
Code:
Terminal_Print("Press a key: ");After going through a long thought process of how to let this happen, without having the print statements re-print during each loop, and for each of the read statements to only read once, the solution is twofold. First, there's a global variable that is 1 if any terminal function is waiting on input, and 0 otherwise. Built into each print statement is a simple check, so that the statement is only executed if the waiting flag is 0. This works out so that the flag is always 1 at print statements that have already been printed, and it is also 1 at any statements following the point where it's waiting for input. However, to keep read statements that have already read input from reading again, I need a global array of bools, each representing one of the calls to Terminal_Read(). And within Terminal_Read() I call a handler that looks at and modifies this array to determine whether that particular call of the function is the one that should be reading the input. Yes, I could certainly allocate statically, but it would make things a lot easier for me and for other teammates with very little programming experience to not have to redefine the array each time you make add or remove a read statement. Or, I could simply allocate an array of 1000 elements, since I probably would never need that many, but there's already not a whole lot of memory on a PIC. I think I'll just go with a "Specify how many times you called Read()" approach. After all, it does get to a certain point where you don't change your terminal too much. I'll post it all here when I'm done. |
Re: malloc?
There's gotta be a moderately automated way to do this.
One way I can think of to make the array-resizing a bit easier would be to do something like this: Code:
enum ReadReasons (CAMERA_X,CAMERA_Y,CAMERA_Z,CAMERA_SPEED,TURN_SPEED,REASON_COUNT);I keep thinking you could also use the preprocessor to do this, because having to do dynamic allocation for something that you know the needed size of at compile-time just sets of alarm bells in my head. |
Re: malloc?
Hmm.....ingenious. I may well have to use that.
Preprocessor was exactly what I was thinking. When I get time, I'll bet there's a #pragma or something similar that can do the job. My original idea was to duplicate a basic DOS-Style console app, from the programmer's perspective; printf, scanf, etc; very flexible. And essentially, that's what it's turned out to be, even to the point that my version of scanf takes a pointet to where the data is stored as an argument, rather than returning it, which was not originally intended. The only slight difficulty is that you have to #define the number of read calls you make. But I figure, just leave it at 100 or so until you've got the terminal communication part finalized. Plus, there may yet be a preprocessor way to do this. Gimme a day or two to comment it all and such and I'll post it up here. I'm sure someone will find a uses for it. |
Re: malloc?
I believe what you're looking for is a buffer. You statically allocate a char array, and have an index showing the next place to write a character.
Also, if you want to get fancy, you can loop the buffer around--a ring buffer. So when you reach the end of the buffer, you start at the beginning. This is very eficiently done when your buffer size is a power of two. (index &= 0x1FF) Just some thoughts. |
| All times are GMT -5. The time now is 00:15. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi