Is correct ?

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

    #1

    Is correct ?

    Hi all,
    i have a trouble understanding this:

    example:

    #include....

    char * testA(void);

    char * testB(void);

    char * testC(void);




    int main(void)
    {
    char * acMsg=NULL;
    ....

    strcpy(acMsg,te stA());

    strcpy(acMsg,te stB());

    strcpy(acMsg,te stC());

    return 0;
    }

    char * testA(void)
    {
    char * acMsg=NULL;

    acMsg=malloc((1 4+1)*sizeof(cha r));

    strncpy(acMsg," THIS IS A TEST,14);

    return acMsg;

    // free???
    }

    char * testB(void)
    {
    char acMsg[]="THIS IS A TEST";

    // this isn't correct (i hope)becaust i try to
    return a local address(after this call i
    lost the address of 'acMsg')

    return &acMsg[0];
    }

    char * testC(void)
    {
    char acMsg[]="THIS IS A TEST";
    char *acMsg_B=NULL;

    // this isn't correct becaust i try to
    return a local address(after this call i
    lose thi variable address)(??)

    acMsg_B=acMsg;

    return acMsg_B;

    // free??
    }


    Now...one of these examples are correct...becau se i feel confuse..

    Thanks all and have i nice day.

  • infobahn

    #2
    Re: Is correct ?

    lasek wrote:[color=blue]
    >
    > Hi all,
    > i have a trouble understanding this:
    >
    > example:
    >
    > #include....
    >
    > char * testA(void);
    >
    > char * testB(void);
    >
    > char * testC(void);
    >
    > int main(void)
    > {
    > char * acMsg=NULL;
    > ....
    >
    > strcpy(acMsg,te stA());[/color]

    Undefined behaviour. acMsg doesn't point to sufficient storage
    to store 15 bytes.
    [color=blue]
    >
    > strcpy(acMsg,te stB());[/color]

    Undefined behaviour. acMsg doesn't point to sufficient storage
    to store 15 bytes, and testB's return value is the address of
    a local object that no longer exists by this point in the code.
    [color=blue]
    >
    > strcpy(acMsg,te stC());[/color]

    Undefined behaviour. acMsg doesn't point to sufficient storage
    to store 15 bytes, and testC's return value is the address of
    a local object that no longer exists by this point in the code.

    [color=blue]
    >
    > return 0;
    > }
    >
    > char * testA(void)
    > {
    > char * acMsg=NULL;
    >
    > acMsg=malloc((1 4+1)*sizeof(cha r));
    >
    > strncpy(acMsg," THIS IS A TEST,14);[/color]

    You forgot the closing "
    [color=blue]
    >
    > return acMsg;
    >
    > // free???[/color]

    No, don't free the space until you've finished with it.

    Comment

    • lasek

      #3
      Re: Is correct ?

      ..Sorry i've forgot to allocate memory for
      'acMsg' and for missing '"' for strncpy(acMsg," THIS IS A TEST,14);
      but i want to know if the returned value is correct, and when i must call
      'free' if the pointer(testA) is allocated into a the same function.

      Comment

      • infobahn

        #4
        Re: Is correct ?

        lasek wrote:[color=blue]
        >
        > .Sorry i've forgot to allocate memory for
        > 'acMsg' and for missing '"' for strncpy(acMsg," THIS IS A TEST,14);
        > but i want to know if the returned value is correct, and when i must call
        > 'free' if the pointer(testA) is allocated into a the same function.[/color]

        Let's try to keep this as simple as possible - clc experts please
        note that I am trying to be helpful here, not necessarily "complete".

        lasek: I know you want to know about free(), but I think it would be
        a good idea to start at the beginning! :-) Your question does get
        answered in this article, I promise.

        When you want to allocate memory dynamically, you call malloc.
        You tell malloc how much memory you want, and it returns either
        NULL (which means "sorry, no can do") or a pointer to sufficient
        storage to meet your request.

        When using malloc, you must remember to:

        #include <stdlib.h>

        When (and only when) you have finished with the storage, you give
        the memory back by calling free(), passing the pointer value that
        you were originally given.

        The best way to call malloc is like this:

        p = malloc(n * sizeof *p);

        which, if it succeeds, allocates sufficient storage to store
        n objects of the appropriate size (i.e. the size of an object
        of the kind to which p points). The only time this doesn't work
        is when p is of type void *, in which case you need only do
        p = malloc(n) instead, to get n bytes of storage.

        When should you free this memory? ONLY after you have finished
        with it. So, in the case where you do something like:

        char *sduplicate(con st char *s)
        {
        char *p = malloc(strlen(s ) + 1) * sizeof *p);
        if(p != NULL)
        {
        strcpy(p, s);
        }
        return p;
        }

        you should NOT free(p) within this function; to do so would destroy
        the point of having the function in the first place! The code that
        calls this function takes (and might pass along to another function)
        the responsibility for freeing the space...

        char *s = sduplicate("Hel lo, world!");
        if(s != NULL)
        {
        reverse(s); /* do something to the copy */
        printf("%s\n", s); /* display */
        /* ... whatever ... */

        /* we have now finished with the storage, so we can call free() */
        free(s);
        }


        If you call malloc within a function and then decide that you no
        longer require the storage, you can call free within the same
        function. But if you do so, DON'T try to use that storage again.

        I hope that answers your question.

        Comment

        • Christopher Benson-Manica

          #5
          Re: Is correct ?

          lasek <claudio.rosset ti@acrm.it> spoke thus:
          [color=blue]
          > char * testA(void)
          > {
          > char * acMsg=NULL;
          > acMsg=malloc((1 4+1)*sizeof(cha r));
          > strncpy(acMsg," THIS IS A TEST,14);
          > return acMsg;
          > // free???[/color]

          No, but...
          [color=blue]
          > }[/color]
          [color=blue]
          > strcpy(acMsg,te stA());[/color]

          ....after strcpy() is called, you've lost the pointer that testA
          returned, meaning that you no longer have the ability to free that
          memory. You want something like

          char *tmp=testA();
          strcpy( acMsg, tmp ); /* assuming malloc() succeeded - you should check */
          free( tmp );

          Think about this in relation to infobahn's article :)

          --
          Christopher Benson-Manica | I *should* know what I'm talking about - if I
          ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

          Comment

          • infobahn

            #6
            Re: Is correct ?

            Christopher Benson-Manica wrote:[color=blue]
            >
            > lasek <claudio.rosset ti@acrm.it> spoke thus:
            >
            >[color=green]
            > > strcpy(acMsg,te stA());[/color]
            >
            > ...after strcpy() is called, you've lost the pointer that testA
            > returned, meaning that you no longer have the ability to free that
            > memory.[/color]

            Nice spot. There's always one more chocolate in the bottom of the box.

            Comment

            Working...