473,770 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Any difference in these array declarations

Hello,
can anyone please tell me if there is any difference between the two:
double Array1[10];
and
double* Array2 = new double[10];
..
..
..
delete [] Array2;
Thanks
Jan 4 '06 #1
18 2269

Vasileios Zografos wrote:
Hello,
can anyone please tell me if there is any difference between the two:

double Array1[10];
and
double* Array2 = new double[10];
delete [] Array2;


Array1 is name of an array, a constant. Array2 is simply a pointer to a
double. Array2 can be reassigned a different value, Array1 cannot be.

double pi = 3.14;
Array2 = & pi; // fine
Array1 = & pi// error. Incompatible types in assignment.

Jan 4 '06 #2
Vasileios Zografos wrote:
can anyone please tell me if there is any difference between the two:
double Array1[10];
and
double* Array2 = new double[10];
.
.
.
delete [] Array2;


Of course there is. 'Array1' has the type 'array of 10 double', whereas
'Array2' has the type 'pointer to double'.

V
Jan 4 '06 #3
different method of memory allocation,
array1 is allocated on stack,
array2 is allocated on heap.

Jan 4 '06 #4

"Tsubasa" <ch**********@g mail.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
different method of memory allocation,
array1 is allocated on stack,
array2 is allocated on heap.


Thanks ;)
Jan 4 '06 #5

Tsubasa wrote:
different method of memory allocation,
array1 is allocated on stack,

not always. May be in data section, if it is declared non-locally.

Jan 4 '06 #6
Tsubasa wrote:
different method of memory allocation,
array1 is allocated on stack,
I presume you meant 'Array1', not 'array1'. And where it's allocated
depends entirely on where the declaration appears.
[..]


And please quote the relevant portion of the post you're replying to.
Jan 4 '06 #7
Vasileios Zografos wrote:

double Array1[10];

double* Array2 = new double[10];
.
.
.
delete [] Array2;


Array1 is exception safe. Array2 is not exception safe if an uncaught
exception is thrown in the "..." section of code.
Jan 4 '06 #8
Neelesh Bodas wrote:
Tsubasa wrote:
different method of memory allocation,
array1 is allocated on stack,


not always. May be in data section, if it is declared non-locally.


Just to pick a nit, neither 'stack', nor 'data section', nor even 'heap'
are proper C++ terms for the area of memory where objects live. It would
be better to use 'automatic', 'static', or 'dynamic' to distinguish the
objects. The only thing known is that dynamic objects are allocated in
the "free store".

V
Jan 4 '06 #9
OK here goes file mou... I will try and explain the difference without
getting too technical,

Declaring an array as "double Array1[10] can be thought of as declaring
a "hard coded" array... i.e. it's size MUST be known at the time of
compilation.... For example,

const int CONST_VAL = 5;
int nVal = 6;

double Array1[10]; // valid
double Array2[CONST_VAL]; // also valid
double Array3[nVal]; // NOT valid, nVal is not known
at compilation!!
Declaring an array as "double* pArray2 = new double[10]" has the same
effect with the main difference being the size of the array can be
stated from the value contained in a variable.... For example,

const int CONST_VAL = 5;
int nVal = 6;

double* pArray1 = new double[10]; // valid
double* pArray2 = new double[CONST_VAL]; // also valid
double* pArray3 = new double[nVal]; // also valid
Whichever way the array is created, it is still accessed in the same
way

double Array1[10]; // Array 1
double pArray2 = new double[10]; // Array 2

for( int i = 0; i < 10; i++ )
{
Array1[ i ] = i;
pArray2[ i ] = i;
}

Both arrays will contain exactly the same data...

BUT remember, any memory created using the "new" keyword MUST be
deleted when finished with otherwise you risk aquiring memory
leaks!!... And also "hard coded" arrays do not require deletion as they
are automatically deleted when out of scope!!...

So Array 2 requires to be deleted as so when not required further,

delete pArray2; (or delete [] pArray2;)

Regards

Frinos

Vasileios Zografos wrote:
Hello,
can anyone please tell me if there is any difference between the two:
double Array1[10];
and
double* Array2 = new double[10];
.
.
.
delete [] Array2;
Thanks


Jan 4 '06 #10

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

Similar topics

7
5461
by: Arthur Sinko | last post by:
Hi, Is it possible to answer the following question: what is the difference between two declarations: vector<double> a(n); and double a;
8
1464
by: Guybrush Threepwood | last post by:
Hi, I was always wondering if there is any difference between void someFunction(int,int) and void someFunction(int A, int B) is there?
7
1605
by: Paul | last post by:
Say I have: char foo; char** bar; In my understanding, these two have the same meaning in memory. Could someone elucidate upon these two types and their meanings? Thanks, Paul
11
5722
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little more today, and found that there is very little difference between a struct and a class in C++. Both have inheritance, access modifiers, etc. The only diff I see is the default access level is public vs. private. I have always just used structs as a minimal class, as that is the way
24
20083
by: RHNewBie | last post by:
Hello, What is the difference between null and NULL. Are x == null and x == NULL the same? The compiler is gcc 3.2. Thanks.
14
2039
by: code break | last post by:
what is the difference in this pointers decalarition ? int *ptr; and int (*ptr);
7
3416
by: arnuld | last post by:
this programme gives unusual output. i am not able to find out where the semantic bug lies: /* C++ Primer - 4/e * * an example from section section 7.2.4, page 241 * STATEMENT * write a function that prints the elements of an array. don't use pointer to an array as parameter because pointer will be copied, use reference to the array instead.
10
3444
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
11
5151
by: sunnyalways4u2000 | last post by:
hello sir, Sir will please tell me the exact difference between C and advanced C...what are the extra features or funcions...etc added in this advanced one.
0
9617
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
10099
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 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...
1
10036
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8929
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...
0
5354
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...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
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.