0

I keep getting segfalut while trying to create a array, which size is 10000000.

unsigned char anal1[10000000];

do I have to malloc for such huge variables?

4
  • 1
    unsinged char *anal1 = malloc(10000000); Commented Feb 11, 2014 at 17:04
  • I know, but i prefer stack. Commented Feb 11, 2014 at 17:04
  • Specifies the stack size in the compiler, if any. Commented Feb 11, 2014 at 17:06
  • 6
    Too bad. Trying to take 10 MB off the stack is just a horrible idea. Get used to Heap memory. I also recommend against calling your variables anal (unless you work for a proctologist). Commented Feb 11, 2014 at 17:06

1 Answer 1

1

Option #1 - a local variable - the stack must be large enough to accommodate it:

void func(...)
{
    unsigned char anal1[10000000];
    ...
}

Option #2 - a static local variable - the data-section must be large enough to accommodate it:

void func(...)
{
    static unsigned char anal1[10000000];
    ...
}

Option #3 - a global variable - the data-section must be large enough to accommodate it:

unsigned char anal1[10000000];

Option #4 - a static global variable - the data-section must be large enough to accommodate it:

static unsigned char anal1[10000000];

Option #5 - if you allocate it during run-time, then the heap must be large enough to accommodate it:

unsigned char* anal1 = malloc(10000000);

Whichever option you choose, you will probably need to assert the corresponding requirement within your project settings (not sure what IDE you're using, so cannot tell you exactly how to configure that).

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

2 Comments

What's data-section in Option #2?
global variables or local-static variables (a static variable inside a function, which I have not mentioned in the answer above)... now added (and moved options 2-3 to 3-4)

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.