Dynamic memory

This is a discussion on Dynamic memory within the C Programming forums, part of the General Programming Boards category; Hi guys, I would like to check with all of you. Let say if I would like to do something ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    58

    Post Dynamic memory

    Hi guys,

    I would like to check with all of you. Let say if I would like to do something inside a function. Will it be better if I delcare a dynamic memory then using a static one?

    Let say if I would like to store the value of the array into a temp array inside a function. How can I do it? will it be something like that? Do I need to add a null at the back of the char array ? what was the best way to do it ?

    Code:
    void function(char* item1)
    {
       unsigned int i;
    
       char *p = (char*)malloc(strlen(item1) * (char))
     
        for(i=0;i<strlen(item1);i++){
                  
        p[i] = item1[i];
    
       }
       
    
       // some code 
       free(p); 
    }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Dynamic memory will probably be most appropriate in this situation, and you deal with it mostly appropriately. Few minor things:

    1) Yes, you do need strlen + 1 for the extra null-byte.
    2) You don't want to check to strings length on each loop iteration if it is unchanging. Initialize a variable with strlen(item) and use it in the loop condition.
    3) Don't cast the return value of malloc.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    Thanks Tonto.

    By the way, what do you mean by cast a return value? I'm pretty new so if you don't mind could you briefly explan what you mean by that ?

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    ptr = (char *) malloc();
    -------^^^^^

    That'd be a cast. Kind of like when you'd want to force an int to a char:

    ch = (char) intvar;

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    Oh... I get it ..Thanks kennedy

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    Just one more thing, let say if I want to allocate memory to a pointer. Must I always check that whether there is enough memory? what will be ideal way to deal with pointer?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int *ptr;
    ...
    ptr = malloc( somesize * sizeof *ptr ); /* try to allocate some integers... */
    
    if( ptr == NULL )
    {
        ...malloc failed, that's bad...
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Just one more thing, let say if I want to allocate memory to a pointer. Must I always check that whether there is enough memory? what will be ideal way to deal with pointer?
    Its better to allocate memory and check if the allocation was successful or not. Like Quzah's example. The pointer will remain NULL in condition of a failed memory allocating.
    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. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 09:57 PM
  2. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  3. 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
  4. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 03:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 09:52 AM

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