Help with Dynamic memory

This is a discussion on Help with Dynamic memory within the C++ Programming forums, part of the General Programming Boards category; I wanted to know if I want an dynamic array so which method is more suitable and why is it ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    32

    Question Help with Dynamic memory

    I wanted to know if I want an dynamic array so which method is more suitable and why is it more suitable

    method 1:

    int *p,x;

    p= new int;

    cin>>x;//input the limit of array

    for(int i=0; i<x; ++i)
    cin>>p[i];

    method 2:

    int *p,x;

    cin>>x;//input the limit of array

    p= new int[x];

    for(int i=0; i<x; ++i)
    cin>>p[i];


    and when we follow this method where does the value of input elemet store (my guess is buffer)

    int *p,x;

    cin>>x;//input the limit of array

    for(int i=0; i<x; ++i)
    cin>>p[i];

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Any special reason you want to use an array here vice say a std::vector?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Quote Originally Posted by Dr.Xperience
    I wanted to know if I want an dynamic array so which method is more suitable and why is it more suitable
    Unless by chance x == 1, the first method is plain wrong. The second method is on the right track, except that x might not be positive, yet there was no error checking.

    Furthermore, consider using a std::vector<int> instead of managing memory yourself.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    32
    hmm but if you use a primitive compiler like turbo C++ it doesn't give an error and ouput is also seems to be alright when x==1. And thank you all for your time and comments. I just wanted to manipulate memory manually so I asked for the help. hmm we have just learnt the basics in our university. one more thing confuse me how can we see the output of the program without using borland extentions like getch().

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Quote Originally Posted by Dr.Xperience
    hmm but if you use a primitive compiler like turbo C++ it doesn't give an error and ouput is also seems to be alright when x==1.
    x == 1 is a case where the code should work. It is more interesting if x > 1, but even if the code "works", it is still wrong.

    Quote Originally Posted by Dr.Xperience
    one more thing confuse me how can we see the output of the program without using borland extentions like getch().
    Run your program from a command prompt.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    32
    Quote Originally Posted by laserlight View Post
    Run your program from a command prompt.
    Impressive never thought of that. I wonder how is ANSI C++ without borland extentions. If you may please reffer me any book or site from which I can learn core aspects of ANSIc++. I mean I can never image making a program without using clrscr(), gotoxy() etc. etc.
    Thank You again for your comment.
    I will be studying vector now.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Quote Originally Posted by Dr.Xperience
    If you may please reffer me any book or site from which I can learn core aspects of ANSIc++.
    I recommend Accelerated C++.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    32
    Thank You very much

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    You should not be studying ANSI C++. You should be studying ISO C++, since the latest standards of the language are managed by ISO, and not ANSI. The latest version being C++11, which is close to being finalized.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    32
    I came across the fact but still until they are in practise it will take some time to reach India more specifically our university.

  11. #11
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Dr.Xperience View Post
    I came across the fact but still until they are in practise it will take some time to reach India more specifically our university.
    If you adhere to what is taught here, you'll be stuck in the stone ages for ever.

    Seriously, use a GOOD compiler, learn from MODERN books, and argue the point with *everyone* you meet.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    32
    Thanks for the advice . Infact to do so I have joint this forum to know new things. To learn more by myself.

  13. #13
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Dr.Xperience View Post
    Thanks for the advice . Infact to do so I have joint this forum to know new things. To learn more by myself.
    Now write some good programs, looking up the references whenever you're in doubt.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  14. #14
    Registered User
    Join Date
    Sep 2010
    Posts
    32
    Yup thank you and by modern books which book you mean

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Laser recommended a good one, and we have a list of book recommendations that you can browse through to find one that suits you best.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  2. dynamic memory
    By Gabi in forum C Programming
    Replies: 9
    Last Post: 06-20-2010, 05:41 PM
  3. Dynamic Memory
    By misplaced in forum C++ Programming
    Replies: 2
    Last Post: 03-28-2005, 01:10 AM
  4. static memory and dynamic memory
    By nextus in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2003, 07:46 PM
  5. dynamic memory
    By falconetti in forum C Programming
    Replies: 2
    Last Post: 12-21-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21