Thread: Help on Segmentation Fault!!!

  1. #1
    Registered User
    Join Date
    Sep 2005

    Talking Help on Segmentation Fault!!!

    i am new user of linux programming!!
    i need ur help!!

    #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");
                     test[i] = 'a';
                     printf("%d bytes written to memory\n", i+1);
           return 0;
    void * mymalloc(size_t request)
           return sbrk(request);
    the output is:
    2565 bytes written to memory
    2566 bytes written to memory
    2567 bytes written to memory
    2568 bytes written to memory
    Segmentation fault

    my question is wat is going on this code???can anyone explain to me ??? y izzit segmentation fault after 2568 bytes ???

    another question is wat should i do if i want to improve the mymalloc() function?

    thanks all!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Another question is why doesn't it segfault after 6 bytes?
    That's the problem with undefined behaviour. Code which should by all rights crash and burn magically keeps going. This is what makes finding some bugs really tricky.

    > y izzit

  3. #3
    Registered User
    Join Date
    Oct 2005
    Salem, I think u havent answer this person's question yet. giving link wouldn't realy help... cos i think that "that" link is not clear. i dont even know wat is it. it to complicated for beginner to understand.
    try to gives a more simple to understand tutorials or direct answer.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    And I think "u" should read the same FAQ as well.

  5. #5
    Registered User
    Join Date
    Sep 2004
    It partially has to do with Operating System specific functionallity.

    It could be dealing with virtual memeory, page sizes, and trying to access memory that isn't yours (hence the seg fault).

    Also, on error, sbrk doesn't return NULL, it returns -1. Don't forget to read your man pages.

    Also, I don't think sbrk was ment to be a replacment for malloc, as its not even a system call, at least in my man page for it, it states " brk() and sbrk() are not defined in the C Standard and are deliberately excluded from the POSIX.1 standard (see paragraphs B. and B.8.3.3)." and was simply brought over from 4.3 BSD.
    Last edited by Xipher; 02-19-2006 at 11:36 AM.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM