View Full Version: i think i found a misteak in my book

C++ Learning Community > C++ in General > i think i found a misteak in my book


Title: i think i found a misteak in my book
Description: if so its cool i found it lol...


ih8censorship - January 25, 2005 04:36 AM (GMT)
QUOTE

an array of objects can be allocated dynamically with new as in

int *ptr=new int[100];


which allocates an array with 100 intergers and assigns the starting location of the array to ptr. the preceeding array of intergers is deleted with the statement

delete [] ptr;


i would think that there would be 101 intergers instead of 100 since arrays start with 0. this wasent in like a true or false part of the book it was in the chapter summary.... so am i right that its wrong or is the book right?

dorto - January 25, 2005 05:05 AM (GMT)
ofcourse the book is right.

int *ptr=new int[100];

allocates memory for 100 integers and those hundred integers can be accessed as ptr[0]....ptr[99]. More generally:

int *ptr=new int[N];

allocates memory for N integers which can be accessed as ptr[0]...ptr[N-1]. N here is the size and not the highest index. ptr[N] doesn't exist.

dr voodoo - January 25, 2005 02:17 PM (GMT)
Indexes go from 0 to 99 both included. The size of the array, meaning the number of elements, is 100.

ih8censorship - January 25, 2005 04:06 PM (GMT)
so

int *ptr=new int[100];

is different than

int ptr[100];

?

dorto - January 25, 2005 04:11 PM (GMT)
why do you think so? are you doing lots of basic these days? ;)

int ptr[100];

the hundred integers can be accessed as ptr[0]....ptr[99]. ptr[100] doesn't exist. 100 here is the size not the highest index.

dr voodoo - January 25, 2005 04:34 PM (GMT)
In C/C++ the last element has the index size-1. Always.

ih8censorship - January 25, 2005 10:34 PM (GMT)
oh allright... i guess i just figured it allocated all the way out because it will save the data.. funny thing is this works...
CODE
#include <iostream>
using namespace std;
int main()
{
int nums[10];
nums[11]=52;
cout<<nums[11];
cin.get();

return 0;
}

KTC - January 25, 2005 10:49 PM (GMT)
In C, C++, Java, C#, and pretty much every other programming language as well that's C based, if not more starts counting at 0. It's always always 0 ... (n-1).

QUOTE
oh allright... i guess i just figured it allocated all the way out because it will save the data.. funny thing is this works...
CODE
#include <iostream>
using namespace std;
int main()
{
int nums[10];
nums[11]=52;
cout<<nums[11];
cin.get();

return 0;
}
That's because neither C or C++ check what you're indexing into. That assignmnet can be writing to pretty much anything in memory.

Nintendofreak88 - January 25, 2005 10:50 PM (GMT)
Well there you're writing past the end of the array into some other memory, probably not a good idea unless you plan on crashing your system. ;)

EDIT: Aww, KTC beat meh to it... :(

donprogc++ - January 25, 2005 11:33 PM (GMT)
remember that an array is technically a pointer
so nums[11] would point somewhere else in memory as already stated

KTC - January 25, 2005 11:42 PM (GMT)
Array is NOT the same thing as a pointer. It's almost always substitutable with one another, but not all the times. (Just a technical point.)

ih8censorship - January 26, 2005 12:07 AM (GMT)
well thanks for the advice guys, probly prevented a future bug in something id make ^_^

aab - January 28, 2005 05:58 PM (GMT)
what donprogc++ was saying, is that an array is created as an area of memory, where the OS uses a value to read from as the start of the array (a pointer, though not used by the coder as a pointer), and then reads
(size of each element)*(element subscript ie 5 in : 'array[5]=')
forward in memory, then reads a value (size of each element).

donprogc++ - January 28, 2005 06:23 PM (GMT)
Thanks for explaining it aab ^_^




* Hosted for free by InvisionFree