473,943 Members | 17,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arrays in C++

#include <iostream>

int main()
{
int len;
std::cin >len;
int Arr[len];
int *p = new int[len];
Arr[len-1]=5;
std::cout << &len << " " << &Arr << " " << p << std::endl;
return 0;
}

This code declares an array Arr of size len which is not fixed at
compile time. This program compiles using g++ (gcc version 3.2.3
20030502) and works properly. I am surprised to see this because I
thought arrays in C++ should have size fixed at compile time.
Is g++ deviating from standards here?

Mar 22 '07
11 1969
"Sunny" <sh************ ***@gmail.comwr ote in message
news:11******** **************@ l77g2000hsb.goo glegroups.com.. .
: #include <iostream>
:
: int main()
: {
: int len;
: std::cin >len;
: int Arr[len];
: int Arr1[2*len];
: int width;
: Arr[len-1]=5;
: std::cout << &len << " " << &Arr << " " << &Arr1<<"" <<&width <<
: std::endl;
: return 0;
:
: }
:
: This code declares two variable size arrays. I am entering len as 20.
: The program produces the following output:
:
: len : 0xbfff99a0
: Arr : 0xbfff9940
: Arr1 : 0xbfff98a0
: width :0xbfff999c
:
: ^
: 8a0 |
: |
: 940 |
: |
: 9a0 |
: |
: 99c |
I would rather represent this as:
99a0 len
999c width
9940 Arr
98a0 Arr1

: So width is allocated 4 bytes, len 0x60 bytes, Arr 0xA0 bytes. I am
: unable to understand how compiler generates size of Arr as 0xA0 bytes
: when it is of length 0x14*4 = 0x50 bytes. And why len gets 0x60
: bytes.

This is not the way that I would interpret the above results:
width gets 4 bytes (sizeof(int) appears to be 4 bytes)
Arr gets 99c-940 = 0x5c = 92 bytes
Arr1 gets 940-8a0 = 0xa0 = 160 bytes
With len=20, the required sizes for the arrays are:
- 20*4 = 80 bytes
- 40*4 = 160 bytes.
There seems to be some kind of padding or other compiler-generated
data between Arr and width, which is perfectly fine (it's probably
for housekeeping data required by the dynamic allocation).

BTW, runtime-sized C arrays are not allocated on the heap
with malloc, but possibly a function/macro called "alloca",
which you may be able to look-up in your compiler's doc.
This of course is all off-topic here, but I wanted to
clear some of the confusion...
I hope this helps,
Ivan

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Mar 23 '07 #11
On Mar 23, 2:25 pm, "Ivan Vecerina"
<_INVALID_use_w ebfo...@ivan.ve cerina.comwrote :
"Sunny" <shiladitya.bis ...@gmail.comwr ote in message

news:11******** **************@ l77g2000hsb.goo glegroups.com.. .
: #include <iostream>
:
: int main()
: {
: int len;
: std::cin >len;
: int Arr[len];
: int Arr1[2*len];
: int width;
: Arr[len-1]=5;
: std::cout << &len << " " << &Arr << " " << &Arr1<<"" <<&width <<
: std::endl;
: return 0;
:
: }
:
: This code declares two variable size arrays. I am entering len as 20.
: The program produces the following output:
:
: len : 0xbfff99a0
: Arr : 0xbfff9940
: Arr1 : 0xbfff98a0
: width :0xbfff999c
:
: ^
: 8a0 |
: |
: 940 |
: |
: 9a0 |
: |
: 99c |
I would rather represent this as:
99a0 len
999c width
9940 Arr
98a0 Arr1

: So width is allocated 4 bytes, len 0x60 bytes, Arr 0xA0 bytes. I am
: unable to understand how compiler generates size of Arr as 0xA0 bytes
: when it is of length 0x14*4 = 0x50 bytes. And why len gets 0x60
: bytes.

This is not the way that I would interpret the above results:
width gets 4 bytes (sizeof(int) appears to be 4 bytes)
Arr gets 99c-940 = 0x5c = 92 bytes
Arr1 gets 940-8a0 = 0xa0 = 160 bytes
With len=20, the required sizes for the arrays are:
- 20*4 = 80 bytes
- 40*4 = 160 bytes.
There seems to be some kind of padding or other compiler-generated
data between Arr and width, which is perfectly fine (it's probably
for housekeeping data required by the dynamic allocation).

BTW, runtime-sized C arrays are not allocated on the heap
with malloc, but possibly a function/macro called "alloca",
which you may be able to look-up in your compiler's doc.

This of course is all off-topic here, but I wanted to
clear some of the confusion...
I hope this helps,
Ivan

--http://ivan.vecerina.c om/contact/?subject=NG_POS T<- email contact form
Brainbench MVP for C++ <>http://www.brainbench.com
Thanks Ian,
You are right. I had interpreted sizes incorrectly. I will look at the
alloca() call.

Mar 23 '07 #12

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

Similar topics

19
2868
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type > >pointer-to-array-of-size-N-of-type-T (which is fine) and not having type > >array-of-size-N-of-type-T (with some exceptions, which is curious). > > So far > >the consensus seems to be that while everyone is aware of this no one knows
21
3959
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 "browse" it). So I expected that when I run this program, I get both c1.A and c2.A pointing to the same address, and changing c1.A means that also c2.A changes too. ----- BEGIN example CODE -----------
5
29265
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir); pfiles = ld.GetFiles("*.aspx.resx|") + ld.GetFiles("*.ascx.resx") + ld.GetFiles("*.master.resx"); but of course there is no + operation allowed on the FileInfo arrays returned by the GetFiles method.
3
2857
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; } *arrays; It's supposed to be a structure containing an array of chars, an array of ints and an int. I declare functions like this : arrays *parseline(char *line, int N)
1
8726
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections Framework are said to have an element type. http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
41
5057
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in the hash are alphabetically sorted if the key happens to be alpha numeric. Which I believe makes sense because it allows for fast lookup of a key.
6
13195
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get: the two arrays {"one","two","three"},{"red","green","blue} the result one red one green
1
2458
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are of different lengths (e.g. usually between 25 and 45 rows. The columns in each file have the same length). The text files have been numbered sequentially e.g. cb0, cb1, cb2 and so on. I would like to read the data from each text file into...
16
2564
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over at just comp.lang.c++. If one of these groups is too inappropriate, just take it off from where you send your replies.) Hi.
29
35636
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how multi-dimensional arrays work, b) how to call a function with a multi-dimensional array, c) how to return a multi-dimensional array from a function, or d) how to read and write arrays from a disc file. Note to C++ programmers: You should be using...
0
10142
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10666
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9866
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8228
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6090
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4515
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3516
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.