segmentation fault, memory problem ?

Discussion in 'C++' started by vfunc@talktalk.net, Sep 20, 2006.

  1. Guest

    I have a function "test" in a seperate file to main and a segmentation
    fault seems to occur before the first line of the test function is
    executed.

    void test()
    {
    DEBUG_LOG("in test");
    maca c;
    ....
    }

    I am guessing that there is something wrong with maca, which is an
    class based on an array of another class which is also based on an
    array. O(64x10000) elements altogether. Maybe I need to allocate some
    memory in the compilation, or somewhere else ? I can post more details
    if neccessary
    , Sep 20, 2006
    #1
    1. Advertising

  2. S S Guest

    wrote:
    > I have a function "test" in a seperate file to main and a segmentation
    > fault seems to occur before the first line of the test function is
    > executed.
    >
    > void test()
    > {
    > DEBUG_LOG("in test");
    > maca c;
    > ...
    > }
    >
    > I am guessing that there is something wrong with maca, which is an
    > class based on an array of another class which is also based on an
    > array. O(64x10000) elements altogether. Maybe I need to allocate some
    > memory in the compilation, or somewhere else ? I can post more details
    > if neccessary


    If you can replicate it in small code and send us the code would be of
    help to debug.
    S S, Sep 20, 2006
    #2
    1. Advertising

  3. Guest

    I reduced the size of the array from 100,000 to 1,000 and the fault
    disappeared. However I may need to up that new value. So my queston
    is how do I reserve a chunk of memory, with g++ directive or someother
    way ?
    , Sep 20, 2006
    #3
  4. wrote:
    > I reduced the size of the array from 100,000 to 1,000 and the fault
    > disappeared. However I may need to up that new value. So my queston
    > is how do I reserve a chunk of memory, with g++ directive or someother
    > way ?


    If your array is local, that is probably a stack overflow error.
    Try "ulimit -s" and look at the value returned (your maximum
    allowed stack size). Use "ulimit" to increase it, or get rid of
    the local variable altogether (by allocating it dynamically via new[]).

    HTH,
    - J.
    Jacek Dziedzic, Sep 20, 2006
    #4
  5. Jacek Dziedzic schrieb:
    > [stack problem description]
    >
    > If your array is local, that is probably a stack overflow error.
    > Try "ulimit -s" and look at the value returned (your maximum
    > allowed stack size). Use "ulimit" to increase it, or get rid of
    > the local variable altogether (by allocating it dynamically via new[]).
    >


    The OP may also consider using std::vector -- then he gets the memory
    managment and exception safety for free, and he will have to change less
    code than with manual deletes all over the place.

    best regards,
    -- Markus

    > HTH,
    > - J.
    Markus Grueneis, Sep 20, 2006
    #5
  6. Guest

    Can't I tell the compiler to allow more memory ? Why do I have to
    program it ?
    , Sep 20, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vishal Grover
    Replies:
    10
    Views:
    541
    Vishal Grover
    Sep 11, 2003
  2. Hon Seng Phuah

    Free memory cause segmentation fault.

    Hon Seng Phuah, Jul 7, 2004, in forum: C Programming
    Replies:
    8
    Views:
    623
    Old Wolf
    Jul 7, 2004
  3. Sameer
    Replies:
    19
    Views:
    465
  4. Replies:
    21
    Views:
    893
    Peter 'Shaggy' Haywood
    May 13, 2007
  5. Ram
    Replies:
    4
    Views:
    734
Loading...

Share This Page