Thread: When do we need malloc????

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    101

    When do we need malloc????

    When do we need malloc????
    for example, I declare
    char *str1;
    char *str2;
    str1="Testing string 1";
    strcpy(str2,str1);
    Can I do that???
    or I need to have some memory for str2 by:
    str2=(char*) malloc(strlen(str1)+1) ????
    if no need to do that, when we need "malloc"??
    Thx......

  2. #2
    duck-billed platypus
    Guest

    Talking

    Actually, it might be nice to declare space for str1 also. Since this is the c++ board, why not use 'new' instead of 'malloc'

    Code:
    char *str1 = "Testing string 1";  // atomatically reserves enough space
    char str2;
    
    str2 = new char[strlen( str1 ) + 1];
    
    strcpy( str2, str1 );

  3. #3
    Unregistered
    Guest
    It's needed for run-time memory allocation.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Originally posted by Unregistered
    It's needed for run-time memory allocation.
    I'm sorry..... What do u mean?

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>I'm sorry..... What do u mean?

    It means creating memory on the fly..... You can allocate memory dynamically while your program runs.......If more is needed, malloc will provide more....

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Like this

    > I'm sorry..... What do u mean?

    Static allocation:

    int Array[10];

    Memory for 10 elements are allocated, no more, no less, whatever happens in the program.
    You must use a constant expression, so you cannot do like this:

    int Array[Variable];

    Dynamic allocation:

    int* Array;
    int NrOfElements;
    cin >> NrOfElements;
    Array=new int[NrOfElements];
    ...
    delete[] Array;

    The user can specify how much memory that should be allocated when running the program, not only when coding.
    Just remember to remove the allocated memory with delete.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    8
    You have the NEW and DELETE operators for dynamic allocation. Check the compiler's doc for more info.

  8. #8
    I thought you don't need malloc() unless you're using C.

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    reply

    malloc() is for C and new is for C++
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    then why are we discussing this in C++ forum instead of C forum?

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Well

    Well, you can use malloc() in C++ if you want to. It's not really wrong...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    C don't have 'new' and 'delete' , right?
    I'm sorry to ask C instead of C++ here..... I don't know it since u guys said it's C.... but can u help me here for this question only? thx!!


    I know there are new in C++..........
    but when we need it??? As I don't know when there are Segmentation fault if not to use malloc, and I'm worry about memory leakage if forget free the memory.

    Thanks!!~

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    I write a simple C program to explain how to use "malloc" and "free".

    #include "stdlib.h"
    #include "stdio.h"
    #include "malloc.h"

    void main( void )
    {
    char *string; /* dynamic length */

    /* If you don't allocate (or not enough) any memory space for "string", you cannot assign any value to "string". Otherwise, the memory Segment may be crashed (uncertainty of when). */

    string = malloc(1000);

    if( string == NULL )
    printf( "Insufficient memory available " );
    else
    {
    printf( "Memory space allocated successfully " );

    /* If "string" will not be used to perform other calculations, you should free it at once because "string" will hold the system recourse. If there are so many unfree memory segments, the system will be unstable due to less system resource available */

    free( string );

    printf( "Memory freed " );
    }
    }

    Is that correct?? Can anyone helps?? thx!!

  14. #14
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Example

    Originally posted by DramaKing
    C don't have 'new' and 'delete' , right?
    I'm sorry to ask C instead of C++ here..... I don't know it since u guys said it's C.... but can u help me here for this question only? thx!!


    I know there are new in C++..........
    but when we need it??? As I don't know when there are Segmentation fault if not to use malloc, and I'm worry about memory leakage if forget free the memory.

    Thanks!!~
    Code:
    
    int main()
    {
      int* Array; //Create pointer
      Array=new int[256]; //Allocate memory for 256 ints
    
      if(Array!=NULL) //Checks if allocation was successful, if not the pointer has the value NULL
      {
    
        //Whatever you want to do with the array...
    
        delete[] Array; //Deallocate memory, but only if it has been allocated first. Otherwise it might crash...
      }
    
      return 0;
    }
    
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM