Technical two dimensional array question

G

Guest

Hi, I have a cumbersome question:

Given the following declaration:

string [,] varray = new string[3,5]; (15 blocks of memory)

Does it allocate a contiguous blocks of memory or not? If it does,
is it guaranteed to be contiguous everytime?

What If I tried to go out of range but in a knowingly safe manner like the
following in order to assign to the next array out of the 3 arrays:

varray[0,5] = "string object"; // out of range overflow
console.writeline(varray[0,1]);

I tried it and I get an exception error, as I should, but, I also tried
catching the exception to allow it to work but the problem is that the
exception blocked my program before the assignment could occur; proving why I
said it did not work. Am I wrong on this last assumption?

In C, something like this particular program would work becuase C is not
very strict when it comes to safety conforms.

I would appreciate if someone could answer these two questions for me, and
also, if I could be refered to a book that gets into the nity-gritty
technicalities of C# semantics. Especially concerning with the subject of
this thread.

Thanks in advance,
Jesika.
 
G

Guest

Jesika,
I think the issue here is not that you need to worry about memory usage,
since by definition managed code has full "memory management".

The issue is, when you have declared an array you cannot change it. A
string[3,5] array can only be changed by creating a new array and copying its
contents.

Trying to "work around" the runtime by catching an exception and then
merrily sailing on with something new like this isn't going to work.
Hope that helps.
Peter
 
B

Bjorn Abelli

...
Given the following declaration:

string [,] varray = new string[3,5]; (15 blocks of memory)

Does it allocate a contiguous blocks of memory or not?

It does not.

At least not in the way I think you mean.

What it allocates is memory space for *references* to string objects, not
space for the objects themselves.
What If I tried to go out of range but in a knowingly
safe manner like the following in order to assign to
the next array out of the 3 arrays:

varray[0,5] = "string object"; // out of range overflow
console.writeline(varray[0,1]);

I tried it and I get an exception error, as I should, but,
I also tried catching the exception to allow it to work
but the problem is that the exception blocked my program
before the assignment could occur; proving why I
said it did not work. Am I wrong on this last assumption?

I don't even understand what you mean when you say that you "catch the
exception to allow it to work"?

If an exception is thrown because it isn't allowed, it won't be allowed
ever...
In C, something like this particular program would work
becuase C is not very strict when it comes to safety conforms.

Which is one of the problems of C.

In a C implementation that memory space could very well be occupied by
something else, which you'll destroy by exceeding the boundaries of the
allocated array.

// Bjorn A
 
W

Willy Denoyette [MVP]

inline

Willy.

Jesika said:
Hi, I have a cumbersome question:

Given the following declaration:

string [,] varray = new string[3,5]; (15 blocks of memory)

Does it allocate a contiguous blocks of memory or not? If it does,
is it guaranteed to be contiguous everytime?

Yes, the array of "string references" is guaranteed to be contigious.

What If I tried to go out of range but in a knowingly safe manner like the
following in order to assign to the next array out of the 3 arrays:

varray[0,5] = "string object"; // out of range overflow
console.writeline(varray[0,1]);

I tried it and I get an exception error, as I should, but, I also tried
catching the exception to allow it to work but the problem is that the
exception blocked my program before the assignment could occur; proving
why I
said it did not work. Am I wrong on this last assumption?
You can catch (and swallow) the exception, but you can never assign a
reference to a location that is outside of the array bounds. Why would you
ever wanna do this?
In C, something like this particular program would work becuase C is not
very strict when it comes to safety conforms.

No, such program contains a bug, it can seeingly work, but sooner or later
it will fail as you are overwriting whatever is located after the last array
element.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top