473,554 Members | 3,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.writeli ne(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 1350
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.writeli ne(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.writeli ne(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****@discuss ions.microsoft. com> wrote in message
news:4D******** *************** ***********@mic rosoft.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.writeli ne(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
11798
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious way to do this is a nested for-loop - but we all know O(n^2) is bad. So I am looking for something like ArrayList.ToArray(), or Matlabs A(:). C#
272
13942
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
5
3870
by: nelly0 | last post by:
developing a program that will manipulate noise levels (measured in decibels) that is collected by car manufacturers. These noise levels are produced at seven different speeds by a maximum of six different models of cars that are produced by the car manufacturer. Task 1 Step 1: Create a directory called Assignment02 and create the files of...
152
9767
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { { 3.14 }, { 42.6 } }; f((double *)array, sizeof array / sizeof **array); return 0;
0
7584
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7547
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7879
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6129
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5426
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
828
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.