anything wrong with this code?

Discussion in 'C++' started by hijkl, Mar 7, 2007.

  1. hijkl

    hijkl Guest

    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;
    }
    hijkl, Mar 7, 2007
    #1
    1. Advertising

  2. On 7 Mar, 08:21, "hijkl" <> wrote:
    > 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?

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 7, 2007
    #2
    1. Advertising

  3. hijkl

    mimi Guest

    On 3ÔÂ7ÈÕ, ÏÂÎç3ʱ21·Ö, "hijkl" <> wrote:
    > 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;
    > }
    mimi, Mar 7, 2007
    #3
  4. hijkl

    hijkl Guest

    sorry but u misunderstood.
    array is a function name i think ..
    hijkl, Mar 7, 2007
    #4
  5. hijkl

    hijkl Guest

    or i am not about array()
    hijkl, Mar 7, 2007
    #5
  6. hijkl

    hijkl Guest

    mimi
    but that doesnt matters..its same thing wat u mentioned/
    hijkl, Mar 7, 2007
    #6
  7. hijkl

    Ian Collins Guest

    hijkl wrote:
    > sorry but u misunderstood.
    > array is a function name i think ..
    >

    Misunderstood what? Leave some context and drop the silly abbreviations.

    --
    Ian Collins.
    Ian Collins, Mar 7, 2007
    #7
  8. hijkl

    Ian Collins Guest

    hijkl wrote:
    > 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;
    > }
    >


    --
    Ian Collins.
    Ian Collins, Mar 7, 2007
    #8
  9. hijkl

    hijkl Guest

    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
    hijkl, Mar 7, 2007
    #9
  10. hijkl wrote:
    > 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
    John Harrison, Mar 7, 2007
    #10
  11. hijkl

    Ian Collins Guest

    hijkl wrote:
    > 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.

    --
    Ian Collins.
    Ian Collins, Mar 7, 2007
    #11
  12. hijkl

    hijkl Guest

    On Mar 7, 12:08 am, Ian Collins <> wrote:
    > hijkl wrote:
    > > 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.
    >
    > --
    > Ian Collins.


    same program i posted
    hijkl, Mar 7, 2007
    #12
  13. hijkl

    hijkl Guest

    On Mar 7, 12:07 am, John Harrison <> wrote:
    > hijkl wrote:
    > > 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
    hijkl, Mar 7, 2007
    #13
  14. hijkl

    hijkl Guest

    On Mar 7, 12:07 am, John Harrison <> wrote:
    > hijkl wrote:
    > > 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.


    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.
    hijkl, Mar 7, 2007
    #14
  15. hijkl

    Ian Collins Guest

    hijkl wrote:
    > On Mar 7, 12:07 am, John Harrison <> wrote:
    >>
    >>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.

    --
    Ian Collins.
    Ian Collins, Mar 7, 2007
    #15
  16. hijkl

    Ian Collins Guest

    hijkl wrote:
    > On Mar 7, 12:08 am, Ian Collins <> wrote:
    >
    >>hijkl wrote:
    >>
    >>>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.
    >>

    >
    > 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 "-- ").

    --
    Ian Collins.
    Ian Collins, Mar 7, 2007
    #16
  17. hijkl

    hijkl Guest

    On Mar 7, 1:09 am, "hijkl" <> wrote:
    > On Mar 7, 12:07 am, John Harrison <> wrote:
    >
    >
    >
    >
    >
    > > hijkl wrote:
    > > > 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.

    >
    > 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
    hijkl, Mar 7, 2007
    #17
  18. hijkl

    Ian Collins Guest

    hijkl wrote:
    > On Mar 7, 1:09 am, "hijkl" <> wrote:
    >
    >>On Mar 7, 12:07 am, John Harrison <> wrote:
    >>
    >>>Your main misunderstanding seem to be that you think this code

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

    >>
    >>>allocates n integers.

    >>

    <snip>
    >
    > so its cleary says that it allocated 10 integers.
    >

    No, it clearly shows that 10 unfortunate memory locations have been
    written over.

    --
    Ian Collins.
    Ian Collins, Mar 7, 2007
    #18
  19. On 7 Mar, 10:26, "hijkl" <> wrote:
    > On Mar 7, 1:09 am, "hijkl" <> wrote:
    >
    >
    >
    > > On Mar 7, 12:07 am, John Harrison <> wrote:

    >
    > > > hijkl wrote:
    > > > > 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.

    >
    > > 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?

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 7, 2007
    #19
  20. hijkl

    Sarath Guest

    On Mar 7, 4:21 pm, "hijkl" <> wrote:
    > 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;
    }
    Sarath, Mar 7, 2007
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mano kumar
    Replies:
    2
    Views:
    346
    Kevin Spencer
    Oct 17, 2003
  2. Replies:
    5
    Views:
    484
  3. Chris
    Replies:
    7
    Views:
    453
    BigBrian
    Feb 9, 2005
  4. jim

    anything wrong with this code?

    jim, Aug 25, 2004, in forum: C Programming
    Replies:
    5
    Views:
    305
    Mark A. Odell
    Aug 26, 2004
  5. Anything wrong with this code?

    , Aug 16, 2007, in forum: C Programming
    Replies:
    3
    Views:
    339
    CBFalconer
    Aug 17, 2007
Loading...

Share This Page