By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,440 Members | 1,411 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,440 IT Pros & Developers. It's quick & easy.

can you help in this programs?

P: 23
i want to know what happens when running these programs:
void main()
{
int *p=new int[10];
while(p)
p=new int[1000];
*p=99;
cout<<*p<<endl;
}


void main()
{ double*p;
while(p!=null)
{
p=new double[5000];
p=new double[10];
for(inti=0; i<10;i++)
cin>>p[i];
for(i=0;i<10;i++)
}
cout<<"p["<<i<<"]"<<p[i]<<endl:
}
Dec 26 '06 #1
Share this Question
Share on Google+
3 Replies


P: 30
i want to know what happens when running these programs:
void main()
{
int *p=new int[10];
while(p)
p=new int[1000];
*p=99;
cout<<*p<<endl;
}


void main()
{ double*p;
while(p!=null)
{
p=new double[5000];
p=new double[10];
for(inti=0; i<10;i++)
cin>>p[i];
for(i=0;i<10;i++)
}
cout<<"p["<<i<<"]"<<p[i]<<endl:
}


for the first one

first p will adress a [10] int array then
it will enter the while loop
- it will adress a [1000] int array
-the first element of the [1000] array will become 99 thenh it will write it on the consol
-then the while will repeat it self until you'll go out of memory
because the arrays are created but aren't deleted and they continue to exeits although p isen't pointing at the actualy there is no pointer to them
the spaces you alocated for the arrays will become memory holes
then you'll problebly receve an error mesage


for the second one

i never used " null" if you mean " (p!=0) "
you'll probably get a "The variable 'p' is being used without being defined. " error
and it won't compile


who wrote these programs??? and why ??????? :)
i supose you didn't wrote these becose if you did you wouldn't had asked :)
Dec 26 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
for the first one

first p will adress a [10] int array then
it will enter the while loop
- it will adress a [1000] int array
-the first element of the [1000] array will become 99 thenh it will write it on the consol
-then the while will repeat it self until you'll go out of memory
because the arrays are created but aren't deleted and they continue to exeits although p isen't pointing at the actualy there is no pointer to them
the spaces you alocated for the arrays will become memory holes
then you'll problebly receve an error mesage
You haven't got this quite right, note there is no { following the while so in fact what it does is repeatedly allocate an array of memory until it runs out of memory. Then it writes 99 to the pointer returned from new when the last memory allocation failed, i.e. it writes to a NULL pointer and then it reads from the same NULL pointer.

This invokes undefined behaviour

The second example does not compile because it is has at least 2 obvious syntax errors (I stopped looking after that). It therefore does nothing.
Dec 26 '06 #3

P: 30
You haven't got this quite right, note there is no { following the while so in fact what it does is repeatedly allocate an array of memory until it runs out of memory. Then it writes 99 to the pointer returned from new when the last memory allocation failed, i.e. it writes to a NULL pointer and then it reads from the same NULL pointer.

This invokes undefined behaviour

The second example does not compile because it is has at least 2 obvious syntax errors (I stopped looking after that). It therefore does nothing.

yep silly me :D I should wear my glaces more often @-)
Dec 26 '06 #4

Post your reply

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