I was trying to show a friend of mine, "why we need 'new' and DMA", and
I suddenly found that I can create arrays of variable length! which is
not supposed to work, but it works!
#include <iostream>
int main()
{
size_t l;
std::cin >l;
T a[l];
for (size_t i = 0; i != l; ++i)
std::cin >a[i];
return 0;
}
instead of
T* a = new T[l]();
and I could even create multidimensiona l arrays! It just didn't work
previously, is it a new stupid feature in GCC?
BTW, I'm using gcc version 4.0.3 and didn't have chance to try it with
other compilers! 12 3120
Peyman wrote:
I was trying to show a friend of mine, "why we need 'new' and DMA", and
I suddenly found that I can create arrays of variable length! which is
not supposed to work, but it works!
#include <iostream>
int main()
{
size_t l;
std::cin >l;
T a[l];
for (size_t i = 0; i != l; ++i)
std::cin >a[i];
return 0;
}
instead of
T* a = new T[l]();
and I could even create multidimensiona l arrays! It just didn't work
previously, is it a new stupid feature in GCC?
BTW, I'm using gcc version 4.0.3 and didn't have chance to try it with
other compilers!
It's a gcc extension, and has been around for a while. Try compiling
with -Wall -Wextra -pedantic, and see what happens.
Best regards,
Tom
Peyman wrote:
I was trying to show a friend of mine, "why we need 'new' and DMA", and
I suddenly found that I can create arrays of variable length! which is
not supposed to work, but it works!
#include <iostream>
int main()
{
size_t l;
std::cin >l;
T a[l];
for (size_t i = 0; i != l; ++i)
std::cin >a[i];
return 0;
}
instead of
T* a = new T[l]();
and I could even create multidimensiona l arrays! It just didn't work
previously, is it a new stupid feature in GCC?
BTW, I'm using gcc version 4.0.3 and didn't have chance to try it with
other compilers!
Nice troll.
thank tom.
just I'm wondering what does the -Wall -Wextra and -pedantic do!?
Peyman wrote:
thank tom.
just I'm wondering what does the -Wall -Wextra and -pedantic do!?
<implementati on specific/>
Any reason you can't check the manual for yourself? Here's the link: http://gcc.gnu.org/onlinedocs/gcc-4....arning-Options
</implementation specific>
Best regards,
Tom
Hi,
That's pretty neat. I actually wonder why this is not just 'standard C/C++'
since it (at least on most compilers I think) just makes room on the stack
similar to alloca. It is easier to write and faster than a new T[] /delete[]
T
--
Regards, Ron AF Greve http://moonlit.xs4all.nl
"Peyman" <pe**********@g mail.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
>I was trying to show a friend of mine, "why we need 'new' and DMA", and
I suddenly found that I can create arrays of variable length! which is
not supposed to work, but it works!
#include <iostream>
int main()
{
size_t l;
std::cin >l;
T a[l];
for (size_t i = 0; i != l; ++i)
std::cin >a[i];
return 0;
}
instead of
T* a = new T[l]();
and I could even create multidimensiona l arrays! It just didn't work
previously, is it a new stupid feature in GCC?
BTW, I'm using gcc version 4.0.3 and didn't have chance to try it with
other compilers!
Moonlit wrote:
I actually wonder why this is not just 'standard C/C++'
since it (at least on most compilers I think) just makes room on the stack
similar to alloca.
It is standard C (C99), and has thus a chance to become standard C++ for
compatibility reasons.
It is easier to write and faster than a new T[] /delete[] T
Which you shouldn't write anyway, but instead std::vector< T >.
Jens
--
"Jens Theisen" <jt***@arcor.de wrote in message
news:44******** **************@ newsspool1.arco r-online.net...
Moonlit wrote:
>I actually wonder why this is not just 'standard C/C++' since it (at least on most compilers I think) just makes room on the stack similar to alloca.
It is standard C (C99), and has thus a chance to become standard C++ for
compatibility reasons.
Thanks for the info. It would be a nice feature.
>It is easier to write and faster than a new T[] /delete[] T
Which you shouldn't write anyway, but instead std::vector< T >.
Jens
True I do use vector a lot. Sometimes it find it still easier to use arrays
when for instance calling OS or library functions that expect arrays.
Regards, Ron AF Greve http://moonlit.xs4all.nl
Peyman posted:
T a[l];
T* a = new T[l]();
To make them equivalent, either:
(1) Change the first one to:
T a[l] = {};
(2) Change the second one to:
T *a = new T[l];
But not both.
I haven't read up much on VLA's, but I wonder what happens when you apply
sizeof to them, or what would happen if you tried to invoke the following
template function with one:
template<class T,size_t len>
void Func(T (&)[len])
{
}
--
Frederick Gotham
Hi,
--
Regards, Ron AF Greve http://moonlit.xs4all.nl
"Frederick Gotham" <fg*******@SPAM .comwrote in message
news:8w******** ***********@new s.indigo.ie...
Peyman posted:
> T a[l];
> T* a = new T[l]();
To make them equivalent, either:
(1) Change the first one to:
T a[l] = {};
(2) Change the second one to:
T *a = new T[l];
But not both.
I haven't read up much on VLA's, but I wonder what happens when you apply
sizeof to them, or what would happen if you tried to invoke the following
template function with one:
template<class T,size_t len>
void Func(T (&)[len])
{
}
Good question.
I just tried some things with (a bit outdated) g++ compiler
It unfortunately doesn't work in VC++.
But with the gnu compiler sizeof just outputs the correct value (the size of
the array). I am not sure how the gnu people did that. With my (old) g++
3.2.2 version the template version just didn't compile (internal error,
segmentation fault, file bug report).
l = 8;
so unsigned long a[l];
sizeof( a ) would output 32
with an unsigned long length of 4. BTW I entered the value dynamically so
the sizeof( a ) isn't just a constant
I tried to find the value on the stack (other then in the variable l) but
couldn't find it. Wonder where they put that value.
>
--
Frederick Gotham
Regards, Ron AF Greve This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tom |
last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case).
Thanks,
Tom.
|
by: chris |
last post by:
Hi all,
I need to know, what is the difference between dynamic memory
allocation, and stack allocation ?
1. If I have a class named DestinationAddress, when should I use
dynamic memory allocation to create object of that class ?
2. If it says "dynamic memory allocation", is it mean the
following code :
DestinationAddress* dest = new DestinationAddress(); // code 1
|
by: Sandeep Chikkerur |
last post by:
Hi,
If the entire heap memory for dynamic allocation is not available,
does the compiler always return NULL ?
eg:
char *s;
s = (char *)malloc(...);
|
by: xian_hong2046 |
last post by:
Hello,
I think dynamic memory allocation is supposed to be used when one
doesn't know in advance how much memory to allocate until run time. An
example from Thinking in C++ is to dynamically create an array (using
"new") since one doesn't know it size when writing the program.
However, it looks to me that the size information must come from
somewhere at run time, and this information can be passed to array
creation function as a...
|
by: toton |
last post by:
Hi,
I have little confusion about static memory allocation & dynamic
allocation for a cluss member.
I have class like
class Bar{
public:
explicit Bar(){
cout<<"bar default"<<endl;
}
| |
by: Ken |
last post by:
In C programming, I want to know in what situations we should use
static memory allocation instead of dynamic memory allocation. My
understanding is that static memory allocation like using array is
faster than malloc, but dynamic memory allocation is more flexible.
Please comment... thanks.
|
by: Peterwkc |
last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back.
By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this?
Second Program:
/* Best Method to allocate memory for 2D Array because it's
...
|
by: ranjeetasharma81 |
last post by:
Hi all,
I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am facing:
1- From structure and dynamic memory allocation point of view, the code is
very complicated. The code has various “nested structures” with a number
of levels. The size of memory allocated for pointer to structure or its
pointer...
|
by: vivek |
last post by:
i have some doubts on dynamic memory allocation and stacks and heaps
where is the dynamic memory allocation used?
in function calls there are some counters like "i" in the below
function. Is this stored in stack. If yes whether it will be deleted
on exiting from the function.
is dynamic memory allocation needed for this purpose
|
by: swornavidhya.mahadevan |
last post by:
Which allocation (Static / Dynamic) is suitable for the situation when
we are trying to allocate for a overloaded memory when the memory is
full and no space to allocate.
What will happen if both the allocation is impossible.
Sworna vidhya
|
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...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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: 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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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.
| |