1

I can't figure out what's the problem with the following code, it just crashes without outputing anything to the screen:

#include <cstdlib>
#include <iostream>
using namespace std;

typedef struct {
    unsigned int recid;
    unsigned int num;
    char str[120];
    bool valid;
} record_t;

typedef struct {
    unsigned int blockid;
    unsigned int nreserved; 
    record_t entries[100];
    bool valid;
    unsigned char misc;
} block_t;

int main(){
    cout << "Before Buffer" << endl;
    block_t buffer[1000];
    cout << "After Buffer" << endl;
    return 0;
}

I tried Qt debugger and GBD and they just show segmentation fault and point at the start of the main function.

The size of each block_t element is 13,2 Kbs so the size of the buffer array should be around 13Mb. Maybe that's too much for a C-array?

3
  • If you think it is the size of the array, then have you tried smaller sizes? Commented Apr 29, 2013 at 1:57
  • I'm supposed to do external sorting on 100GB data given just this buffer I didn't think I could't store even 13MB in it... I must be missing sth... Commented Apr 29, 2013 at 2:01
  • 1
    Your array is probably bigger than your stack space. Use heap space (e.g. block_t** b = (block_t**)malloc(sizeof(block_t)*1000);). Commented Apr 29, 2013 at 2:03

2 Answers 2

3
block_t buffer[1000];

probably used all your stack space (requires larger than 1000* 100 *120 *1 Byte assume ASCII approximately equals 12MB, not considering other fields of those structs), therefore, you get a segmentation fault.

Try to use:

block_t * buffer = new block_t[1000];

or std::vector instead or increase your stack space to larger size if possible.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks didn't know these 2 had that much difference other than being able to allocate in runtime
2

Your buffer variable is about 13MB - too large for a stack allocation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.