anything wrong with this code?

H

hijkl

hey guys
anything wrong with this code??
if it is then what?

int *array(int n){
return new int(n);
}
int main(){
int *p = array(10);
for( int i = 0; i < 10; i++ ) {
p = 0;
}
printf( "%d\n", p[0] );
p = array(10);
printf( "%d\n", p[0] );
return 0;
}
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

hey guys
anything wrong with this code??
if it is then what?

First of all you are using pointers, which is generally no a good
idea. And second, you use an array, we generally prefer that you use a
vector.
int *array(int n){
return new int(n);

return new int[n];
}
int main(){
int *p = array(10);
for( int i = 0; i < 10; i++ ) {

It is often preferred to use ++i instead, which is at least as fast as
i++ but can potentially be faster, especially when working with
iterators instead of integers.
p = 0;
}
printf( "%d\n", p[0] );


Memory leak, you forgot to free the memory you allocated on the first
call to array(), insert delete[] p;
p = array(10);
printf( "%d\n", p[0] );
return 0;
}

I fail to see the point of your code, is it to show that a variable
allocated with new is uninitialized?
 
M

mimi

hey guys
anything wrong with this code??
if it is then what?

int *array(int n){
return new int(n);
}
This is equal to
int *array(int n)
{
int *i = new int;
*i = 10;
return i;
}
Surely, the functionality is not matching the function name.
int main(){
int *p = array(10);
for( int i = 0; i < 10; i++ ) {
p = 0;
}
printf( "%d\n", p[0] );
p = array(10);
printf( "%d\n", p[0] );
return 0;
}
 
I

Ian Collins

hijkl said:
sorry but u misunderstood.
array is a function name i think ..
Misunderstood what? Leave some context and drop the silly abbreviations.
 
I

Ian Collins

hijkl said:
hey guys
anything wrong with this code??
if it is then what?
It won't compile and even if it did, it leeks memory.
int *array(int n){
return new int(n);
}
int main(){
int *p = array(10);
for( int i = 0; i < 10; i++ ) {
p = 0;
}
printf( "%d\n", p[0] );
p = array(10);
printf( "%d\n", p[0] );
return 0;
}
 
H

hijkl

ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.

thanks
sanjay
 
J

John Harrison

hijkl said:
ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.

thanks
sanjay

Only two int pointer that need to be freed.

Your main misunderstanding seem to be that you think this code

int *array(int n){
return new int(n);
}

allocates n integers. It only allocates *one*. If you want to allocate n
integer you write it like this

int *array(int n){
return new int[n];
}

[n] not (n)

john
 
I

Ian Collins

hijkl said:
ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.
What program? Please keep enough context for your reply to make sense.
 
H

hijkl

hijkl said:
ok i tried running this program..it will run fine only problem with
this code is memory leak..
there atleast 12 int pointers that needs to free at the end.
thanks
sanjay

Only two int pointer that need to be freed.

Your main misunderstanding seem to be that you think this code

int *array(int n){
return new int(n);
}

allocates n integers. It only allocates *one*. If you want to allocate n
integer you write it like this

int *array(int n){
return new int[n];
}

[n] not (n)

john
 
H

hijkl

Only two int pointer that need to be freed.

Your main misunderstanding seem to be that you think this code

int *array(int n){
return new int(n);
}

allocates n integers.

HI JOhn
i understand that this code allocates only one intger.
but

for( int i = 0; i < 10; i++ ) {
p = 0;
}
this code will initialize next 9 integer to consecutive locations..
if you will debug then u will notice that..
let me know if you not agree with me.
 
I

Ian Collins

hijkl said:
Only two int pointer that need to be freed.

Your main misunderstanding seem to be that you think this code

int *array(int n){
return new int(n);
}

allocates n integers.

HI JOhn
i understand that this code allocates only one intger.
but

for( int i = 0; i < 10; i++ ) {
p = 0;
}
this code will initialize next 9 integer to consecutive locations..
if you will debug then u will notice that..
let me know if you not agree with me.

No, it will write over what ever lives in memory after the single
integer you have allocated.
 
I

Ian Collins

hijkl said:
same program i posted
On Usenet, each post should make sense on its own, so keep enough
context in your replies. You should also snip people's signatures (the
bit after "-- ").
 
H

hijkl

Only two int pointer that need to be freed.
Your main misunderstanding seem to be that you think this code
int *array(int n){
return new int(n);
}
allocates n integers.

HI JOhn
i understand that this code allocates only one intger.
but

for( int i = 0; i < 10; i++ ) {
p = 0;
}
this code will initialize next 9 integer to consecutive locations..
if you will debug then u will notice that..
let me know if you not agree with me.- Hide quoted text -

- Show quoted text -


John
try following code and you will understand how its working
....
for( int i = 0; i < 10; i++ ) {
p = i;
}

printf("%d", [0]);
printf("%d", [1]);
printf("%d", [2]);
printf("%d", [3]);
.
.
.
printf("%d", [9]);
....
the output will be
0
1
2
..
..
..
9

so its cleary says that it allocated 10 integers.

thanks
sanjay
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

HI JOhn
i understand that this code allocates only one intger.
but
for( int i = 0; i < 10; i++ ) {
p = 0;
}
this code will initialize next 9 integer to consecutive locations..
if you will debug then u will notice that..
let me know if you not agree with me.- Hide quoted text -

- Show quoted text -

John
try following code and you will understand how its working
...
for( int i = 0; i < 10; i++ ) {
p = i;
}

printf("%d", [0]);
printf("%d", [1]);
printf("%d", [2]);
printf("%d", [3]);
.
.
.
printf("%d", [9]);
...
the output will be
0
1
2
.
.
.
9

so its cleary says that it allocated 10 integers.


It's just luck that keeps this code from crashing, or doing something
worse. What you have is undefined behaviour, to see that just consider
what would happen if the integer you allocated were located on the
last addressable memory location?
 
S

Sarath

hey guys
anything wrong with this code??
if it is then what?

int *array(int n){
return new int(n);
}
int main(){
int *p = array(10);
for( int i = 0; i < 10; i++ ) {
p = 0;
}
printf( "%d\n", p[0] );
p = array(10);
printf( "%d\n", p[0] );
return 0;
}


Seems you are trying to do the following
int *array(int n)
{
return new int[n];
}
int main()
{
UINT ARRAY_SIZE = 10;
int *p = array(ARRAY_SIZE);
memset( array, 0, ARRAY_SIZE))
printf( "%d\n", p[0] );
/* This statement will make the previous allocated memory orphan.
by doing this you are leaving handle the memory location where an
array of
integer pointers allocated. p will take any address which we are
assigning.
putting a delete []p; will avoid memory leak problems
*/
p = array(10);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top