Thread: char* returning question

  1. #16
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by kermit
    Code:
    char *s = (char *) malloc(100);
    Why the cast to char?
    well, I guess I'm just used to cast to (char *), from all the books and examples I've ever read.
    But I understand that it's not really necessary with C, but to C++ which is more restrictive with casting.


    One last question, lately I've been looking into memory leaks, and I understand that it's important to free allocated memory; But what if I don't allocate memory with a function like malloc(), but simply do:
    Code:
    char str[100];
    I know I can't free this kind of string myself, but can I really trust the OS to do it on it's own?
    If so, why do I need to allocate manually any way?


    Thank again for all your help
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #17
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    can I really trust the OS to do it on it's own?
    yes, you can.

    If so, why do I need to allocate manually any way?
    There are a couple reasons. The first is that if you just do
    Code:
    char str[100]
    The str array goes out of scope once the function it was declared in returns (As was explained by others in this thread already). If you need a variable with a lifespan longer than the life of the function it was declared in, then you can use malloc().
    Another reason you might need to allocate memory with malloc() is that this way allocates memory from the heap instead of the stack. Programs have limited stack space, so if you need a very large variable, you need to use malloc(). For instance, the following declaration would probably crash your program.
    Code:
    char buff[10000000];
    If instead you ran
    Code:
    char *buff = malloc(10000000);
    that should work just fine.

  3. #18
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by bithub
    Another reason you might need to allocate memory with malloc() is that this way allocates memory from the heap. Programs have limited stack space, so if you need a very large variable, you need to use malloc(). For instance, the following declaration will probably crash your program.
    Then where is the malloc() memory is allocated at?
    And what is the difference between it this type of memory and the heap?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The stack builds up. The heap builds down. Then they crash into eachother and your program dies. In the mean time, malloc goes and plays off some place else, and causes a huge memory lead when your stack crashes into the heap. Or maybe not. (edit:Here ya go.)

    "Damn you, mixed drink! Damn you!"


    Quzah.
    Last edited by quzah; 08-28-2005 at 03:21 AM.
    Hope is the first step on the road to disappointment.

  5. #20
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If so, why do I need to allocate manually any way?
    Try the following program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int scores[50]; /* How big should this be??? */
      int num_scores;
      int i;
    
      printf("How many scores (1 - 50)? ");
      fflush(stdout);
    
      scanf("%d", &num_scores);
    
      for(i = 0;i < num_scores;++i)
        scanf("%d", &scores[i]);
    
      return 0;
    }
    How big are you going to make the array? One person might just want to enter 10, another person might want to enter 5000. What are you going to do? Make the array as big as possible to make sure there's enough room? That would definitely be a waste of memory if someone only wanted to enter a few.

    Dynamic allocation to the rescue!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      int *scores;
      int num_scores;
      int i;
    
      printf("How many scores (1 - 50)? ");
      fflush(stdout);
    
      scanf("%d", &num_scores);
    
      if(!(scores = malloc(sizeof(*scores) * num_scores)))
      {
        puts("Memory allocation error!");
        return 1;
      }
    
      for(i = 0;i < num_scores;++i)
        scanf("%d", &scores[i]);
    
      free(scores);
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. question about returning an array
    By Jasonymk in forum C++ Programming
    Replies: 6
    Last Post: 08-07-2003, 09:37 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM