Memory Allocation using Pointers

This is a discussion on Memory Allocation using Pointers within the C++ Programming forums, part of the General Programming Boards category; Hi all, I want to create a pointer to a C-String like so: Code: char *pCString = "This is a ...

  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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    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
    20,968
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 10:52 AM
  2. Static Memory allocation
    By p3rry in forum C Programming
    Replies: 25
    Last Post: 12-23-2008, 07:30 AM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  4. Memory allocation at runtime?
    By electrolove in forum C Programming
    Replies: 6
    Last Post: 02-05-2003, 10:39 AM
  5. Windows Memory Allocation :: C++
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 11-03-2002, 11:13 AM

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