473,395 Members | 1,530 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,395 software developers and data experts.

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!

Sep 3 '06 #1
12 3091
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

Sep 3 '06 #2
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.
Sep 3 '06 #3
thank tom.
just I'm wondering what does the -Wall -Wextra and -pedantic do!?

Sep 3 '06 #4
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....arning-Options

</implementation specific>

Best regards,

Tom

Sep 3 '06 #5
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**********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.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!

Sep 3 '06 #6
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
Sep 3 '06 #7


--

"Jens Theisen" <jt***@arcor.dewrote in message
news:44**********************@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
Sep 3 '06 #8
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
Sep 3 '06 #9
Hi,

--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:8w*******************@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
Sep 3 '06 #10

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

Sep 4 '06 #11
Hi,

--

"Gavin Deane" <de*********@hotmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.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
Sep 4 '06 #12

Moonlit 写道:
Hi,

--

"Gavin Deane" <de*********@hotmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.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.
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
Regards, Ron AF Greve

http://moonlit.xs4all.nl
Sep 4 '06 #13

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

Similar topics

9
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.
6
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...
6
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(...);
13
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...
11
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; }
24
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...
1
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...
3
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...
14
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...
10
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.