Find the answer to your Linux question:
Results 1 to 7 of 7
Hi! I am facing problems here... This is the coding that i compile in linux terminal : Code: #include <stdio.h> void *mymalloc(size_t request); int main() { char * test; int ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2006
    Posts
    4

    Help needed in segmentation fault


    Hi! I am facing problems here... This is the coding that i compile in linux terminal :
    Code:
    #include <stdio.h>
    void *mymalloc(size_t request);
    int main()
    {
           char * test;
           int i = 0;
           test = (char *)mymalloc(sizeof(char) * 6);
           if (test == NULL)
           {    
                  printf("Malloc failed\n");
                  exit(1);
           }
           while(1)
           {
                     test[i] = 'a';
                     printf("%d bytes written to memory\n", i+1);
                     i++;
           }
           return 0;
    }
    void * mymalloc(size_t request)
    {
           return sbrk(request);
    }
    and this is my output:
    2565 bytes written to memory
    2566 bytes written to memory
    2567 bytes written to memory
    2568 bytes written to memory
    Segmentation fault

    Can anyone explain to me what's actually happenning? If want to improve, how?? Thanks a lot for your help!!!

    Mod edit - Added code tags
    Last edited by Dolda2000; 02-15-2006 at 05:15 PM.

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    What were you expecting, exactly? Since you're allocating 6 bytes, I don't find it particularly strange that you get segmentation faults when you write more than that (even so much as 2568 bytes).

  3. #3
    Just Joined!
    Join Date
    Jan 2006
    Location
    India
    Posts
    52
    Hi,

    The 'sbrk()' system call will alway allocate the memory in terms of memory chunk or blocks of memory,which could be 1KiB,or 4KiB depending on the configuration.

    So when u request for only 6 bytes of memory the kernel inturns allocates 1 data block of memory.
    Hence the kernel allows you to access till max value of the block size,In my system it is 4KiB(4096 bytes). If you try to access the memory above this limit the kernel finds that the process is accessing the illegal memory address, hence
    kills the process.

    -- rajesh

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Feb 2006
    Posts
    4

    Thanks!!!

    Hi! Thanks a lot!
    If i want to improve this program (means that no segmentation fault), how shoud i modify this program? Thanks!!!

  6. #5
    Just Joined!
    Join Date
    Jan 2006
    Location
    India
    Posts
    52
    Please check out for the block size of your system.

    I am looking for how to find the blcok size of the system.

    Once you find the block size then in your programm do a limit chek on memory:

    while(1)
    {
    if ( i < BLK_SIZE ) {
    test[i] = 'a';
    printf("%d bytes written to memory\n", i+1);
    i++;
    }
    else {
    prinhtf("Memory limit reached\n");
    break;
    }
    }

  7. #6
    Just Joined!
    Join Date
    Dec 2005
    Posts
    30
    if you have allocated space for n characters, then the loop should iterate for only n times. so the loop condition should be
    while(i<n){

    blah.. blah ..

    i++;
    }

    in your example it should be

    while(i<6){
    blah .. blah ..
    i++;
    }

  8. #7
    Just Joined!
    Join Date
    Feb 2006
    Posts
    1

    Red face

    hello!!! i have a same problem with babycorn...
    i have a question here....
    what we have to modified if we want improve the mymalloc function ??
    i have modified babycorn mymalloc funtion a little bit ....but i still need ur advise.... how to improve more???
    here are my code.....
    Code:
    void * mymalloc(size_t request)
    {
         request = sbrk(0);
    }
    the output i get is different

    2462 bytes written to memory
    2463 bytes written to memory
    2464 bytes written to memory
    Segmentation fault

    thanks all!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •