0

I'm really new to C and something is bugging me...

I declared a typedef:

    typedef struct{
        double
            real,
            img;
    }complex;

And, inside a function, I declared the following array:

    complex system[MAX_NODES+1][MAX_NODES+2];

The first thing the function does with the elements of this array is initializing them, but the application would compile and crash during runtime, UNLESS another array with the same dimensions and type was also declared (even though it wasn't used):

    complex system1[MAX_NODES+1][MAX_NODES+2],
    complex system[MAX_NODES+1][MAX_NODES+2];

This led me to believe it was a memory problem, and maybe the first system was allocating the memory that was needed... Is that the case? If so, why?

MAX_NODES is a constant set to 300, so I thought the array declaration was already allocating the memory... Should I actually use malloc?

Anyways, declaring that system as static seems to solve the problem:

    static complex system[MAX_NODES+1][MAX_NODES+2];

I just don't know why... I know a static variable inside a function makes that variable hold its value in subsequent function calls, but how is that related to memory or whatever this problem is? Any tips?

Thanks a lot in advance.

4
  • 1
    Your code sounds reasonable. Can you give a bit more context? Is it possible that you were trying to access the array outside of its scope (e.g. outside the function)? Commented Nov 22, 2013 at 4:56
  • 3
    Needs to show real, compilable code - including initialization. Commented Nov 22, 2013 at 4:56
  • 1
    The issue pretty much has to be in your initialization function. I think we need to see it. Commented Nov 22, 2013 at 5:10
  • Initialization function that runs right after goes like this: void initialize_system(complex system[MAX_NODES+1][MAX_NODES+2]){ int lines, columns; for (lines = 0; lines <= MAX_NODES+1; lines++){ for (columns = 0; columns <= MAX_NODES+2; columns++){ system[lines][columns].real = 0; system[lines][columns].img = 0; } } } and it does initialize the array before it crashes Whole, compilable code is here: pastebin.com/ZEfLDCJn Problem is inside mna function. Thank you! Commented Nov 22, 2013 at 5:13

1 Answer 1

2

Your code:

for (lines = 0; lines <= MAX_NODES+1; lines++)
{ 
     for (columns = 0; columns <= MAX_NODES+2; columns++){ 

should be

for (lines = 0; lines < MAX_NODES+1; lines++)
{ 
     for (columns = 0; columns < MAX_NODES+2; columns++){ 

You are overshooting both array dimensions by 1 otherwise - hence the memory error.

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

5 Comments

For OP's information - the fact that it 'works' with extra array or static is just a coincidence (although easily explainable).
Not coincidence: It "works" because the extra array provides a buffer - additional memory on the stack this is not being used, and can "legally" be accessed.. Since it is allocated at compile time there are no pointers being overwritten, and all is well.
Many thanks, that was it :) Still, could you say why the static variable apparently would solve the problem?
A static variable is created in a different area of memory to a variable in a function (although this is implementation dependent), so when it overshoots, it will be trashing something different - maybe harmless, maybe not. A variable in a function will trash the stack if you overshoot - which explains why adding another variable (effetively a block of memory) to the stack will change the behaviour. Basically, once you overshoot memory, what happens is anyone's guess, and will depend on compiler, enviornment, surrounding variables, which way the wind is blowing... etc.
@Floris as i've said, easily explainable. But as i recall, it is still UB.

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.