Page 1 of 1

Wierd all of the sudden

Posted: Mon Oct 18, 2004 7:15 pm
by steve
This is my first post here, seems like a great site. Anyways, I have a basic kernel that handles interrupts and is starting to use paging. I'm starting to write my free function so i decided to test some things. Here's by main():

Code: Select all

    clear();
              
    interrupts();  
    sti();    
       
    tm=time_init();        
    paging_init();

    printk("startup date: %2d/%2d/%2d\n", tm.day, tm.month,   tm.year);
    printk("startup time: %2d:%2d:%2d\n\n", tm.hour, tm.min, tm.sec);
    
    char *test;
    char *test1;
    
    //printk("Location of `test': %x at initialization\n", test); 
    test = (char *)malloc(sizeof(char));    
    //printk("Location of `test': %x after malloc\n", test);
    test = "Hello World!";
    //printk("Location of `test': %x after assignment\n\n", test);
    //printk("Location of `test1': %x at initialization\n", test1);
    test1 = (char *)malloc(sizeof(char));  
    //printk("Location of `test1': %x after malloc\n", test1);
    test = "I'm Alive\n";
    //printk("Location of `test1': %x after assignment\n\n", test1); 
great, works like that. Now i uncomment the first printk, works, 2nd, works ... anyways when you uncomment the 6th one, bochs goes into spasms and says "load_seg_reg: GDT: SS: index(0015*8+7) > limit(000017)" I know it's not anything to do with the real code because i take that out and it works (I mean still produces the error). I look at my GDT and everything seems to be fine. What could be the problem? Everything worked before i put in the stuff with test and test1.

Re:Wierd all of the sudden

Posted: Mon Oct 18, 2004 7:49 pm
by Curufir

Code: Select all

[quote="steve"]
    test = (char *)malloc(sizeof(char));    
    test = "Hello World!";
These lines look kinda odd. You allocate space for 1 char, then use it for a string that's 13 bytes long. Chances are you've trashed your stack somewhere along the line.[/quote]

Re:Wierd all of the sudden

Posted: Mon Oct 18, 2004 7:58 pm
by steve
heh, well i changed it. Still doesn't work though. I even took out the mallocs and it still doesn't. The only way i can get it too work is by commenting out the last printk :-\

Re:Wierd all of the sudden

Posted: Mon Oct 18, 2004 11:58 pm
by distantvoices
maybe your boot loader doesn't fetch all the blocks of your kernel into memory?

Looks like the image is cut off, and then, sweet gosh almighty, hell's stampede is on it's road in paradise.

Re:Wierd all of the sudden

Posted: Tue Oct 19, 2004 12:09 am
by steve
Well there's my problem. My kernel is 9.2 kb, and i was only loading 9 kb of it. Can't believe I did that. Thanks for the help guys.

Re:Wierd all of the sudden

Posted: Tue Oct 19, 2004 3:55 am
by Pype.Clicker
by the way, doing

Code: Select all

char *test=malloc(13);
test="Hello World";
never copied "hello world" into the freshly-allocated memory in C and it will never do.
Instead, the "test" pointer now points at the constant string and the memory you malloc'ated is now lost (e.g. you can't tell where it is any longer)

Oh, and btw, you should stay away from standard-lib names for your functions ... i rather suggest "kalloc" or "kmalloc" or "kmem_alloc", but "malloc" will simply confuse anyone working with you later :P

Re:Wierd all of the sudden

Posted: Tue Oct 19, 2004 8:14 pm
by steve
Thanks for the tips Pype. I'm now using kalloc, kfree and strcopy for assigning the variables.