[huskerlug] Re: More of a GCC problem.

  • From: "Steve Goddard" <goddard@xxxxxxxxxxx>
  • To: <huskerlug@xxxxxxxxxxxxx>
  • Date: Tue, 18 Mar 2003 09:50:31 -0600

> Am I doing something stupid I don't see?

Yes, and no.  I think you do not understand how to use variable length
arrays.  You may also have a similarly defined array in which results in
overwriting stack space.

The link you provided says this:   "The length of an array is computed once
when the storage is allocated and is remembered for the scope of the array
in case you access it with `sizeof'."

I believe, in your code, that means that the length is zero bytes long.  It
will NOT change when the value of the variable iBalance.   It works when you
delare the array to be of size 100 because then you are allocating space for
the array.

Steve
goddard@xxxxxxxxxxx
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dr. Steve Goddard                 Assistant Professor
Room #215-A, Ferguson Hall        Dept. of Computer Science and Eng.
Phone: (402) 472-9968             University of Nebraska-Lincoln
Fax: (402) 472-7767               Lincoln NE 68588-0115
Email: goddard@xxxxxxxxxxx        WWW: http://www.cse.unl.edu/~goddard/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

----- Original Message -----
From: "Jon Richardson" <jonpaulrichardson@xxxxxxxxxxx>
To: <huskerlug@xxxxxxxxxxxxx>
Sent: Tuesday, March 18, 2003 3:12 AM
Subject: [huskerlug] More of a GCC problem.


>
>
>
> Grr.... I'm fed up of buggy compilers, maybe it's just me? :)  Anyways I'm
> fairly new to gcc having ported my code from visual c++ 6. (Although this
> code wasn't ported from visual c++ 6)  I'm getting some strange results
from
> the output of my code.
>
> The variable iLabelNode is a variable sized array declared as such:
>
> int iBalance = 0;
> int iLabelNode[iBalance];
>
> The variable iBalance later changes to a larger value.
>
> Here is the code where the problem occurs:
>
> puts("<------------- FlowGraph Crap -------------->");
>         for (UINT m = 1; m <= oListLabel.Count(); ++m)
>         {
>                 printf("%s = %d\n", oListLabel.Value(m), iLabelNode[m -
1]);
>         }
>
>         printf("iLabelNode[0] = %d\n", iLabelNode[0]);
>
>         //get actual count
>         m_iNodeCount -= iBalance;
>
>         printf("iLabelNode[0] = %d\n", iLabelNode[0]);
>
>         //create nodes
>         Node* pNode = 0;
>         for (int y = 0; y < m_iNodeCount; ++y)
>         {
>                 pNode = new Node;
>                 pNodeArray[y] = pNode;
>         }
>         printf("iLabelNode[0] = %d\n", iLabelNode[0]);  //<-- screw up
here
>
>
> Can also be viewed here for pretty syntax highlighting :)
> http://www.rafb.net/paste/results/P1014321.html
>
> PORTION OF OUTPUT
> <------------- FlowGraph Crap -------------->
> IL_0006: = 4
> IL_000e: = 6
> iLabelNode[0] = 4
> iLabelNode[0] = 4
> iLabelNode[0] = 134587360
>
> My question is why does the value of iLabelNode[0] change when it's
clearly
> not being modified?  The funny thing is is that if I declare it as such
"int
> iLableNode[100]" it works fine.  Should I just avoid variable sized
arrays?
> Am I doing something stupid I don't see?
>
> Here is what the gcc manual says about variable sized arrays, didn't say
> much but maybe I'm missing something.
>
http://gcc.gnu.org/onlinedocs/gcc-3.2.2/gcc/Variable-Length.html#Variable%20
Length
>
> If anyone could shed some light on this for me I would be very thankful,
> this is driving me nuts.
>
> Thanks,
>
> Jon
>
>
>
>
>
> _________________________________________________________________
> Help STOP SPAM with the new MSN 8 and get 2 months FREE*
> http://join.msn.com/?page=features/junkmail
>
>
> ----
> Husker Linux Users Group mailing list
> To unsubscribe, send a message to huskerlug-request@xxxxxxxxxxxxx
> with a subject of UNSUBSCRIBE
>
>


----
Husker Linux Users Group mailing list
To unsubscribe, send a message to huskerlug-request@xxxxxxxxxxxxx
with a subject of UNSUBSCRIBE


Other related posts: