#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? 11 1959
Sunny wrote:
#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?
Yes, gcc supports C99 style variable length arrays.
--
Ian Collins.
Ian Collins wrote:
Sunny wrote:
>>#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?
Yes, gcc supports C99 style variable length arrays.
I should have added "as an extension to ", not deviating from the standard.
--
Ian Collins.
On Mar 22, 3:05 pm, Ian Collins <ian-n...@hotmail.co mwrote:
Ian Collins wrote:
Sunny wrote:
>#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?
Yes, gcc supports C99 style variable length arrays.
I should have added "as an extension to ", not deviating from the standard.
--
Ian Collins.- Hide quoted text -
- Show quoted text -
Thanks for the reply, Ian.
On Mar 22, 3:09 pm, "Sunny" <shiladitya.bis ...@gmail.comwr ote:
On Mar 22, 3:05 pm, Ian Collins <ian-n...@hotmail.co mwrote:
Ian Collins wrote:
Sunny wrote:
>>#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?
Yes, gcc supports C99 style variable length arrays.
I should have added "as an extension to ", not deviating from the standard.
--
Ian Collins.- Hide quoted text -
- Show quoted text -
Thanks for the reply, Ian.- Hide quoted text -
- Show quoted text -
#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 |
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.
Thanks
On 22 Mar, 13:04, "Sunny" <shiladitya.bis ...@gmail.comwr ote:
On Mar 22, 3:09 pm, "Sunny" <shiladitya.bis ...@gmail.comwr ote:
On Mar 22, 3:05 pm, Ian Collins <ian-n...@hotmail.co mwrote:
Ian Collins wrote:
Sunny wrote:
>#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?
Yes, gcc supports C99 style variable length arrays.
I should have added "as an extension to ", not deviating from the standard.
--
Ian Collins.- Hide quoted text -
- Show quoted text -
Thanks for the reply, Ian.- Hide quoted text -
- Show quoted text -
#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 |
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.
It's a pointer, it does not matter how large the array is since it's
not allocated on the stack, which the other variables (len, width).
Arr must be a pointer since the compiler doesn't know at compile-time
how much space to reserve on the stack for the array, so it makes room
for an array and handles the allocation and deallocation for you. So
basically it's just a compiler generated wrapper around
int* Arr;
arr = new int[len];
// At end of scope
delete[] Arr;
--
Erik Wikström
#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 |
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.
How the compiler works internally is implementation dependent. Most
likely, the extra are housekeeping bytes. If exception handling is
turned on/off, you will probably notice other differences too.
If you would like more compiler specific answers, you will need to go to
a more compiler specific newsgroup. This is an ANSI C++ group AFAIK ;),
and is not worried so much about the internals.
Good luck.
Adrian
--
_______________ _______________ _______________ _______________ _________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under a Creative Commons /
\ Attribution-Share Alike 3.0 License /
\_______[ http://creativecommons.org/licenses/by-sa/3.0/]______/
\/_______[blog:_http://adrians-musings.blogspo t.com/]______\/
Sunny wrote:
#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?
If you turn on -Wall -ansi -pendatic, then you will see a warning that
you are using variable length array.
Fei
On Mar 22, 6:19 pm, "Erik Wikström" <eri...@student .chalmers.se>
wrote:
On 22 Mar, 13:04, "Sunny" <shiladitya.bis ...@gmail.comwr ote:
On Mar 22, 3:09 pm, "Sunny" <shiladitya.bis ...@gmail.comwr ote:
On Mar 22, 3:05 pm, Ian Collins <ian-n...@hotmail.co mwrote:
Ian Collins wrote:
Sunny wrote:
>>#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?
Yes, gcc supports C99 style variable length arrays.
I should have added "as an extension to ", not deviating from the standard.
--
Ian Collins.- Hide quoted text -
- Show quoted text -
Thanks for the reply, Ian.- Hide quoted text -
- Show quoted text -
#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 |
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.
It's a pointer, it does not matter how large the array is since it's
not allocated on the stack, which the other variables (len, width).
Arr must be a pointer since the compiler doesn't know at compile-time
how much space to reserve on the stack for the array, so it makes room
for an array and handles the allocation and deallocation for you. So
basically it's just a compiler generated wrapper around
int* Arr;
arr = new int[len];
// At end of scope
delete[] Arr;
--
Erik Wikström- Hide quoted text -
- Show quoted text -
Hi Eric,
I investigated further and printed out &Arr[0], &Arr[1],.... These
addresses looked like stack addresses ad not heap addresses. So it is
unlikely that compiler is using new.
Sunny wrote:
>
Hi Eric,
I investigated further and printed out &Arr[0], &Arr[1],.... These
addresses looked like stack addresses ad not heap addresses. So it is
unlikely that compiler is using new.
You are right. It is allocating it on the stack. The sizes and address
locations indicate such. However, this is not (currently) in the C++
spec, this is (as Ian stated) an extension. Do this at your own peril.
If you move to another compiler, it may not work.
Adrian
--
_______________ _______________ _______________ _______________ _________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ _---_ Q. What are you doing here? _---_ /
\ / | A. Just surf'n the net, teaching and | \ /
\__/___\___ learning, learning and teaching. You?_____/___\__/
\/______[blog:__http://adrians-musings.blogspo t.com/]______\/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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 -----------
|
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.
|
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)
|
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
| |
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.
|
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
|
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...
|
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.
|
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...
|
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...
| |
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
|
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...
|
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...
|
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();...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |