composing a string

Programming, for all ages and all languages.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

If you're programming this in your own OS (A lot of the replies earlier assumed this was in a hosted environment), then you should DEFINATELY use static allocation with a max size, thus:

Code: Select all

 
unsigned int gets(char *buffer, unsigned int max_length) 
{ 
  unsigned int sz = 0; 
  while( (*buffer++=getc()) != '\n' && 
         sz < max_length-1) 
    sz++; 
  *--buffer  = '\0'; 
  return sz; 
} 

... in main.c ... 

char string[512];
gets(string, 512); 
JamesM
mohammed
Member
Member
Posts: 93
Joined: Mon Jan 30, 2006 12:00 am
Location: Mansoura, Egypt

Post by mohammed »

it worked fine thank you but i am going to attach it to the IRQ when the user hit a key the handler search for what function is in use
because i still have problem with this
while (! (inportb(0x64) & 0x1) ) ;
my speed on the keyboard affect what appears on the screen
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

JamesM wrote:If you're programming this in your own OS (A lot of the replies earlier assumed this was in a hosted environment), then you should DEFINATELY use static allocation with a max size
Where did you get that logic from? If you have dynamic allocation, use it. If you do not, then use static allocation.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

But he doesn't have dynamic allocation in his OS yet, and he seems to get confused easily...
Post Reply