Thread: Memory Allocation using Pointers

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    30

    Question Memory Allocation using Pointers

    Hi all,

    I want to create a pointer to a C-String like so:
    Code:
    char *pCString = "This is a string";
    and then delete the memory reserved for the C-String:
    Code:
    delete pCString;
    From what I understand, this just deletes the memory pointed to by pCString. So couldn't I still use this pointer? I mean, reallocate memory for another string like so:
    Code:
    pCString = "This is another string";
    Here is an example of the full source code:
    Code:
    #include <iostream>
    
    int main()
    {
    	char *pC_String = "This is a string";
    	std::cout << pC_String << std::endl;
    	delete pC_String;
    	pC_String = "This is another string";
    	std::cout << pC_String << std::endl;
    
    	system("PAUSE");
    	return 0;
    }
    Now, i'm pretty darn sure that when I try to allocate memory for the new C-String, my program crashes. What am I missing? I want to be able to point to another value without keeping the other value in memory.

    If I was unclear, please say so and I will try to reiterate my question.

    Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    char *pC_String = "This is a string";
    That actually creates a CONSTANT char *, which you can't delete, or...surprise, surprise...it crashes your program.

    You ONLY need to delete pointers which were allocated via the new() function call, either by you or some other library function.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by rags_to_riches
    That actually creates a CONSTANT char *, which you can't delete, or...surprise, surprise...it crashes your program.
    To clarify: it is not the fact that what the pointer points to is constant that is the problem. The point is that new should be matched with delete, but here the problem is that you use delete where new was not used.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of strings and memory allocation
    By maluyk in forum C Programming
    Replies: 7
    Last Post: 01-26-2009, 11:52 AM
  2. Static Memory allocation
    By p3rry in forum C Programming
    Replies: 25
    Last Post: 12-23-2008, 08:30 AM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Memory allocation at runtime?
    By electrolove in forum C Programming
    Replies: 6
    Last Post: 02-05-2003, 11:39 AM
  5. Windows Memory Allocation :: C++
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 11-03-2002, 12:13 PM