Thread: static character pointer within a function

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    85

    Question static character pointer within a function

    Hi All,

    I am not sure all static variables define locally within a function is putting on stack memory or on the heap or global memory space.
    Does anyone know?
    Thanks!
    DV007

    Code:
    class Foo
    {
       public:
       Foo();
       void convert_message_to_string()
       {
          static char * strbuffer;    // is strbuffer on stack or heap after function return???
         // convert an unpredict size of message struct into string, then return . With restriction of not allow using New operator or Malloc(). Gosh!  
       }
    
      private:
       struct MessageType message;
    
    };
    Last edited by dv007; 02-09-2006 at 11:49 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    All static variables are located in the program's data segment where other globals reside.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Note that the variable is just an uninitialized pointer. What it points to depends on what you assign to it. If you use new to allocate space for the buffer, then that space resides on the heap. If you assign the address of an existing variable or literal then the pointer might point to memory somewhere else.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Daved
    Note that the variable is just an uninitialized pointer.
    Isn't it implicitily initialized to a null pointer because it has static storage duration?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm not sure. You are probably right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 07:23 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM