473,396 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Are 2D arrays contiguous?

Greetings.

Are statically allocated two-dimensional arrays guaranteed to be
contiguous, or is it possible the compiler will allocate padding after
each row? Is the following code OK?

#include <string.h>

int main(void) {
char a[5][5];
memset(a, 'x', 25);
return 0;
}

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= < In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Aug 15 '08 #1
3 5427
On Aug 15, 1:38*pm, Tristan Miller <psychon...@nothingisreal.com>
wrote:
Greetings.

Are statically allocated two-dimensional arrays guaranteed to be
contiguous, or is it possible the compiler will allocate padding after
each row? *Is the following code OK?

#include <string.h>

int main(void) {
* char a[5][5];
* memset(a, 'x', 25);
* return 0;

}

Regards,
Tristan

--
* *_
* _V.-o *Tristan Miller [en,(fr,de,ia)] *>< *Space is limited
*/ |`-' *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*<*In a haiku, so it's hard
(7_\\ * *http://www.nothingisreal.com/* >< *To finish what you

As far as I know they are.
Aug 15 '08 #2
Tristan Miller wrote:
Greetings.

Are statically allocated two-dimensional arrays guaranteed to be
contiguous,
Yes.
or is it possible the compiler will allocate padding after
each row?
No.
Is the following code OK?
Yes.
#include <string.h>

int main(void) {
char a[5][5];
memset(a, 'x', 25);
return 0;
}
--
pete
Aug 15 '08 #3
Tristan Miller <ps********@nothingisreal.comwrites:
Are statically allocated two-dimensional arrays guaranteed to be
contiguous, or is it possible the compiler will allocate padding after
each row? Is the following code OK?

#include <string.h>

int main(void) {
char a[5][5];
memset(a, 'x', 25);
return 0;
}
Yes, because you're treating the object as an array of characters.

On the other hand, this:

a[0][5] = 'y';

will probably set a[1][0] to 'y', but it's not guaranteed; strictly
speaking, it's undefined behavior.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 15 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: SABmore | last post by:
Is there a performance advantage to parsing thru a recordset verus using an array? I'm currently trying to populate a listbox by returning data from my database, then either parsing thru the...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
1
by: Paul | last post by:
See topic; Context: Passing arrays around in a MPI system(Message Passing Interface- Parallel Program crap) Thanks much! -Paul
10
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i...
197
by: Steve Kobes | last post by:
Is this legal? Must it print 4? int a = {{1, 2}, {3, 4}}, *b = a; printf("%d\n", *(b + 3)); --Steve
8
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along...
38
by: Peteroid | last post by:
I looked at the addresses in an 'array<>' during debug and noticed that the addresses were contiguous. Is this guaranteed, or just something it does if it can? PS = VS C++.NET 2005 Express...
1
by: xllx.relient.xllx | last post by:
Hi, I have two questions: 1.)Is it true that an rectangular array is really just an single dimensional array that lets itself be treated as a multi-dimensional array? For example the...
12
by: divya | last post by:
1. If The size of the array is unknown during programming time then how is such array declared?? Suppose there is an Array named arrClashname(size unknown at start), Now when the clash occurs...
14
by: ds | last post by:
Hi all, I have to pass an array of doubles to a legacy C function that copies some data using memcpy. The code would look like this: extern "C"{ void legacyCFunctionFill(void* arg); } .......
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.