Thread: free() Memory Question

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by rogster001 View Post
    isn't 100,000 out of bounds for an integer? thought it was 16,000, unless i'm the dinosaur;->
    For either short or int, -32767 to +32767 is guaranteed.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For signed, yes. For unsigned, that would be 0 - 65535.
    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.

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by guillermoh View Post
    I'm trying to solve this:
    to solve the 1st - just add () to function to indicate the call of the function

    About the second - when you return something like

    return "some const string";

    you returning pointer to const string that will not be destructed, so its OK...

    If you are creating the string inside your function - its another story
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #19
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    I understand. So this would be fine

    Code:
    const char *text[] = {"memory","information"};
    
    
    
    int main()
    {
          while(i<100)
          {
                ...
                printf("Example &#37;s\n",function());
                ...
                i++;
           }
    
          return 0;
    }
    
    char* function ()
    {
           char* data;
           ...
           data = text[0];
           ....
           return data;
    }
    Thanks for you help!

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it would work fine, but not the best solution. To return constant data, consider making it static to your function instead.
    That way you avoid global variables.
    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.

  6. #21
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by guillermoh View Post
    I understand. So this would be fine
    You should declare function as
    const char* function(void);
    in this case
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #22
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    Thanks.

    What about

    static const char* string[] = .... ?

  8. #23
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by guillermoh View Post
    Thanks.

    What about

    static const char* string[] = .... ?
    its ok

    Code:
    const char* sHelloWorld(void)
    {
       static const char* hello = "Hello, World!";
       return hello;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > static const char* string[] = .... ?

    Did you really want an array of read-only strings?

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by citizen View Post
    > static const char* string[] = .... ?

    Did you really want an array of read-only strings?
    Probably, because that's what they would be even without const and static - string literals are non-writable in most modern OS's.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #26
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by matsp View Post
    Probably, because that's what they would be even without const and static - string literals are non-writable in most modern OS's.

    --
    Mats
    but with const you will get warning from compiler when trying to assign pointer to this string to the regular char* pointer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #27
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by matsp View Post
    Probably, because that's what they would be even without const and static - string literals are non-writable in most modern OS's.

    --
    Mats
    Well aware of that, except that most of the examples up to now have been simple variables and not arrays. The OP might have been confused by vart's example. He demonstrated an entirely different type and its behavior.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about memory
    By simo_mon in forum C Programming
    Replies: 7
    Last Post: 03-20-2009, 08:02 AM
  2. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  3. Memory question
    By John_L in forum Tech Board
    Replies: 8
    Last Post: 06-02-2008, 10:06 PM
  4. OS out of memory if malloc without free?
    By hkuser2001 in forum C Programming
    Replies: 7
    Last Post: 04-24-2006, 07:23 AM
  5. Question regarding constructors and memory.....
    By INFERNO2K in forum C++ Programming
    Replies: 6
    Last Post: 05-25-2005, 11:30 AM