allocating mem in a function and assigning a ptr to the first byte of that mem array...

Discussion in 'C Programming' started by mast2as@yahoo.com, Feb 28, 2007.

  1. Guest

    I almost apologize to ask this question but I have been starting at
    this code for a bit of time and don't understand what's wrong with it.
    I am not arguing about the fact it's good or not coding, it's actually
    a shorter version of something more complex that i am doing.

    Anway the idea is that there's a func Test which takes a pointer to
    void argument, allocate some memory, set this memory with some cotent,
    assign the ptr to a void to the first byte of this new alloc memory
    and returuns. Normally because our argument points to the first byte
    of that memory array * AND THAT THE MEM HASN'T BEEN FREED *, i should
    still be able to access the content of that mem array.

    But i don't it crashes.

    Please help,

    cheers -mark

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

    //#include <map>
    #include <string>

    void Test( void *add, char st[2] )
    {
    char *tt = new char[3];
    strcpy( tt, st );
    add = (void*)tt;
    printf( ">> %s\n", (char*)add );
    //free( tt );
    }

    int main( int argc, char **argv )
    {
    void *a;
    Test( a, "ab" );
    // crashes here ?
    printf( "<< %c\n", *((char*)a + 1 ) );

    char *ttt = new char[3];
    strcpy( ttt, "ab" );
    a = (void*)ttt;
    printf( "<< %c\n", *((char*)a + 1 ) );

    return 0;
    }
     
    , Feb 28, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    Re: allocating mem in a function and assigning a ptr to the firstbyte of that mem array...

    wrote:
    > I almost apologize to ask this question but I have been starting at
    > this code for a bit of time and don't understand what's wrong with it.
    > I am not arguing about the fact it's good or not coding, it's actually
    > a shorter version of something more complex that i am doing.
    >
    > Anway the idea is that there's a func Test which takes a pointer to
    > void argument, allocate some memory, set this memory with some cotent,
    > assign the ptr to a void to the first byte of this new alloc memory
    > and returuns. Normally because our argument points to the first byte
    > of that memory array * AND THAT THE MEM HASN'T BEEN FREED *, i should
    > still be able to access the content of that mem array.
    >
    > But i don't it crashes.
    >
    > Please help,
    >
    > cheers -mark
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    > #include <cmath>
    >
    > //#include <map>
    > #include <string>
    >
    > void Test( void *add, char st[2] )
    > {
    > char *tt = new char[3];


    You are posting C++ to comp.lang.c. Don't do that!

    If we pretend this is C, your problem is the perennial bug of assigning
    a value to a local variable (add) and attempting to access it outside
    the function.

    If you want to allocate memory in a function, pass a pointer to the
    pointer you want to use

    void test( void **add char st[2] )
    {
    *add = malloc(3);

    --
    Ian Collins.
     
    Ian Collins, Feb 28, 2007
    #2
    1. Advertising

  3. Guest


    > You are posting C++ to comp.lang.c. Don't do that!
    >
    > If we pretend this is C, your problem is the perennial bug of assigning
    > a value to a local variable (add) and attempting to access it outside
    > the function.
    >
    > If you want to allocate memory in a function, pass a pointer to the
    > pointer you want to use
    >
    > void test( void **add char st[2] )
    > {
    > *add = malloc(3);
    >
    > --
    > Ian Collins.


    Thanks Ian. Yes I mixed C++ and C and was aware it was a perennial
    bug, the thing is that i need to go back to some old C code and i am
    just not used to it anymore... really sorry, thanks again for putting
    me back on the right track - i just started at some code today for so
    long, i got lost...
     
    , Feb 28, 2007
    #3
  4. Nelu Guest

    Re: allocating mem in a function and assigning a ptr to the firstbyte of that mem array...

    wrote:
    > I almost apologize to ask this question but I have been starting at
    > this code for a bit of time and don't understand what's wrong with it.
    > I am not arguing about the fact it's good or not coding, it's actually
    > a shorter version of something more complex that i am doing.
    >
    > Anway the idea is that there's a func Test which takes a pointer to
    > void argument, allocate some memory, set this memory with some cotent,
    > assign the ptr to a void to the first byte of this new alloc memory
    > and returuns. Normally because our argument points to the first byte
    > of that memory array * AND THAT THE MEM HASN'T BEEN FREED *, i should
    > still be able to access the content of that mem array.
    >
    > But i don't it crashes.
    >
    > Please help,
    >
    > cheers -mark
    >
    > #include <stdlib.h>
    > #include <stdio.h>


    > #include <cmath>
    > //#include <map>
    > #include <string>


    Not standard C headers. Probably C++.

    >
    > void Test( void *add, char st[2] )
    > {
    > char *tt = new char[3];


    new operator does not exist in C.
    You need to check the value of tt to make sure it's not NULL.
    (In your case you should probably catch the exception which is OT
    here).

    > strcpy( tt, st );
    > add = (void*)tt;


    add has the value passed to the function. Here you change the
    value that add holds but not the value that was passed to the
    function.

    > printf( ">> %s\n", (char*)add );
    > //free( tt );


    If you'll free tt here, add will point to memory that doesn't
    belong to it. Also, be consistent, use new with delete and malloc
    with free. Given the fact that this is c.l.c you can only use
    malloc with free.

    > }
    >
    > int main( int argc, char **argv )
    > {
    > void *a;
    > Test( a, "ab" );


    a is an uninitialized pointer. You should not use it like that
    because it's value is passed to Test but you are not allowed to
    use its value. Remember the observations for add from above? That
    function didn't change the value it received because it can't, so
    add has the same value as before passing it to Test.

    > // crashes here ?


    Probably. You should pass a pointer to your pointer and change
    that value:
    void Test( void **add, char st[2] )
    and use *a=tt;
    Pass *a as Test(&a,"ab") and you should set a to NULL before
    calling Test.

    > printf( "<< %c\n", *((char*)a + 1 ) );
    >
    > char *ttt = new char[3];
    > strcpy( ttt, "ab" );
    > a = (void*)ttt;
    > printf( "<< %c\n", *((char*)a + 1 ) );
    >
    > return 0;
    > }
    >


    You'll have to decide what language you want to use. You mix C
    and C++ too much. Although the C functions exist in C++ you
    should probably use what C++ provides if you want to stay with
    C++ (and start posting to the appropriate group, probably
    c.l.c++), or use only C code and get help here.

    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)
     
    Nelu, Feb 28, 2007
    #4
  5. Nelu Guest

    Re: allocating mem in a function and assigning a ptr to the firstbyte of that mem array...

    Nelu wrote:
    > wrote:

    <snip>
    >> void *a;
    >> Test( a, "ab" );

    >
    > a is an uninitialized pointer. You should not use it like that
    > because it's value is passed to Test but you are not allowed to
    > use its value. Remember the observations for add from above? That
    > function didn't change the value it received because it can't, so
    > add has the same value as before passing it to Test.
    >
    >> // crashes here ?

    >
    > Probably. You should pass a pointer to your pointer and change
    > that value:
    > void Test( void **add, char st[2] )
    > and use *a=tt;
    > Pass *a as Test(&a,"ab")


    > and you should set a to NULL before calling Test.


    Sorry, I forgot to mention: it's good to initialize pointers to
    NULL just in case, but this initialization is not required in
    this case (as opposed to the original one) because you are
    passing &a to Test as opposed to a, and &a is valid as the
    address of a.

    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)
     
    Nelu, Feb 28, 2007
    #5
  6. CBFalconer Guest

    Re: allocating mem in a function and assigning a ptr to the first byteof that mem array...

    wrote:
    >
    > I almost apologize to ask this question but I have been starting at
    > this code for a bit of time and don't understand what's wrong with it.
    > I am not arguing about the fact it's good or not coding, it's actually
    > a shorter version of something more complex that i am doing.
    >
    > Anway the idea is that there's a func Test which takes a pointer to
    > void argument, allocate some memory, set this memory with some cotent,
    > assign the ptr to a void to the first byte of this new alloc memory
    > and returuns. Normally because our argument points to the first byte
    > of that memory array * AND THAT THE MEM HASN'T BEEN FREED *, i should
    > still be able to access the content of that mem array.
    >
    > But i don't it crashes.
    >
    > Please help,
    >
    > cheers -mark
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    > #include <cmath>


    no such header file.

    >
    > //#include <map>
    > #include <string>


    no such header file.

    If your compiler didn't object to the above, you are using a C++
    compiler, which is not a good thing to do to C code.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
     
    CBFalconer, Feb 28, 2007
    #6
  7. pete Guest

    wrote:
    >
    > I almost apologize to ask this question but I have been starting at
    > this code for a bit of time and don't understand what's wrong with it.
    > I am not arguing about the fact it's good or not coding, it's actually
    > a shorter version of something more complex that i am doing.
    >
    > Anway the idea is that there's a func Test which takes a pointer to
    > void argument, allocate some memory, set this memory with some cotent,
    > assign the ptr to a void to the first byte of this new alloc memory
    > and returuns. Normally because our argument points to the first byte
    > of that memory array * AND THAT THE MEM HASN'T BEEN FREED *, i should
    > still be able to access the content of that mem array.
    >
    > But i don't it crashes.
    >
    > Please help,
    >
    > cheers -mark
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    > #include <cmath>
    >
    > //#include <map>
    > #include <string>
    >
    > void Test( void *add, char st[2] )
    > {
    > char *tt = new char[3];
    > strcpy( tt, st );
    > add = (void*)tt;
    > printf( ">> %s\n", (char*)add );
    > //free( tt );
    > }
    >
    > int main( int argc, char **argv )
    > {
    > void *a;
    > Test( a, "ab" );
    > // crashes here ?
    > printf( "<< %c\n", *((char*)a + 1 ) );
    >
    > char *ttt = new char[3];
    > strcpy( ttt, "ab" );
    > a = (void*)ttt;
    > printf( "<< %c\n", *((char*)a + 1 ) );
    >
    > return 0;
    > }


    /* BEGIN new.c */

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

    void *Test(char *st)
    {
    char *tt = malloc(strlen(st) + 1);

    if (tt != NULL) {
    strcpy(tt, st );
    printf( ">> %s\n", tt);
    }
    return tt;
    }

    int main(void)
    {
    void *a;
    char *ttt;

    a = Test("ab");
    if (a != NULL) {
    printf( "<< %c\n", *((char*)a + 1 ) );
    }
    ttt = malloc(sizeof "ab");
    if (ttt != NULL) {
    strcpy(ttt, "ab");
    a = ttt;
    printf( "<< %c\n", *((char*)a + 1 ) );
    }
    return 0;
    }

    /* END new.c */

    --
    pete
     
    pete, Feb 28, 2007
    #7
  8. Re: allocating mem in a function and assigning a ptr to the firstbyte of that mem array...

    wrote:
    > I almost apologize to ask this question but I have been starting at
    > this code for a bit of time and don't understand what's wrong with it.
    > I am not arguing about the fact it's good or not coding, it's actually
    > a shorter version of something more complex that i am doing.
    >
    > Anway the idea is that there's a func Test which takes a pointer to
    > void argument, allocate some memory, set this memory with some cotent,
    > assign the ptr to a void to the first byte of this new alloc memory
    > and returuns. Normally because our argument points to the first byte
    > of that memory array * AND THAT THE MEM HASN'T BEEN FREED *, i should
    > still be able to access the content of that mem array.


    After cleaning up your C++-isms, you will still have a logical error in
    the way you call your Test() function. Please examine the following
    carefully, comparing it to your original.


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

    #if 0
    /* mha: apart from the problems of using '//' style comments in usenet
    postings, no matter what the language, and with using them with C89
    or C90 compilers, where they are errors, commenting out code is best
    done with the #if / #endif form exemplified here. */
    #include <map>
    #endif

    #if 0
    /* mha: there are no headers named <cmath> or <string> in C. */
    #include <cmath>
    #include <string>
    #endif
    #include <string.h> /* mha */

    void Test(void **add /* mha: NB */ , char st[2])
    {
    #if 0
    /* mha: the following is a syntax error in C. A replacement follows.
    It appears that your function provides no way to report failures. */
    char *tt = new char[3];
    #endif
    char *tt;
    if (!(tt = malloc(3))) {
    fprintf(stderr, "%s",
    "malloc failed, and 'Test()' provides no way to "
    "handle that.\nI'm giving up.\n");
    exit(EXIT_FAILURE);
    }
    strcpy(tt, st);
    *add = tt; /* mha: NB */
    printf(">> %s\n", (char *) *add);

    /* mha: it is a good thing you don't free tt; In your off-topic
    C++-ism you allocate it with new. Such things are freed with
    delete or delete[], not with free(). */
    // free( tt );

    }

    int main(void)
    {
    void *a;
    Test(&a, "ab"); /* mha: NB */
    printf("<< %c\n", *((char *) a + 1));
    {
    #if 0
    /* mha: declaration following executable statements in the block
    is an error in C89 or C90. I have added a block. */
    /* mha: the following is a syntax error in C. A replacement
    follows. */
    char *ttt = new char[3];
    #endif
    char *ttt;
    if (!(ttt = malloc(3))) {
    fprintf(stderr, "%s",
    "malloc failed in main(),\nI'm giving up.\n");
    free(a);
    exit(EXIT_FAILURE);
    }
    strcpy(ttt, "ab");
    a = (void *) ttt;
    printf("<< %c\n", *((char *) a + 1));
    /* mha: Please clean up after yourself. */
    free(ttt); /* mha */
    }
    /* mha: Please clean up after yourself. */
    free(a); /* mha */
    return 0;
    }
     
    Martin Ambuhl, Feb 28, 2007
    #8
  9. jaysome Guest

    On Wed, 28 Feb 2007 15:47:04 -0500, Martin Ambuhl
    <> wrote:

    > wrote:
    >> I almost apologize to ask this question but I have been starting at
    >> this code for a bit of time and don't understand what's wrong with it.
    >> I am not arguing about the fact it's good or not coding, it's actually
    >> a shorter version of something more complex that i am doing.
    >>
    >> Anway the idea is that there's a func Test which takes a pointer to
    >> void argument, allocate some memory, set this memory with some cotent,
    >> assign the ptr to a void to the first byte of this new alloc memory
    >> and returuns. Normally because our argument points to the first byte
    >> of that memory array * AND THAT THE MEM HASN'T BEEN FREED *, i should
    >> still be able to access the content of that mem array.

    >
    >After cleaning up your C++-isms, you will still have a logical error in
    >the way you call your Test() function. Please examine the following
    >carefully, comparing it to your original.
    >
    >
    >#include <stdlib.h>
    >#include <stdio.h>
    >
    >#if 0
    > /* mha: apart from the problems of using '//' style comments in usenet
    > postings, no matter what the language, and with using them with C89
    > or C90 compilers, where they are errors, commenting out code is best
    > done with the #if / #endif form exemplified here. */
    >#include <map>
    >#endif
    >
    >#if 0
    > /* mha: there are no headers named <cmath> or <string> in C. */
    >#include <cmath>
    >#include <string>
    >#endif
    >#include <string.h> /* mha */
    >
    >void Test(void **add /* mha: NB */ , char st[2])
    >{
    >#if 0
    > /* mha: the following is a syntax error in C. A replacement follows.
    > It appears that your function provides no way to report failures. */
    > char *tt = new char[3];
    >#endif
    > char *tt;
    > if (!(tt = malloc(3))) {
    > fprintf(stderr, "%s",
    > "malloc failed, and 'Test()' provides no way to "
    > "handle that.\nI'm giving up.\n");
    > exit(EXIT_FAILURE);
    > }
    > strcpy(tt, st);
    > *add = tt; /* mha: NB */
    > printf(">> %s\n", (char *) *add);
    >
    > /* mha: it is a good thing you don't free tt; In your off-topic
    > C++-ism you allocate it with new. Such things are freed with
    > delete or delete[], not with free(). */
    > // free( tt );
    >
    >}
    >
    >int main(void)
    >{
    > void *a;
    > Test(&a, "ab"); /* mha: NB */
    > printf("<< %c\n", *((char *) a + 1));
    > {
    >#if 0
    > /* mha: declaration following executable statements in the block
    > is an error in C89 or C90. I have added a block. */
    > /* mha: the following is a syntax error in C. A replacement
    > follows. */
    > char *ttt = new char[3];
    >#endif
    > char *ttt;
    > if (!(ttt = malloc(3))) {
    > fprintf(stderr, "%s",
    > "malloc failed in main(),\nI'm giving up.\n");
    > free(a);
    > exit(EXIT_FAILURE);
    > }
    > strcpy(ttt, "ab");
    > a = (void *) ttt;
    > printf("<< %c\n", *((char *) a + 1));
    > /* mha: Please clean up after yourself. */
    > free(ttt); /* mha */


    Same effect as:

    free(a);

    > }
    > /* mha: Please clean up after yourself. */
    > free(a); /* mha */


    Ouch. This free()'s a pointer that has already been free()'d. Not sure
    if this is what you intended.

    --
    jay
     
    jaysome, Mar 1, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sid
    Replies:
    5
    Views:
    1,080
  2. Jason

    difference between *ptr++ and ++*ptr ?

    Jason, May 15, 2005, in forum: C Programming
    Replies:
    19
    Views:
    6,551
    Chris Torek
    May 19, 2005
  3. hans christian

    newbie question: difference between (*ptr) and *ptr

    hans christian, Jun 11, 2006, in forum: C Programming
    Replies:
    5
    Views:
    736
    hans christian
    Jun 11, 2006
  4. Rakesh Kumar
    Replies:
    5
    Views:
    686
    James Kanze
    Dec 21, 2007
  5. DSF
    Replies:
    7
    Views:
    108
    James Kuyper
    Jun 12, 2014
Loading...

Share This Page