problems with dynamic allocation in an ellipses function

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

    problems with dynamic allocation in an ellipses function

    Hello all

    I'm pretty new to C, so please accept my apologies in advance :-)

    I'm trying to allocate space for an array of pointers to strings
    (which are accepted as ellipses) inside a while loop, and after the
    allocation, when i "assert" the allocation, the assertion fails!!!

    void printStrings(s1 , ...){ //ellipses function
    ....
    ....
    void *myList;
    int count=1;
    char *pArr=s1;
    va_start(myList , s1);
    while( myList ){
    pArr = (char *) realloc(pArr, ++count * sizeoff(char*)) ;
    assert(pArr);
    pArr[count-1] = va_arg(myList, char*);
    }

    However, if i take the 2 lines of the allocation and assertion out of
    he while, it works!!!
    i.e. - the following is OK :

    void *myList;
    int count=1;
    char *pArr=s1;
    va_start(myList , s1);

    pArr = (char *) realloc(pArr, ++count * sizeoff(char*)) ;
    assert(pArr); //THIS IS OK

    even if i put these two lines in a for loop that run just once, it
    fails!!!

    int count=1;
    char *pArr=s1;
    va_start(myList , s1);
    for(i =0; i <1; i++){ // LOOP RUNS JUST ONCE
    pArr = (char *) realloc(pArr, ++count * sizeoff(char*)) ;
    assert(pArr); //ASSERTION FAILS HERE!!!
    }

    what Am i doing wrong and why is the allocation failing inside the
    while/for loops

    Thanks a lot
    Shiron

  • Richard Heathfield

    #2
    Re: problems with dynamic allocation in an ellipses function

    hyperboogie said:
    Hello all
    >
    I'm pretty new to C, so please accept my apologies in advance :-)
    >
    I'm trying to allocate space for an array of pointers to strings
    (which are accepted as ellipses) inside a while loop, and after the
    allocation, when i "assert" the allocation, the assertion fails!!!
    >
    void printStrings(s1 , ...){ //ellipses function
    ...
    ...
    void *myList;
    int count=1;
    char *pArr=s1;
    va_start(myList , s1);
    while( myList ){
    pArr = (char *) realloc(pArr, ++count * sizeoff(char*)) ;
    Make sure you have #include <stdlib.hnear the top of your source file,
    lose the cast, use a temp, and get sizeof right:

    char *tmp = realloc(pArr, ++count * sizeof *pArr);
    if(tmp != NULL)
    {
    pArr = tmp;
    assert(pArr);
    In C90, assert requires an integer expression:

    assert(pArr != NULL);

    but this is not a good way to check whether the allocation succeeded, as
    it will abort the application on failure if NDEBUG is not defined, and
    will ignore the failure otherwise!


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at the above domain, - www.

    Comment

    • hyperboogie

      #3
      Re: problems with dynamic allocation in an ellipses function

      On Mar 27, 8:54 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
      hyperboogie said:
      >
      >
      >
      Hello all
      >
      I'm pretty new to C, so please accept my apologies in advance :-)
      >
      I'm trying to allocate space for an array of pointers to strings
      (which are accepted as ellipses) inside a while loop, and after the
      allocation, when i "assert" the allocation, the assertion fails!!!
      >
      void printStrings(s1 , ...){ //ellipses function
      ...
      ...
      void *myList;
      int count=1;
      char *pArr=s1;
      va_start(myList , s1);
      while( myList ){
      pArr = (char *) realloc(pArr, ++count * sizeoff(char*)) ;
      >
      Make sure you have #include <stdlib.hnear the top of your source file,
      lose the cast, use a temp, and get sizeof right:
      >
      char *tmp = realloc(pArr, ++count * sizeof *pArr);
      if(tmp != NULL)
      {
      pArr = tmp;
      >
      assert(pArr);
      >
      In C90, assert requires an integer expression:
      >
      assert(pArr != NULL);
      >
      but this is not a good way to check whether the allocation succeeded, as
      it will abort the application on failure if NDEBUG is not defined, and
      will ignore the failure otherwise!
      >
      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
      email: rjh at the above domain, - www.
      Sorry - it doesn't work ....
      in any case .... why should I lose the cast? realloc returns void
      * and pArr is char * , so a cast is required ... or am I wrong?

      Comment

      • Doug

        #4
        Re: problems with dynamic allocation in an ellipses function

        On 27 Mar, 19:38, "hyperboogi e" <hyperboo...@gm ail.comwrote:
        Hello all
        >
        I'm pretty new to C, so please accept my apologies in advance :-)
        >
        I'm trying to allocate space for an array of pointers to strings
        (which are accepted as ellipses) inside a while loop, and after the
        allocation, when i "assert" the allocation, the assertion fails!!!
        >
        void printStrings(s1 , ...){ //ellipses function
        ...
        ...
        void *myList;
        int count=1;
        char *pArr=s1;
        va_start(myList , s1);
        while( myList ){
        pArr = (char *) realloc(pArr, ++count * sizeoff(char*)) ;

        My guess:

        You are attempting to realloc the memory pointed to by pArr. The
        first time through the loop, pArr == s1.

        If you are calling with something like:

        printStrings("T his is the string which will be pointed to by s1",
        etc., etc., ...

        ....then I think there's a good chance that the string "This is the
        string which will be pointed to by s1" will have static storage/be
        read-only due to string pooling/etc.* So I don't think that
        realloc'ing this memory is valid.

        * I've no doubt got the terminology wrong, but give it ten minutes and
        you'll soon see a hundred people correct me!

        Hope that helps,
        Doug

        Comment

        • Doug

          #5
          Re: problems with dynamic allocation in an ellipses function

          On 27 Mar, 20:05, "hyperboogi e" <hyperboo...@gm ail.comwrote:
          in any case .... why should I lose the cast? realloc returns void
          * and pArr is char * , so a cast is required ... or am I wrong?- Hide quoted text -
          I think that if you include <stdlib.hthen you don't need the cast -
          you are permitted to assign void * to any pointer type.

          With that in place, the cast then just hides potential problems.

          Doug

          Comment

          • Ian Collins

            #6
            Re: problems with dynamic allocation in an ellipses function

            hyperboogie wrote:
            Hello all
            >
            I'm pretty new to C, so please accept my apologies in advance :-)
            >
            I'm trying to allocate space for an array of pointers to strings
            (which are accepted as ellipses) inside a while loop, and after the
            allocation, when i "assert" the allocation, the assertion fails!!!
            >
            void printStrings(s1 , ...){ //ellipses function
            ....
            ....
            void *myList;
            int count=1;
            char *pArr=s1;
            What is s1? If it isn't a char* allocated by malloc, all bets are off.

            --
            Ian Collins.

            Comment

            • hyperboogie

              #7
              Re: problems with dynamic allocation in an ellipses function

              On Mar 27, 9:37 pm, Ian Collins <ian-n...@hotmail.co mwrote:
              hyperboogie wrote:
              Hello all
              >
              I'm pretty new to C, so please accept my apologies in advance :-)
              >
              I'm trying to allocate space for an array of pointers to strings
              (which are accepted as ellipses) inside a while loop, and after the
              allocation, when i "assert" the allocation, the assertion fails!!!
              >
              void printStrings(s1 , ...){ //ellipses function
              ....
              ....
              void *myList;
              int count=1;
              char *pArr=s1;
              >
              What is s1? If it isn't a char* allocated by malloc, all bets are off.
              >
              --
              Ian Collins.
              s1 is just a string. the function was called from main like so:

              printStrings("f ather", "mother", "brother", "sister", NULL);

              one correction to what I wrote earlier:
              The function is printStrings(ch ar* s1, ...){...}

              Comment

              • hyperboogie

                #8
                Re: problems with dynamic allocation in an ellipses function

                On Mar 27, 9:36 pm, "Doug" <DougTheS...@go oglemail.comwro te:
                On 27 Mar, 20:05, "hyperboogi e" <hyperboo...@gm ail.comwrote:
                >
                in any case .... why should I lose the cast? realloc returns void
                * and pArr is char * , so a cast is required ... or am I wrong?- Hide quoted text -
                >
                I think that if you include <stdlib.hthen you don't need the cast -
                you are permitted to assign void * to any pointer type.
                >
                With that in place, the cast then just hides potential problems.
                >
                Doug
                I have added <stdlib.hand tried it with and without the cast - no
                difference.

                Comment

                • Doug

                  #9
                  Re: problems with dynamic allocation in an ellipses function

                  On 27 Mar, 20:49, "hyperboogi e" <hyperboo...@gm ail.comwrote:
                  s1 is just a string. the function was called from main like so:
                  >
                  printStrings("f ather", "mother", "brother", "sister", NULL);
                  >
                  one correction to what I wrote earlier:
                  The function is printStrings(ch ar* s1, ...){...}- Hide quoted text -
                  >
                  Well, this is your problem. In your call to printStrings(), "father"
                  is not a dynamically allocated piece of memory. Thus, you cannot
                  realloc it.

                  Doug

                  Comment

                  • Doug

                    #10
                    Re: problems with dynamic allocation in an ellipses function

                    On 27 Mar, 20:51, "hyperboogi e" <hyperboo...@gm ail.comwrote:
                    On Mar 27, 9:36 pm, "Doug" <DougTheS...@go oglemail.comwro te:
                    >
                    On 27 Mar, 20:05, "hyperboogi e" <hyperboo...@gm ail.comwrote:
                    >
                    in any case .... why should I lose the cast? realloc returns void
                    * and pArr is char * , so a cast is required ... or am I wrong?- Hide quoted text -
                    >
                    I think that if you include <stdlib.hthen you don't need the cast -
                    you are permitted to assign void * to any pointer type.
                    >
                    With that in place, the cast then just hides potential problems.
                    >
                    Doug
                    >
                    I have added <stdlib.hand tried it with and without the cast - no
                    difference.
                    The cast here is not your main problem (and it won't actually affect
                    this exact code). But it's not good style.

                    Doug

                    Comment

                    • Default User

                      #11
                      Re: problems with dynamic allocation in an ellipses function

                      hyperboogie wrote:

                      s1 is just a string. the function was called from main like so:
                      >
                      printStrings("f ather", "mother", "brother", "sister", NULL);
                      >
                      one correction to what I wrote earlier:
                      The function is printStrings(ch ar* s1, ...){...}
                      For the future, post a complete, minimal program that demonstrates the
                      problem.

                      That statement contains three conditions:

                      1. Complete - A program that can be cut and pasted into our compilers
                      if we choose, but at the very least we know is not missing anything.

                      2. Minimal - The program is reduced down as much as possible. That
                      means removing parts where you can to simplify it, but where it still
                      produces the failing behavior. Often the process will reveal the
                      section with the bug, but at least you will make the review process
                      easier.

                      3. Demonstrates the problem - The code you give us should be able to
                      produce the exact condition you are experiencing. In addition, you need
                      to tell us what the program was supposed to do, and what it did
                      instead. If it is failing to compile, transcribe exactly or
                      (preferably) cut and paste the compiler messages.



                      Brian

                      Comment

                      • hyperboogie

                        #12
                        Re: problems with dynamic allocation in an ellipses function

                        On Mar 27, 9:55 pm, "Doug" <DougTheS...@go oglemail.comwro te:
                        On 27 Mar, 20:49, "hyperboogi e" <hyperboo...@gm ail.comwrote:
                        >
                        s1 is just a string. the function was called from main like so:
                        >
                        printStrings("f ather", "mother", "brother", "sister", NULL);
                        >
                        one correction to what I wrote earlier:
                        The function is printStrings(ch ar* s1, ...){...}- Hide quoted text -
                        >
                        Well, this is your problem. In your call to printStrings(), "father"
                        is not a dynamically allocated piece of memory. Thus, you cannot
                        realloc it.
                        >
                        Doug
                        Thanks a lot Doug
                        you were right - that was the problem. I also should have declared
                        pArr as char ** (as apposed to just char *).
                        everything works fine now :-)
                        in any case, i still don't understand why I should not use a cast.
                        After all, realloc returns a:
                        void *

                        Comment

                        • Ian Collins

                          #13
                          Re: problems with dynamic allocation in an ellipses function

                          hyperboogie wrote:
                          On Mar 27, 9:37 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                          >
                          >>hyperboogie wrote:
                          >>
                          >>>Hello all
                          >>
                          >>>I'm pretty new to C, so please accept my apologies in advance :-)
                          >>
                          >>>I'm trying to allocate space for an array of pointers to strings
                          >>>(which are accepted as ellipses) inside a while loop, and after the
                          >>>allocation , when i "assert" the allocation, the assertion fails!!!
                          >>
                          >>>void printStrings(s1 , ...){ //ellipses function
                          >>>....
                          >>>....
                          >>>void *myList;
                          >>>int count=1;
                          >>>char *pArr=s1;
                          >>
                          >>What is s1? If it isn't a char* allocated by malloc, all bets are off.
                          >>
                          *Please* don't quote signatures!
                          >
                          s1 is just a string. the function was called from main like so:
                          >
                          printStrings("f ather", "mother", "brother", "sister", NULL);
                          >
                          So there's your problem.
                          one correction to what I wrote earlier:
                          The function is printStrings(ch ar* s1, ...){...}
                          >
                          That should realy be:

                          printStrings( const char* s1, ...);

                          if s1 is a string literal format string.

                          --
                          Ian Collins.

                          Comment

                          • Ian Collins

                            #14
                            Re: problems with dynamic allocation in an ellipses function

                            hyperboogie wrote:
                            in any case, i still don't understand why I should not use a cast.
                            After all, realloc returns a:
                            void *
                            >
                            You don't need a cast to convert between void* to another pointer type,
                            that's the way the language is defined.

                            --
                            Ian Collins.

                            Comment

                            • CBFalconer

                              #15
                              Re: problems with dynamic allocation in an ellipses function

                              hyperboogie wrote:
                              >
                              .... snip ...
                              >
                              Sorry - it doesn't work ....
                              in any case .... why should I lose the cast? realloc returns
                              void * and pArr is char * , so a cast is required ... or am I wrong?
                              Yes, you are wrong. See the C-faq.

                              --
                              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

                              Working...