Re: How do you store a stoopid array in the stoopid ViewState?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



To be honest I can't see where there is a problem. The code looks fine....
the error is saying you are trying to add 1 to an object, which would be an
error, but I can't see where it is happening and the code I tested (the one I
posted in my inital response) works fine... I have it in a code behind
file... however, even if you have it inline it should still work.

If line 128 is giving you the error, i.e. "int_array[nn] = int_array[nn] +
1;" then the only thing I can think of is that int_array[nn] is actually
returning an object instead of a UInt32... I can't see why this would be the
case but it seems like it is... so, you could try this changing that line to:

int_array[nn] = ((UInt32) int_array[nn]) + 1;

and if that doesn't work you could try this

int_array[nn] = UInt32.Parse(int_array[nn].ToString()) + 1;


Anyway, seems like you are having a weird problem. As regards your question
about arrays. Unfortunately .NET arrays, even array of value types, are
stored on the heap. The only thing stored on the stack is a reference to the
array. I don't know if there is a way around this. I doubt it. By the way, do
you mind if I ask why you want it on the stack? Is it just for performance
reasons?

I hope I've helped somehow.

Brian Delahunty
Ireland

http://briandela.com/blog


"wASP" wrote:

> On Wed, 10 Aug 2005 14:38:05 -0700, Brian Delahunty
> <BrianDelahunty@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Hi Brian,
>
> I have a problem with the ELSE case:
>
> /* ------------------------------------------------------------------ */
> /* ------------------------------------------------------------------ */
> else
> {
> Label1.Text = " On postback: |";
>
> UInt32[] int_array = (UInt32[]) ViewState["int_array"];
>
> for (UInt32 nn = 0; nn < 11; nn++)
> int_array[nn] += 1;
>
> for (UInt32 nn = 0; nn < 11; nn++)
> Label1.Text = Label1.Text + int_array[nn].ToString() + "|";
> }
> }
> /* ------------------------------------------------------------------ */
> /* ------------------------------------------------------------------ */
>
>
>
> It threw the following exception:
> /* ------------------------------------------------------------------ */
> /* ------------------------------------------------------------------ */
>
> Server Error in '/Webfolder01' Application.
> --------------------------------------------------------------------------------
>
> Compilation Error
> Description: An error occurred during the compilation of a resource required
> to service this request. Please review the following specific error details
> and modify your source code appropriately.
>
> Compiler Error Message: CS0019: Operator '+' cannot be applied to
> operands of type 'object' and 'int'
>
> Source Error:
>
> Line 127: for (Int32 nn = 0; nn < 11; nn++)
> Line 128: int_array[nn] = int_array[nn] + 1;
> Line 129:
> Line 130: for (Int32 nn = 0; nn < 11; nn++)
>
> /* ------------------------------------------------------------------ */
> /* ------------------------------------------------------------------ */
>
>
>
> Do I need to set some kind of a switch or something somewhere?
>
>
> >Also, what do you mean by "immediate array"?
>
> One that allocates a block of memory from the program stack,
> instead of dynamically from the heap.
>
> IOW, C# creates arrays dynamically - much like doing the following in C++:
>
> int (*int_array)[] = farmalloc (11);
>
>
> ... instead of allocating from the program stack in C++ like this:
>
> int int_array[11];
>
> I don't see anything in any docs on C# that will let you do anything
> other than to allocate array-space from the heap.
>
>
> - wASP
>
.



Relevant Pages

  • Re: ALLOCATABLE arrays
    ... || If try to create a very large array on the stack and you do not have enough ... || Allocating on the heap gives you access to a hell of a lot more memory (well ... and "heap" (and there probably are/were some computers which don't/didn't ... | Automatic arrays are always allocated on the stack. ...
    (comp.lang.fortran)
  • Re: Stack or Heap
    ... When are arrays allocated on the heap, ... Is a Fortran stack typically larger than a C stack? ... Quite often automatic arrays will be allocated on the heap, as well, ...
    (comp.lang.fortran)
  • Re: heap allocation of arrays
    ... | to force all arrays to be allocated on the heap. ... | the stack would be replaced with pointers on the stack. ... | heap is easier to detect than failure to allocate space on the ...
    (comp.lang.fortran)
  • Re: ALLOCATABLE arrays
    ... > If try to create a very large array on the stack and you do not have enough ... > Allocating on the heap gives you access to a hell of a lot more memory (well ... Allocatable arrays are allocated on the heap. ... Automatic arrays are always allocated on the stack. ...
    (comp.lang.fortran)
  • Re: How does managed code work?
    ... Does it work the same way as the native stack with a frame pointer that is the head of a linked list of stack frames where each time we enter a function we create a new stack frame in which new variables are pushed and each time we exit a function the entire stack frame is popped? ... Can someone point me to a discussion of the managed heap? ... How does it prevent memory leaks that occur in COM when two objects reference each other and keep the others reference count nonzero? ... Because objects don't go out of scope, ...
    (microsoft.public.dotnet.languages.csharp)