Memory Question

This is a discussion on Memory Question within the C++ Programming forums, part of the General Programming Boards category; When we define an array C allocates needed memory to it. Code: char c[10]; This allocates 10 bytes of memory. ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    Question Memory Question

    When we define an array C allocates needed memory to it.
    Code:
    char c[10];
    This allocates 10 bytes of memory. When should we use malloc() or new()?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    You use new when you are not sure how much memory you want to allocate while compiling your program. eg: you want the user(or a file from which you might read data) to specify the size of the array...
    I don't think any one uses malloc in c++ programs...

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    There may be times when you don't know at compile time weather memory should be allocated or not, for example
    Code:
    MyClass *CreateObject()
    {
       if (NeedMoreObjects())
       return new MyClass;
       return 0;
    }

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You also use it when something is supposed to survive the ending of a function:
    Code:
    void func()
    {
      char array[10];
    } // With this line, the memory is gone again.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    OK Thanks
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    also if the data will happen be too large for the stack..

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Take a look at my thread, "I use stupid thread titles".
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. heap vs stack memory question
    By donglee in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2009, 03:34 PM
  2. Pointer memory question
    By Edo in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2009, 02:36 AM
  3. Memory question
    By John_L in forum Tech Board
    Replies: 8
    Last Post: 06-02-2008, 10:06 PM
  4. Another Dynamic Memory Question
    By SirCrono6 in forum C++ Programming
    Replies: 6
    Last Post: 03-02-2005, 11:10 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM

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