Menu

Why doesn't this work?

Pokey Oats
2009-01-21
2012-09-26
  • Pokey Oats

    Pokey Oats - 2009-01-21

    Could someone please be kind enough to explain to me why the result of the following code is what it is (seg fault on the second call of function test) in Dev-C++.

    I am not looking for code correction, because obviously the first test works.

    Am I incorrect in assuming that a straight string (i.e. something in "double quotes" not a char string[]) is terminated by a '\0'?

    Based on the result, I am starting to think that only char strings are terminated by a '\0' and a straight string is designated a start address and length?!

    Still, the crash seems quite consistent when trying to terminate a string that isn't contained in a variable.

    Example follows:

    include <string>

    include <iostream>

    using namespace std;

    void test(char *ptr);

    int main(int argc, char *argv[])
    {
    char string[]="T:\TEST1\TEST2\TEST3.tst";
    test(&string[0]);
    cout << endl << string << "\n_______\n";

    test("C:\TEST4\TEST5\TEST6.tst");
    }

    void test(char *ptr)
    {
    cout << endl << ptr << endl;

    for (int a=strlen(ptr); a>=0; a--)
    {
    cout << ptr[a];
    if (ptr[a]=='\') { ptr[a]='\0'; break; }
    }

    }

     
    • Pokey Oats

      Pokey Oats - 2009-01-22

      Ah ha!~!

      Of course, of course, of course, of course!!

      My apologies for posting what turned out to be such a rudimentary question. Did I mention there were some cobwebs that I'm dusting off?!?!? :)

      Thank you for the other advice and the links. Pascal strings do indeed seem to be a far more efficient means of string definition and I am assuming that given the example's methodology of using pointers in one big string that the previous 1 byte, 255 char limit is subsequently voided due to the minimum pointer size (these days) of 32-bit (4 bytes).

      I tried to use Visual Studio Express instead but am in the unpleasant situation of having to finish a large program for work to just "get the job done" for now and then clean/optimize later. Thus, VSE ended up being sightly detrimental as I'm quite accustomed to Dev-Cpp's interface et all now.

      In my spare time though, I am definitely going to give the VSE Game Developer Kit a bit of a try!!

      One of my life goals is to SCROLL something. I understand the simplicity of the theory. Create a huge 2D array, stick a huge bitmap in it and then render through the image to the dimensions of the screen.. I'm hoping the GDK might make the realization fo the theory much simpler then any previous efforts I've made.. :)

      Again, thank you very much for the answer.

       
    • cpns

      cpns - 2009-01-21

      > I am starting to think that only char strings are terminated by a '\0'
      > and a straight string is designated a start address and length?!

      Nope. A literal string constant is a null terminated char array like any other (only const). Therin lies the problem. In the second call you pass a literal string CONSTANT and then try to modify it!


      The following is unnecessarily convoluted:

      test(&string[0]);

      it is equivalent to just:

      test( string ) ;

      An arrays name is also its address.

      C style strings are inefficient for the reasons noted here: http://www.joelonsoftware.com/articles/fog0000000319.html . You are using C++ so you would do better to use std::string


      My final piece of advice, use a compiler with a decent debugger (Dev-C++'s is not a decent debugger). http://www.microsoft.com/express/vc/

      Clifford


       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.