473,395 Members | 1,677 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.

memory allocation for arrays in c++

#include<conio.h>
#include<iostream.h>
#include<stdio.h>

void main()
{
int x[5] = {1,2,3,4,5};
int i = 0;
int y[5]={5,4,3,2,1};
int result[5]={21,0,0,0,0};



clrscr();
for(i = 0; i <=5; i++)
{
cout << x[i] << "\t" << y[i] << "\t" << result[i] << endl;
}
cout << "\n";

for(i=0;i<=5; i++)
{
printf("%u(%d) ",&x[i], x[i]);
}
cout << endl;
for(i=0;i<=5; i++)
{
printf("%u(%d) ",&y[i], y[i]);
}
cout << endl;
for(i=0;i<=5; i++)
{
printf("%u(%d) ",&result[i],result[i]);
}
cout <<endl;
i = 0;
while(i++ < 5)
{
result[i] = x[i] - y[i];
}
for(i = 0; i <=5; i++)
{
cout << x[i] << "\t" << y[i] << "\t" << result[i] << endl;
}

getch();
}

The output is
x y result
1 -1 21
2 4 -2
3 3 0
4 2 2
5 1 4

please explain how these answer is possible ? mainly how array elements are allocated in memory?
Sep 2 '06 #1
1 2167
Banfa
9,065 Expert Mod 8TB
No the output is

Expand|Select|Wrap|Line Numbers
  1. 1       5       21
  2. 2       4       0
  3. 3       3       0
  4. 4       2       0
  5. 5       1       0
  6. -858993460      -858993460      -858993460
  7.  
  8. 1245008(1)  1245012(2)  1245016(3)  1245020(4)  1245024(5)  1245028(-858993460)
  9. 1244968(5)  1244972(4)  1244976(3)  1244980(2)  1244984(1)  1244988(-858993460)
  10. 1244940(21)  1244944(0)  1244948(0)  1244952(0)  1244956(0)  1244960(-858993460)
  11. 1       5       21
  12. 2       4       -2
  13. 3       3       0
  14. 4       2       2
  15. 5       1       4
  16. -858993460      -858993460      0
You never calculate the result for index 0 because of the increment in the while loop and most of your for loops ( for (i=0; i<=5; i++) ) go round 6 times instead of 5 accessing memory that is not allocated.
Sep 2 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: hall | last post by:
I have a question regarding where memory is allocated when arrays are created. I'll illustrate this by example. I may be wrong on some details, do feel free to correct me. The code piece: int...
3
by: Anoop | last post by:
Hi guys i have a piece of code main() { switch(...) { case 1: {
6
by: J Anderson | last post by:
Greetings, I was going through the quake2 source code (as you do) when I noticed arrays are statically allocated on the stack instead of being allocated dynamically. I’ve seen this before and...
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...
50
by: Joerg Schwerdtfeger | last post by:
Hi folks, how can I determine the total main-memory size and the size of free memory available in bytes? I tried to use mallinfo() from malloc.h - resulting some strange values in Windows...
9
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
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...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
5
by: David Golightly | last post by:
Quick question for the gurus out there: in ECMAScript, one can create a new Array object with a length like so: var animals = new Array(128); This creates a new Array object with a "length"...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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,...
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.