vc.user_comments[i] = string; /*crashing*/

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tatu Portin

    #1

    vc.user_comments[i] = string; /*crashing*/

    Have this kind of struct:

    typedef struct {
    char **user_comments ;
    /* ... */
    } vorbis_comment;

    /* prototype */
    char * read_vorbis_str ing
    ( FILE *sc);

    /* problem */
    vc.user_comment s[i] = read_vorbis_str ing (sc);

    'read_vorbis_st ring' returns a pointer to a calloced string. This function
    works for sure.
    But the problem is, that 'vc.user_commen ts[i] = string;' is not the right way
    to assing the address of the 'string' to the pointer array 'vc.user_commen ts'.
    What is?
  • karl malbrain

    #2
    Re: vc.user_comment s[i] = string; /*crashing*/

    "Tatu Portin" <axel86@mbnet.f i> wrote in message
    news:xV2td.331$ Eb.226@read3.in et.fi...[color=blue]
    > Have this kind of struct:
    >
    > typedef struct {
    > char **user_comments ;
    > /* ... */
    > } vorbis_comment;
    >
    > /* prototype */
    > char * read_vorbis_str ing
    > ( FILE *sc);
    >
    > /* problem */
    > vc.user_comment s[i] = read_vorbis_str ing (sc);
    >
    > 'read_vorbis_st ring' returns a pointer to a calloced string. This function
    > works for sure.
    > But the problem is, that 'vc.user_commen ts[i] = string;' is not the right[/color]
    way[color=blue]
    > to assing the address of the 'string' to the pointer array[/color]
    'vc.user_commen ts'.[color=blue]
    > What is?[/color]

    You've left out the INSTANTIATION of vorbis_comment. Please post this
    section, where you declare and initialize vc.

    karl m


    Comment

    • Tatu Portin

      #3
      Re: vc.user_comment s[i] = string; /*crashing*/

      karl malbrain wrote:[color=blue]
      > "Tatu Portin" <axel86@mbnet.f i> wrote in message
      > news:xV2td.331$ Eb.226@read3.in et.fi...
      >[color=green]
      >>Have this kind of struct:
      >>
      >>typedef struct {
      >>char **user_comments ;
      >>/* ... */
      >>} vorbis_comment;
      >>
      >>/* prototype */
      >>char * read_vorbis_str ing
      >>( FILE *sc);
      >>
      >>/* problem */
      >>vc.user_comme nts[i] = read_vorbis_str ing (sc);
      >>
      >>'read_vorbis_ string' returns a pointer to a calloced string. This function
      >>works for sure.
      >>But the problem is, that 'vc.user_commen ts[i] = string;' is not the right[/color]
      >
      > way
      >[color=green]
      >>to assing the address of the 'string' to the pointer array[/color]
      >
      > 'vc.user_commen ts'.
      >[color=green]
      >>What is?[/color]
      >
      >
      > You've left out the INSTANTIATION of vorbis_comment. Please post this
      > section, where you declare and initialize vc.
      >
      > karl m
      >
      >[/color]

      Here is your "INSTANTIATION" :

      vorbis_comment read_comments
      ( const char *scname)
      {
      /* After second 'vorbis' string comes the width (int) of VENDOR string.
      * Then comes the VENDOR string itself.
      * After VENDOR string comes the number (int) of comments.
      * After the number of comments comes the width (int) of first user comment.
      * After the first user comment comes the width (int) of the second user comment.
      */
      register int i;

      vorbis_comment vc;
      FILE *sc;
      char *ident = "vorbis";

      sc = fopen (scname, "rb");
      assert (sc != NULL);

      assert (!wind_till (ident, sc));
      assert (!wind_till (ident, sc));

      vc.vendor = read_vorbis_str ing (sc);
      assert (vc.vendor != NULL);

      assert (fread (&vc.comments , sizeof (int), 1, sc) == 1);

      printf ("%d\n", vc.comments);

      vc.comment_wds = (int *) calloc (vc.comments + 1, sizeof (int));
      assert (vc.comment_wds != NULL);

      for (i = 0 ; i < vc.comments - 1 ; i++) {
      vc.user_comment s[i] = read_vorbis_str ing (sc);

      assert (vc.user_commen ts[i] != NULL);
      printf ("__%s__", vc.user_comment s[i]);

      vc.comment_wds[i] = strlen (vc.user_commen ts[i]);
      }

      vc.comment_wds[vc.comments + 1] = 0;


      return vc;
      }
      /* End Of File */

      Comment

      • Tatu Portin

        #4
        Re: vc.user_comment s[i] = string; /*crashing*/

        Problem solved.

        I hadn't calloced memory for the array of pointers. I was only callocing memory
        for the strings which the array of pointers should be pointing to.

        /* ... */

        assert (fread (&vc.comments , sizeof (int), 1, sc) == 1);

        printf ("%d\n", vc.comments);

        /* ADDED */
        vc.user_comment s = (char *) calloc (vc.comments + 1, sizeof (char *));
        assert (vc.user_commen ts != NULL);
        /* End of ADDED */

        vc.comment_wds = (int *) calloc (vc.comments + 1, sizeof (int));
        assert (vc.comment_wds != NULL);
        /* ... */

        Comment

        • CBFalconer

          #5
          Re: vc.user_comment s[i] = string; /*crashing*/

          Tatu Portin wrote:[color=blue]
          >
          > Have this kind of struct:
          >
          > typedef struct {
          > char **user_comments ;
          > /* ... */
          > } vorbis_comment;
          >
          > /* prototype */
          > char * read_vorbis_str ing
          > ( FILE *sc);
          >
          > /* problem */
          > vc.user_comment s[i] = read_vorbis_str ing (sc);
          >
          > 'read_vorbis_st ring' returns a pointer to a calloced string. This
          > function works for sure.
          >
          > But the problem is, that 'vc.user_commen ts[i] = string;' is not
          > the right way to assing the address of the 'string' to the pointer
          > array 'vc.user_commen ts'. What is?[/color]

          You haven't shown a complete compilable program, which forces
          anyone replying to make assumptions (or to ignore you). There is
          no declaration of vc. Assuming one exists in scope, of the form:

          vorbis_comment vc;

          vc.user_comment s is of type char **, i.e a pointer to a pointer to
          a char. It is uninitialized, and any use of it will cause
          undefined behaviour. Don't ask me what you should initialize it
          to, since you didn't bother making your program and specifications
          complete. I suspect you want a linked list rather than an array.

          --
          Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
          Available for consulting/temporary embedded and systems.
          <http://cbfalconer.home .att.net> USE worldnet address!

          Comment

          • Tatu Portin

            #6
            Re: vc.user_comment s[i] = string; /*crashing*/

            CBFalconer wrote:[color=blue]
            > Tatu Portin wrote:
            >[color=green]
            >>Have this kind of struct:
            >>
            >>typedef struct {
            >> char **user_comments ;
            >> /* ... */
            >>} vorbis_comment;
            >>
            >>/* prototype */
            >>char * read_vorbis_str ing
            >> ( FILE *sc);
            >>
            >>/* problem */
            >>vc.user_comme nts[i] = read_vorbis_str ing (sc);
            >>
            >>'read_vorbis_ string' returns a pointer to a calloced string. This
            >>function works for sure.
            >>
            >>But the problem is, that 'vc.user_commen ts[i] = string;' is not
            >>the right way to assing the address of the 'string' to the pointer
            >>array 'vc.user_commen ts'. What is?[/color]
            >
            >
            > You haven't shown a complete compilable program, which forces
            > anyone replying to make assumptions (or to ignore you). There is
            > no declaration of vc. Assuming one exists in scope, of the form:
            >
            > vorbis_comment vc;
            >
            > vc.user_comment s is of type char **, i.e a pointer to a pointer to
            > a char. It is uninitialized, and any use of it will cause
            > undefined behaviour. Don't ask me what you should initialize it
            > to, since you didn't bother making your program and specifications
            > complete. I suspect you want a linked list rather than an array.
            >[/color]

            Here is the complete working program. (compiled -Wall -pedantic)

            /* Start Of File */

            /* Public Domain */
            #include <stdio.h>
            #include <stdlib.h>
            #include <assert.h>
            #include <string.h>

            /* CDmaker
            * Purpose is to correctly rip tags from Ogg Vorbis files for
            * reproduction in printable form for CD distribution.
            */

            typedef struct {
            char **user_comments ;
            int *comment_wds;
            int comments;
            char *vendor;
            } vorbis_comment;

            /* 'read_comments' is a hack.
            * It does not work by the specification.*/
            char * read_vorbis_str ing
            ( FILE *sc);

            int wind_till
            ( const char *ident
            , FILE *sc);

            vorbis_comment read_comments
            ( const char *scname);

            int print_comments
            ( const vorbis_comment vc);

            int main
            ( int argc
            , char *argv[])
            {
            vorbis_comment vc;

            vc = read_comments (argv[1]);

            print_comments (vc);

            return 0;
            }

            char * read_vorbis_str ing
            ( FILE *sc)
            {
            register int i;
            int wd;
            char *str;
            char ch;

            if (fread (&wd, sizeof (int), 1, sc) != 1)
            return NULL;

            str = (char *) calloc (wd + 1, sizeof (int));
            if (str == NULL)
            return NULL;

            for (i = 0 ; i < wd ; i++) {
            ch = fgetc (sc);
            if (ch == EOF)
            break;
            str[i] = ch;
            }
            str[wd + 1] = '\0';

            return str;
            }


            int wind_till
            ( const char *ident
            , FILE *sc)
            {
            register int i;
            int ch = '\0';

            if (ident[0] == '\0')
            return 0;

            while (1) {
            for (i = 0 ; ident[i] == ch ; i++) {
            if (ident[i + 1] == '\0')
            return 0;

            ch = fgetc (sc);
            if (ch == EOF)
            return 2;

            }

            ch = fgetc (sc);
            if (ch == EOF)
            return 1;
            }

            return 3;
            }

            vorbis_comment read_comments
            ( const char *scname)
            {
            /* After second 'vorbis' string comes the width (int) of VENDOR string.
            * Then comes the VENDOR string itself.
            * After VENDOR string comes the number (int) of comments.
            * After the number of comments comes the width (int) of first user comment.
            * After the first user comment comes the width (int) of the second user comment.
            */
            register int i;

            vorbis_comment vc;
            FILE *sc;
            char *ident = "vorbis";

            sc = fopen (scname, "rb");
            assert (sc != NULL);

            assert (!wind_till (ident, sc));
            assert (!wind_till (ident, sc));

            vc.vendor = read_vorbis_str ing (sc);
            assert (vc.vendor != NULL);

            assert (fread (&vc.comments , sizeof (int), 1, sc) == 1);

            /* Allocate memory for pointers */
            vc.user_comment s = (char **) calloc (vc.comments + 1, sizeof (char *));
            assert (vc.user_commen ts != NULL);

            /* Allocate memory for comment lenghts */
            vc.comment_wds = (int *) calloc (vc.comments + 1, sizeof (int));
            assert (vc.comment_wds != NULL);

            for (i = 0 ; i < vc.comments ; i++) {
            vc.user_comment s[i] = read_vorbis_str ing (sc);
            assert (vc.user_commen ts[i] != NULL);

            vc.comment_wds[i] = strlen (vc.user_commen ts[i]);
            }

            vc.comment_wds[vc.comments + 1] = 0;


            return vc;
            }

            int print_comments
            ( vorbis_comment vc)
            {
            register int i;

            printf ("%s, %d comments\n", vc.vendor, vc.comments);

            for (i = 0 ; i < vc.comments ; i++) {
            printf ("%d: \t%s\n"
            , vc.comment_wds[i]
            , vc.user_comment s[i]
            );
            }

            return i;
            }
            /* End Of File */

            Comment

            • honeygrl33

              #7
              Re: vc.user_comment s[i] = string; /*crashing*/

              We have to use arrays, plus we haven't learned pointer yet.. but thanx!

              Comment

              • honeygrl33

                #8
                Re: vc.user_comment s[i] = string; /*crashing*/

                Wow guys...... this is CMSC 100... so lost.... :-\ no pointers just
                arrays and i'm only 19 i dont get all that!!

                Comment

                • honeygrl33

                  #9
                  Re: vc.user_comment s[i] = string; /*crashing*/

                  what's vorbis? Here's my entire program:
                  /*
                  */

                  #include <stdio.h>
                  #include <stdlib.h>

                  #define SIZE 12

                  void printResults (char months[][15], double sales[]);
                  void storeSales (char months[][15], double sales[]);

                  int main ( void )
                  {
                  system ("clear");
                  int i;
                  double sales[ SIZE ];
                  char months [SIZE][15] = {"January", "February", "March", "April",
                  "May",
                  "June", "July", "August", "September" ,
                  "October",
                  "November", "December"} ;

                  for (i = 0; i < SIZE; i++)
                  {
                  printf("%-9s: %9.2f\n", months[ i ], sales[ i ]);
                  }
                  storeSales (months, sales);
                  printResults (months, sales);
                  return 0;

                  }
                  void storeSales ( char months[][15], double sales[] )
                  {
                  int i = 0;
                  for ( i = 0; i < SIZE; i++ )
                  {

                  do
                  {
                  printf("Enter the store's sales for the month of %s:",
                  months[ i ]);
                  scanf( "9.2%lf", &sales [ i ]);
                  printf("\n");
                  }
                  while ((sales[ i ] < 0.00 ) || ( sales[ i ] > 100000.00));
                  }
                  }


                  void printResults ( char months[][15], double sales[] )
                  {
                  int i;
                  double lowest = 0.0, highest = 0.0, average = 0.0;

                  for (i = 0 ; i < SIZE ; i++)
                  average = average + (sales[i] / SIZE);


                  printf ("Average of the store's sales: %lf\n", average);


                  lowest = sales[0];
                  for (i = 0 ; i < SIZE ; i++) {
                  if (lowest > sales[i]) {
                  lowest = sales[i];
                  }
                  }


                  printf ("Lowest sales month: %lf\n", lowest);


                  highest = sales[0];
                  for (i = 0 ; i < SIZE ; i++) {
                  if (highest < sales[i]) {
                  highest = sales[i];
                  }
                  }


                  printf ("Highest sales month: %lf\n", highest);
                  return;
                  }

                  Comment

                  • honeygrl33

                    #10
                    Re: vc.user_comment s[i] = string; /*crashing*/

                    /*
                    */

                    #include <stdio.h>
                    #include <stdlib.h>

                    #define SIZE 12

                    void printResults (char months[][15], double sales[]);
                    void storeSales (char months[][15], double sales[]);

                    int main ( void )
                    {
                    system ("clear");
                    double sales[ SIZE ];
                    char months [SIZE][15] = {"January", "February", "March", "April",
                    "May",
                    "June", "July", "August", "September" , "October",
                    "November", "December"} ;


                    storeSales (months, sales);
                    printResults (months, sales);
                    return 0;

                    }
                    void storeSales ( char months[][15], double sales[] )
                    {
                    int i = 0;
                    for ( i = 0; i < SIZE; i++ )
                    {

                    do
                    {
                    printf("Enter the store's sales for the month of %s:", months[ i ]);
                    scanf( "9.2%lf", &sales [ i ]);
                    printf("\n");
                    }
                    while ((sales[ i ] < 0.00 ) || ( sales[ i ] > 100000.00));
                    }
                    }


                    void printResults ( char months[][15], double sales[] )
                    {
                    int i;
                    double lowest = 0.0, highest = 0.0, average = 0.0;

                    for (i = 0; i < SIZE; i++)
                    {
                    printf("%-9s: %9.2f\n", months[ i ], sales[ i ]);
                    }

                    for (i = 0 ; i < SIZE ; i++)
                    average = average + (sales[i] / SIZE);


                    printf ("Average of the store's sales: %lf\n", average);


                    lowest = sales[0];
                    for (i = 0 ; i < SIZE ; i++) {
                    if (lowest > sales[i]) {
                    lowest = sales[i];
                    }
                    }


                    printf ("Lowest sales month: %lf\n", lowest);


                    highest = sales[0];
                    for (i = 0 ; i < SIZE ; i++) {
                    if (highest < sales[i]) {
                    highest = sales[i];
                    }
                    }


                    printf ("Highest sales month: %lf\n", highest);
                    return;
                    }

                    I see what you mean, I did it.. but after compilation and entering
                    sales for January it's stuck in the loop??? it says "Enter sales for
                    January" until I press ctrl C!

                    Comment

                    • Mark McIntyre

                      #11
                      Re: vc.user_comment s[i] = string; /*crashing*/

                      On 6 Dec 2004 14:57:21 -0800, in comp.lang.c , "honeygrl33 "
                      <girliegirl4eve r@aol.com> wrote:
                      [color=blue]
                      >/*
                      >*/[/color]

                      <snip code>

                      When posting code, you need to post some sort of question too. Why did you
                      post this? Whats your problem? What do you want people to help with?

                      Do NOT rely on people having access to posts you made several days ago.
                      --
                      Mark McIntyre
                      CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                      CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                      Comment

                      • CBFalconer

                        #12
                        Re: vc.user_comment s[i] = string; /*crashing*/

                        Mark McIntyre wrote:[color=blue]
                        > "honeygrl33 " <girliegirl4eve r@aol.com> wrote:
                        >[color=green]
                        >> /*
                        >> */[/color]
                        >
                        > <snip code>
                        >
                        > When posting code, you need to post some sort of question too. Why
                        > did you post this? Whats your problem? What do you want people to
                        > help with?
                        >
                        > Do NOT rely on people having access to posts you made several days
                        > ago.[/color]

                        In fact don't rely on access to ANY other posts. Many get lost, or
                        delayed for days and weeks. Usenet is a 'no guarantees'
                        mechanism. Therefore snip quotations down to the portion germane
                        to your answer, and answer after the quoted portion.

                        --
                        Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                        Available for consulting/temporary embedded and systems.
                        <http://cbfalconer.home .att.net> USE worldnet address!


                        Comment

                        Working...