[huskerlug] Re: More of a GCC problem.

  • From: <jwessels@xxxxxxxxxxxxxxxxxxx>
  • To: <huskerlug@xxxxxxxxxxxxx>
  • Date: Tue, 18 Mar 2003 11:30:23 -0700 (MST)

To allocate a dynamic array in C++ use:

x = blah;
int * myarray = new int[x]; // x must be 1 or greater

then to access the array like any other
myarray[y] = 0; // with y from 0 to x-1

To delete the array
delete [] myarray;

In C you must use malloc and typecast to your array type.

>
> Is there a reason you want to use a variable array?  If not, use a
> #define to create a constant & use that in your array declaration.  Or
> you could just use a literal.
>
> I think the link you gave said it all:
>
>      (However, GCC's implementation of variable-length arrays does not
> yet conform in detail to the ISO C99 standard.)
>
> There is always more than one way to do something.  Especially in a
> language as complex as C/C++.
>
> --Ben Chavet
>
>
> Quoting Jon Richardson <jonpaulrichardson@xxxxxxxxxxx>:
>
>>
>>
>>
>> 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%20Length
>>
>> 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




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


Other related posts: