471,350 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Technical two dimensional array question

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.
Jan 1 '06 #1
3 1277
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Jesika" wrote:
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.

Jan 2 '06 #2

"Jesika" wrote...
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
Jan 2 '06 #3
inline

Willy.

"Jesika" <Je****@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
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.
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.

Jan 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.