Huh ... C behavior

Discussion in 'C Programming' started by sanjaymeher@gmail.com, Dec 29, 2005.

  1. Guest

    Copy paste and run the example now remove the comment /*test2();*/
    in main function ... Its not working .. Whyyyy ?? Behavior is really
    queer !!!! I know the method

    "int addDynamicMemory1(char *ptr, int size)" is not the right context
    of use here But I want to get the solution to this particular problem



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

    void test1();
    void test2();
    int addDynamicMemory1(char *ptr, int size);
    int addDynamicMemory(char **ptr, int size);

    int addDynamicMemory1(char *ptr, int size)
    {
    /* See and chek whether size memory is available or not */
    int currSize;
    if(ptr == NULL)
    {
    ptr = (char*) malloc(size * sizeof(char));
    if(ptr != NULL)
    {
    printf("Initialized memory as null \n");
    return -1;
    }
    else
    {
    printf("Can not Initialized memory as null \n");
    return -1;
    }
    }

    currSize = strlen(ptr);
    size = currSize + size;
    ptr = (char*) realloc(ptr, size*sizeof(char));

    if(ptr != NULL)
    {
    printf(" re Allocation size is %d\n",size);
    return 0;
    }

    printf(" re Allocation failed \n");
    return -1;
    }

    int addDynamicMemory(char **ptr, int size)
    {
    /* See and chek whether size memory is available or not */
    int currSize;
    if(*ptr == NULL)
    {
    *ptr = (char*) malloc(size * sizeof(char));
    if(*ptr != NULL)
    {
    printf("Initialized memory as null \n");
    return -1;
    }
    else
    {
    printf("Can not Initialized memory as null \n");
    return -1;
    }
    }

    currSize = strlen(*ptr);
    size = currSize + size;
    *ptr = (char*) realloc(*ptr, size*sizeof(char));

    if(ptr != NULL)
    {
    printf(" re Allocation size is %d\n",size);
    return 0;
    }

    printf(" re Allocation failed \n");
    return -1;
    }

    int main(void)
    {
    test1();
    /*test2();*/
    }

    void test2()
    {
    char *test = NULL;

    addDynamicMemory(&test, 40);
    printf("At first test value is %s\n",test);
    strcpy(test,"444444444");
    printf("After allocation val is %s\n", test);

    addDynamicMemory1(test, 60);
    strcat(test,"666666666666");
    printf("After allocation val is %s\n", test);

    addDynamicMemory1(test, 60);
    strcat(test,"888888888888888");
    printf("After allocation val is %s\n", test);
    }

    void test1()
    {
    char *test = NULL;

    addDynamicMemory(&test, 40);
    printf("At first test value is %s\n",test);
    strcpy(test,"444444444");
    printf("After allocation val is %s\n", test);

    addDynamicMemory(&test, 50);
    strcat(test,"5555555555");
    printf("After allocation val is %s\n", test);

    addDynamicMemory1(test, 60);
    strcat(test,"666666666666");
    printf("After allocation val is %s\n", test);

    addDynamicMemory1(test, 60);
    strcat(test,"888888888888888");
    printf("After allocation val is %s\n", test);
    }
    , Dec 29, 2005
    #1
    1. Advertising

  2. Chuck F. Guest

    wrote:
    >
    > Copy paste and run the example now remove the comment
    > /*test2();*/ in main function ... Its not working .. Whyyyy ??
    > Behavior is really queer !!!! I know the method

    .... snip lots ...

    It is working correctly according to the C standard. Undefined
    behaviour allows anything whatsoever to happen, including "working"
    (whatever that may be).

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Chuck F., Dec 29, 2005
    #2
    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. Jim Owen

    Output parameters undefined? Huh?

    Jim Owen, Oct 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    411
    Rajesh.V
    Oct 22, 2003
  2. VB Programmer
    Replies:
    1
    Views:
    350
    Chris Moore
    Jun 12, 2004
  3. some dude somewhere

    doc types! huh?

    some dude somewhere, Jan 3, 2004, in forum: HTML
    Replies:
    6
    Views:
    421
    Sam Hughes
    Jan 3, 2004
  4. William Trenker

    Huh?!?!? What is it that I'm seeing here?

    William Trenker, Sep 14, 2003, in forum: Python
    Replies:
    6
    Views:
    288
    Gary Feldman
    Sep 15, 2003
  5. Huh ... C behavior

    , Dec 29, 2005, in forum: C Programming
    Replies:
    3
    Views:
    360
    Richard Heathfield
    Dec 29, 2005
Loading...

Share This Page