Saturday, May 8, 2010

Deleting cstrings

While working on a URL resolver project, I came across a curious aspect of the nature of cstrings. To start out, I was creating a URL object with a string constant as an input. Example: URL * url = new URL(“http://www.google.com/“); In my URL object, I was simply pointing to whatever string was passed in. I should have known better than to do that. Java has made some bad habits in me. Aside from that error, I noticed that I would always get a segfault when trying to delete the cstring the URL was stored in. Later I found that this error was eliminated by copying the string input rather than simply pointing to it. After looking through some memory dumps I have concluded that the reason for the segfault was due to attempting to delete memory that I didn’t have permission to access. In this case the string constant I was passing in was part of my program. I could really make a mess of things if I had been permitted to overwrite that memory.

EDIT:

Since writing this post, I have learned that string constants are stored in the static data area of the memory model. This is separate from where the code is stored in the memory model.