Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a program in VC6. There I have an integer variable and a long pointer as follows

int x = 50;
long *y;
y = new long[x];

Is there anything wrong in the initialization for y, because i am getting memory leak it. Pls provide some help.
Posted

1 solution

y is not a long pointer, but a pointer to a long; but perhaps you meant that.

The expression
C++
new long[x]

allocates an array of x elements each of type long and returns a pointer to the first element. Hence the answer is: No, there is nothing wrong with storing that pointer in your y.

The memory leak will probably result from the fact that you forgot to free that space again. Somewhere in your program you should have a statement like
C++
delete[] y;

to free that storage.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jul-13 2:05am    
Correct, 5ed.
—SA
nv3 22-Jul-13 2:41am    
Thank you, Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900