| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Peyman" |
| Date: |
02 Sep 2006 11:09:50 PM |
| Object: |
Dynamic Memory Allocation |
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 multidimensional 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!
.
|
|
| User: "Moonlit news moonlit xs4all nl" |
|
| Title: Re: Dynamic Memory Allocation |
03 Sep 2006 07:11:59 AM |
|
|
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" <peyman.taher@gmail.com> wrote in message
news:1157256590.048376.229020@h48g2000cwc.googlegroups.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 multidimensional 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!
.
|
|
|
| User: "Jens Theisen" |
|
| Title: Re: Dynamic Memory Allocation |
03 Sep 2006 07:55:00 AM |
|
|
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
.
|
|
|
| User: "Moonlit news moonlit xs4all nl" |
|
| Title: Re: Dynamic Memory Allocation |
03 Sep 2006 08:29:51 AM |
|
|
--
"Jens Theisen" <jth02@arcor.de> wrote in message
news:44fad046$0$5161$9b4e6d93@newsspool1.arcor-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
.
|
|
|
| User: "Gavin Deane" |
|
| Title: Re: Dynamic Memory Allocation |
04 Sep 2006 02:31:20 AM |
|
|
Moonlit wrote:
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.
If you mean OS or library functions that expect a pointer to the first
element of a contiguous sequence of objects (such as would be held in
an array), vectors can still help. Just use &v[0] and v.size() for the
pointer and the array length (where v is your vector). You get all the
protection and convenience of the standard library in your code and the
use of raw pointers doesn't need to propogate outside the API you are
using.
Gavin Deane
.
|
|
|
| User: "Moonlit news moonlit xs4all nl" |
|
| Title: Re: Dynamic Memory Allocation |
04 Sep 2006 06:08:49 AM |
|
|
Hi,
--
"Gavin Deane" <deane_gavin@hotmail.com> wrote in message
news:1157355080.709751.160110@m79g2000cwm.googlegroups.com...
Moonlit wrote:
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.
If you mean OS or library functions that expect a pointer to the first
element of a contiguous sequence of objects (such as would be held in
an array), vectors can still help. Just use &v[0] and v.size() for the
pointer and the array length (where v is your vector). You get all the
Thanks for the reply.
I tried to come up with some examples where array's would be necessary...,
but actually I couldn't find any that couldn't be replaced with vector :-/.
So I guess I should try to use vector even more in the future.
protection and convenience of the standard library in your code and the
use of raw pointers doesn't need to propogate outside the API you are
using.
Gavin Deane
Regards, Ron AF Greve
http://moonlit.xs4all.nl
.
|
|
|
| User: "jimmy" |
|
| Title: Re: Dynamic Memory Allocation |
04 Sep 2006 07:13:49 AM |
|
|
Moonlit =E5=86=99=E9=81=93=EF=BC=9A
Hi,
--
"Gavin Deane" <deane_gavin@hotmail.com> wrote in message
news:1157355080.709751.160110@m79g2000cwm.googlegroups.com...
Moonlit wrote:
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.
If you mean OS or library functions that expect a pointer to the first
element of a contiguous sequence of objects (such as would be held in
an array), vectors can still help. Just use &v[0] and v.size() for the
pointer and the array length (where v is your vector). You get all the
Thanks for the reply.
I tried to come up with some examples where array's would be necessary...,
but actually I couldn't find any that couldn't be replaced with vector :-=
/=2E
So I guess I should try to use vector even more in the future.
I agree with you :)
protection and convenience of the standard library in your code and the
use of raw pointers doesn't need to propogate outside the API you are
using.
Gavin Deane
=20
Regards, Ron AF Greve
=20
http://moonlit.xs4all.nl
.
|
|
|
|
|
|
|
|
|
| User: "Frederick Gotham" |
|
| Title: Re: Dynamic Memory Allocation |
03 Sep 2006 12:44:36 PM |
|
|
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
.
|
|
|
| User: "Moonlit news moonlit xs4all nl" |
|
| Title: Re: Dynamic Memory Allocation |
03 Sep 2006 04:36:45 PM |
|
|
Hi,
--
Regards, Ron AF Greve
http://moonlit.xs4all.nl
"Frederick Gotham" <fgothamNO@SPAM.com> wrote in message
news:8wEKg.13393$j7.327009@news.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
.
|
|
|
|
|
| User: "W Marsh" |
|
| Title: Re: Dynamic Memory Allocation |
02 Sep 2006 11:15:56 PM |
|
|
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 multidimensional 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.
.
|
|
|
|
| User: "Thomas Tutone" |
|
| Title: Re: Dynamic Memory Allocation |
02 Sep 2006 11:15:20 PM |
|
|
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 multidimensional 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
.
|
|
|
| User: "Peyman" |
|
| Title: Re: Dynamic Memory Allocation |
02 Sep 2006 11:54:26 PM |
|
|
thank tom.
just I'm wondering what does the -Wall -Wextra and -pedantic do!?
.
|
|
|
| User: "Thomas Tutone" |
|
| Title: Re: Dynamic Memory Allocation |
02 Sep 2006 11:59:04 PM |
|
|
Peyman wrote:
thank tom.
just I'm wondering what does the -Wall -Wextra and -pedantic do!?
<implementation specific/>
Any reason you can't check the manual for yourself? Here's the link:
http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Warning-Options.html#Warning-Options
</implementation specific>
Best regards,
Tom
.
|
|
|
|
|
|

|
Related Articles |
|
|