Memory allocation

This is a discussion on Memory allocation within the C++ Programming forums, part of the General Programming Boards category; Here is a question which has been bugging me for awhile. In the following example, localtime() returns a pointer to ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934

    Memory allocation

    Here is a question which has been bugging me for awhile. In the following example, localtime() returns a pointer to a tm struct (time_info). Should I deallocate this memory, or is this handled automatically?
    Code:
    #include <iostream>
    #include <time.h>
    
    using namespace std;
    
    int main(void)
    {
       time_t t;
       struct tm *time_info;
       char ctime[80];
    
       t = time(NULL);
       time_info = localtime(&t);
       strftime(ctime,sizeof(ctime),"%c",time_info);
       cout << ctime << endl;
    
       return 0;
    }

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I can't say for sure, since I've never used it before, but I believe that usually you don't deallocate things that you don't allocate yourself. It sounds sort of like Winsock's HOSTENT struct, where there's 1 HOSTENT allocated per application (or something like that), which you must never deallocate.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The structure is statically allocated and therefore you don't deallocate the memory as you would for dynamically allocated memory.
    Last edited by 7stud; 08-07-2003 at 05:07 PM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Thanks guys. So this would be a good way to return a pointer? For example if you wanted to make a string concatenation function:
    Code:
    char *concat(char *a, char *b)
    {
       static char *result;
    
       result = new char[strlen(a)+strlen(b)+1];
       strcpy(result,a);
       strcat(result,b);
       return result;
    }

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    result = new char[strlen(a)+strlen(b)+1];

    Whenever you use new, you are dynamically allocating memory, and therefore you must de-allocate that memory using delete.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    If u said:
    Code:
    char *concat(char *a, char *b)
    {
       static char result[THE_MAX_LEN];
    
       strcpy(result,a);
       strcat(result,b);
       return result;
    }
    Then that would be ok (I think), but then you'd have to have that messy #define at the top, and you'd have to allocate as much memory as you might possibly need, which is generally a lot higher than the amount of memory that you usually DO need. So, I say stick with your way, and call delete[] on the returned char* once you're done with it.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Ok I see that the static does me absolutely zero good in my example. I still have to delete the memory. And with Hunter's example, no new(), therefore no delete() is needed. But as you pointed out Hunter, it's messy because you may waste a lot of memory.

    Regards,
    swoopy

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    There really is little reason to do any tricks anymore. The C++ standard library provides a decent way to return dynamically allocated objects (std::auto_ptr) although there are limitations. It provides a great way to return char or wchar_t arrays (std::string or std::wstring). And it provides a great way to return arrays of any other type than bool (std::vector). You can use the library I refer people to the most, Boost, which offers a better way to return pointers to objects (boost::shared_ptr), as well as a vast number of other things. Boost was created by many of the same smart folks who developed the STL, and it's very likely that many parts of Boost will be in the standard library sooner or later.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I need to get more comfortable with using the tools provided in the STL (strings, vectors, maps, iterators). I sure wish I had a good reference for the STL.

    And thanks for the Boost link Cat.

  10. #10
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212

    no struct declared

    In your example

    Code:
    struct tm *time_info;
    You have not allocated a structure. You have allocated a 4-byte pointer that can hold the address of a structure.

    localtime() returns a pointer to a structure it has allocated. Only your documentation for this API call will tell you if this memory is static, or dynamically allocated.

    If it's static, you don't dispose of it. If it is dynamic, you do.

    Remember to _always_ zero your pointers after you dispose of a RAMblock-- this makes sure you don't have invalid, dangling pointers.
    It is not the spoon that bends, it is you who bends around the spoon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 10:41 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 04:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 07:36 AM

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