susbtring function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • HSeganfredo@gmail.com

    #1

    susbtring function

    Hello again,

    I´ve made a small substring() function that shoudl give back the a
    susbtring from a string, given its start and end positions.

    Is there any chance of the code below give an output containing data
    from another memory area that is completely unrelated to the input
    string? This is happening and the start/end parameters are acceptable
    (within bounds) for the input string...any idea?


    char *substring(char *string, int start, int end){
    printf("Input %s:", string);

    char *result = (char *)malloc((end - start + 1)*sizeof(char) );
    if(result == NULL){
    return(NULL);
    }

    string = string + start;
    while(start < end){
    *result = *string;
    result++;
    string++;
    start++;
    }
    printf("Output %s:", result);
    return(result);
    }

  • Keith Thompson

    #2
    Re: susbtring function

    Ian Collins <ian-news@hotmail.co mwrites:[...]
    > printf("Input %s:", string);
    >>
    >
    You forgot the '\n':
    >
    printf("Input %s:", string);
    So did you. Just as a matter of esthetics, putting the ':' at the end
    seems odd. I'd write this as:

    printf("Input: \"%s\"\n", string);

    [...]
    > return(NULL);
    >
    return isn't a function
    >
    return NULL;
    To expand on that a bit: the syntax of a return statement is
    return ;
    or
    return <expression;

    The parentheses are not necessary, but they are permitted. If you use

    return(NULL);

    then the parentheses are part of the expression, not part of the
    syntax of the return statement itself.

    I prefer *not* to use extraneous parentheses; a return statement isn't
    a function call, so it shouldn't look like one.

    But using parentheses isn't wrong. Even the examples in K&R1 use
    parentheses on return statements (though K&R2 changed this).

    [...]

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

    Comment

    • CBFalconer

      #3
      Re: susbtring function

      HSeganfredo@gma il.com wrote:
      >
      I´ve made a small substring() function that shoudl give back the a
      susbtring from a string, given its start and end positions.
      >
      Is there any chance of the code below give an output containing data
      from another memory area that is completely unrelated to the input
      string? This is happening and the start/end parameters are acceptable
      (within bounds) for the input string...any idea?
      >
      char *substring(char *string, int start, int end){
      start and end are indices into *string, so should be type size_t
      printf("Input %s:", string);
      >
      char *result = (char *)malloc((end - start + 1)*sizeof(char) );
      You can't put this after code. Declare result before the printf
      statement. Delete the cast. Eliminate "*sizeof(char)" , because
      that is 1 by definition.
      if(result == NULL){
      return(NULL);
      }
      >
      string = string + start;
      Where have you checked that start is a valid index for this
      string? Maybe "if (start < strlen(string)) ... " would do.
      while(start < end){
      Where have you checked that end is a valid index for this string?
      Where have you checked that end is larger than start? See above.
      *result = *string;
      Maybe this expression should include start?
      result++;
      Maybe this action should disappear?
      string++;
      start++;
      Indenting code controlled by a conditional is considered cool.
      }
      printf("Output %s:", result);
      return(result);
      This isn't the value malloc returned. You have a problem when you
      free, or you have a memory leak.
      }
      Enjoy.

      --
      Chuck F (cbfalconer at maineline dot net)
      Available for consulting/temporary embedded and systems.
      <http://cbfalconer.home .att.net>


      --
      Posted via a free Usenet account from http://www.teranews.com

      Comment

      • Ian Collins

        #4
        Re: susbtring function

        Charlie Gordon wrote:
        >
        All these corrections are fine and dandy...
        I hope the were more than that.
        to complete the answer for the
        OP, there are 2 reasons you can get data in the output unrelated to in
        input:
        - you forgot the final '\0' after the copy loop. Insert *result = '\0';
        after the }.
        - you don't check that start and end fall within the boundaries of the
        string. If not, you are attempting to read from beyond the end of the
        string or before its beginning or indeed from no particular location... This
        invokes undefined behaviour and does not necessarily cause your program to
        crash.
        >
        There were at least three, as I pointed out, he was returning one past
        the end of the result.

        --
        Ian Collins.

        Comment

        • Michal Nazarewicz

          #5
          Re: susbtring function

          HSeganfredo@gma il.com writes:
          I´ve made a small substring() function that shoudl give back the a
          susbtring from a string, given its start and end positions.
          >
          Is there any chance of the code below give an output containing data
          from another memory area that is completely unrelated to the input
          string?
          As others described it is. :)
          This is happening and the start/end parameters are acceptable
          (within bounds) for the input string...any idea?
          >
          char *substring(char *string, int start, int end){
          printf("Input %s:", string);
          >
          char *result = (char *)malloc((end - start + 1)*sizeof(char) );
          if(result == NULL){
          return(NULL);
          }
          >
          string = string + start;
          while(start < end){
          *result = *string;
          result++;
          string++;
          start++;
          }
          printf("Output %s:", result);
          return(result);
          }
          You may use strncpy() but remember to put 0 byte at the end of the
          string:

          #v+
          char *substring(cons t char *string, size_t start, size_t end) {
          return strcnpy(calloc( end-start+1), string+start, end-start);
          }
          #v-

          Exercise: make the code readable, check if memory allocation succeed,
          check if start is valid string's index and [start, end] is
          non-empty set.

          --
          Best regards, _ _
          .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
          ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
          ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--

          Comment

          • santosh

            #6
            Re: susbtring function

            Charlie Gordon wrote:
            Michal Nazarewicz wrote:
            <snip>
            >#v+
            >char *substring(cons t char *string, size_t start, size_t end) {
            > return strcnpy(calloc( end-start+1), string+start, end-start);
            >}
            >#v-
            >
            Assuming strcncpy was a typo for the infamous strncpy,
            There is a typo in the correction to the previous typo :)

            <snip>

            Comment

            Working...