Working in Unix but not in windows

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

  1. Guest

    This piece of code is not working in windows but working in Unix Not
    getting the reason .. help me out

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

    int addDynamicMemory(char **ptr, int size);

    int addDynamicMemory(char **ptr, int size)
    {
    int currSize;
    currSize = strlen(*ptr);
    size = currSize + size;
    printf("before re Allocation size is %d\n",size);
    *ptr = (char*) realloc(*ptr, size*sizeof(char));
    printf("memory allocated \n");
    return 0;
    }

    int main(int argc, char* argv[])
    {
    char **test;
    char *test1;
    ws1_c = "san jay";

    test = &test1;
    *test = (char*) malloc(1*sizeof(char)); // If U will make it *test =
    (char*) malloc(10*sizeof(char)) It will work .... :( amazing ..
    strcpy(*test,"TEST_11");
    printf("Curr val of one %s\n",*test);

    *test = (char*) realloc(*test, 20*sizeof(char));
    strcat(*test,"3333333333");
    printf("After allocation val is %s\n",*test);

    addDynamicMemory(test, 40) ;
    strcat(*test,"444444444");
    printf("After allocation val is %s\n",*test);

    addDynamicMemory(test, 20) ;
    strcat(*test,"7777777");
    printf("After allocation val is %s\n",*test);

    addDynamicMemory(test, 20) ;
    strcat(*test,"888888888888888");
    printf("After allocation val is %s\n",*test);

    }
    , Dec 28, 2005
    #1
    1. Advertising

  2. ashwin Guest

    Hi,
    Chk this code and let me know if this is what you want...otherwise I
    might have changed the wrong things. If that is what you wanted I can
    explain what is wrong with your code on windows

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

    int addDynamicMemory(char **ptr, int size);

    int addDynamicMemory(char **ptr, int size)
    {
    int currSize;
    currSize = strlen(*ptr);
    size = currSize + size;
    printf("before re Allocation size is %d\n",size);
    *ptr = (char*) realloc(*ptr, size*sizeof(char));
    printf("memory allocated \n");
    return 0;

    }

    //int main(int argc, char* argv[])
    void main()
    {
    char **test;
    char *test1;
    //ws1_c = "san jay";

    test = &test1;
    *test = (char*) malloc(1*sizeof(char)); // If U will make it *test =
    (char*) malloc(10*sizeof(char)) It will work .... :( amazing ..
    strcpy(*test,"TEST_11");
    printf("Curr val of one %s\n",*test);

    *test = (char*) realloc(*test, 20*sizeof(char));
    strcat(*test,"3333333333");
    printf("After allocation val is %s\n",*test);

    addDynamicMemory(test, 40) ;
    strcat(*test,"444444444");
    printf("After allocation val is %s\n",*test);

    addDynamicMemory(test, 20) ;
    strcat(*test,"7777777");
    printf("After allocation val is %s\n",*test);

    addDynamicMemory(test, 20) ;
    strcat(*test,"888888888888888");
    printf("After allocation val is %s\n",*test);

    }

    Ashwin
    ashwin, Dec 28, 2005
    #2
    1. Advertising

  3. Guest

    Hi Ashwin

    Still Facing the same problem after changing the main() signature also
    .... I am compiling this in MS VC++ and facing this problem..

    Reply me back

    Sanjay
    , Dec 28, 2005
    #3
  4. Artie Gold Guest

    wrote:
    > This piece of code is not working in windows but working in Unix Not
    > getting the reason .. help me out
    >
    > #include <iostream.h>

    This is not a standard C header (it's not a C++ header either, but I
    digress...). Fortunately (in this context) you don't use it.

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

    #include <stdlib.h> /* for malloc()/realloc() prototypes */
    >
    > int addDynamicMemory(char **ptr, int size);
    >
    > int addDynamicMemory(char **ptr, int size)

    The definition will serve quite well as a prototype, thank you.
    > {
    > int currSize;
    > currSize = strlen(*ptr);
    > size = currSize + size;
    > printf("before re Allocation size is %d\n",size);
    > *ptr = (char*) realloc(*ptr, size*sizeof(char));


    Do not cast the return value from *alloc functions. It is unnecessary
    and can mask errors (as it has here...see above).

    `sizeof(char) is 1 by definition.

    *Never* directly realloc() -- it can create a memory leak if it fails.
    (Always* realloc() to a temp.)

    > printf("memory allocated \n");
    > return 0;
    > }
    >
    > int main(int argc, char* argv[])
    > {
    > char **test;
    > char *test1;
    > ws1_c = "san jay";
    >
    > test = &test1;
    > *test = (char*) malloc(1*sizeof(char)); // If U will make it *test =
    > (char*) malloc(10*sizeof(char)) It will work .... :( amazing .


    Why not just use `test1'?
    [...as well as same comments about casting the return value and
    sizeof(char)]
    ..
    > strcpy(*test,"TEST_11");


    You've only allocated space for one `char'. What do you expect?

    > printf("Curr val of one %s\n",*test);
    >
    > *test = (char*) realloc(*test, 20*sizeof(char));
    > strcat(*test,"3333333333");
    > printf("After allocation val is %s\n",*test);
    >
    > addDynamicMemory(test, 40) ;
    > strcat(*test,"444444444");
    > printf("After allocation val is %s\n",*test);
    >
    > addDynamicMemory(test, 20) ;
    > strcat(*test,"7777777");
    > printf("After allocation val is %s\n",*test);
    >
    > addDynamicMemory(test, 20) ;
    > strcat(*test,"888888888888888");
    > printf("After allocation val is %s\n",*test);
    >
    > }
    >

    Obviously, you lack a clue.
    Fortunately, clues are easy to obtain. Read a good book on C
    (http://www/accu/org is a good source for suggestions; K&R II is hard to
    beat). Read the C-FAQ. Browse news:comp.lang.c.

    Once you've done all that, please post back if you run into problems.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
    Artie Gold, Dec 28, 2005
    #4
  5. Guest

    Ooops Allocated for only one char and assigning,
    strcpy(*test,"TEST_11"), it to seven chars is the main culprit
    , Dec 28, 2005
    #5
  6. ashwin Guest

    Can you atleast tell me the exact error messages as I can compile and
    run the code in borland

    Ashwin
    ashwin, Dec 28, 2005
    #6
  7. JimS Guest

    On 27 Dec 2005 21:26:50 -0800, ""
    <> wrote:

    >This piece of code is not working in windows but working in Unix Not
    >getting the reason .. help me out
    >
    >#include <iostream.h>


    I'll ignore the above line.

    >#include <stdio.h>
    >#include <string.h>
    >
    >int addDynamicMemory(char **ptr, int size);
    >
    >int addDynamicMemory(char **ptr, int size)
    >{
    > int currSize;
    > currSize = strlen(*ptr);
    > size = currSize + size;
    > printf("before re Allocation size is %d\n",size);
    > *ptr = (char*) realloc(*ptr, size*sizeof(char));
    > printf("memory allocated \n");
    > return 0;
    >}
    >
    >int main(int argc, char* argv[])
    >{
    > char **test;
    > char *test1;
    > ws1_c = "san jay";
    >
    > test = &test1;
    > *test = (char*) malloc(1*sizeof(char));


    Here you've allocated 1 char of space.

    /* If U will make it *test = (char*) malloc(10*sizeof(char)) It will
    work .... :( amazing .. */

    Not really...

    > strcpy(*test,"TEST_11");
    > printf("Curr val of one %s\n",*test);


    Here you copy 8 chars into your 1 char of space.

    Three more notes
    1) sizeof(char) by definition is always 1
    2) don't cast the results of malloc and realloc. It usully hides a
    missing #include <stdlib.h> (which is missing in your example).
    3) You should always check the return result of malloc and realloc for
    success.

    Jim
    JimS, Dec 28, 2005
    #7
  8. >This piece of code is not working in windows but working in Unix Not
    >getting the reason .. help me out
    >
    >#include <iostream.h>
    >#include <stdio.h>
    >#include <string.h>
    >
    >int addDynamicMemory(char **ptr, int size);
    >
    >int addDynamicMemory(char **ptr, int size)
    >{
    > int currSize;
    > currSize = strlen(*ptr);


    The memory needed for a string of strlen() N is N+1, *not* N (the
    '\0' terminator requires space, too). Passing strlen(something)
    rather than strlen(something)+1 to malloc() or realloc() is a red
    flag that you're going to write on more memory than you allocated.

    > size = currSize + size;
    > printf("before re Allocation size is %d\n",size);
    > *ptr = (char*) realloc(*ptr, size*sizeof(char));
    > printf("memory allocated \n");
    > return 0;
    >}

    Gordon L. Burditt
    Gordon Burditt, Dec 28, 2005
    #8
  9. JimS Guest

    On 27 Dec 2005 22:16:32 -0800, ""
    <> wrote:

    >Ooops Allocated for only one char and assigning,
    >strcpy(*test,"TEST_11"), it to seven chars is the main culprit


    eight.

    Jim
    JimS, Dec 28, 2005
    #9
  10. haroon Guest

    wrote:
    > Ooops Allocated for only one char and assigning,
    > strcpy(*test,"TEST_11"), it to seven chars is the main culprit


    But why did it work properly on Unix and not on Windows? Can any body
    explain this? I tried this code on linux and it works fine there as
    well (with space for 1 character allocated).
    haroon, Dec 28, 2005
    #10
  11. Richard Bos Guest

    "haroon" <> wrote:

    > wrote:
    > > Ooops Allocated for only one char and assigning,
    > > strcpy(*test,"TEST_11"), it to seven chars is the main culprit

    >
    > But why did it work properly on Unix and not on Windows?


    There is no particular "work properly" for code which exhibits undefined
    behaviour. Working as if there were memory available is proper. Crashing
    immediately is fine. Writing over other, possibly completely unrelated,
    objects, or even over return addresses or executable code, is legal.
    Appearing to work as the programmer naively expects it to on the
    development machine, but crashing and burning when demonstrated on a
    customer's differently kitted-out machine is also just dandy.

    Richard
    Richard Bos, Dec 28, 2005
    #11
  12. haroon said:

    >
    > wrote:
    >> Ooops Allocated for only one char and assigning,
    >> strcpy(*test,"TEST_11"), it to seven chars is the main culprit

    >
    > But why did it work properly on Unix and not on Windows?


    There isn't a "properly", when you step outside the rules of the language.
    If you start shooting at the other players, you shouldn't be surprised if
    they start behaving in a way you don't expect and can't predict. That
    behaviour may well differ from one platform to another.

    > Can any body explain this? I tried this code on linux and it works
    > fine there as well (with space for 1 character allocated).


    The code is broken. That Linux copes well with it is a tribute to Linux, but
    the correct approach is not to try to understand why it coped well (which
    will be pretty obvious to anyone who ever wrote an allocator), but to fix
    your code, which is a gerharsterly mess.

    Firstly, remove that silly iostream.h header, which isn't a C header and
    isn't even a C++ header.
    Secondly, remove every single cast.
    Thirdly, replace every single instance of sizeof(char) with 1, or remove it
    completely.
    Fourthly, make all necessary changes in your program to reflect the fact
    that strlen returns a size_t, not an int.
    Fifthly, change your program so that it detects any failure to meet an
    allocation request and takes appropriate remedial action.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Dec 28, 2005
    #12
  13. Richard Bos Guest

    Richard Heathfield <> wrote:

    > Firstly, remove that silly iostream.h header, which isn't a C header and
    > isn't even a C++ header.
    > Secondly, remove every single cast.
    > Thirdly, replace every single instance of sizeof(char) with 1, or remove it
    > completely.


    Or if you want to be ready for when you'll start using a wide character
    type, use *<the char pointer>; in this case, **ptr.

    Richard
    Richard Bos, Dec 28, 2005
    #13
    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. Martin Heuckeroth
    Replies:
    5
    Views:
    668
    JiangZemin
    Apr 1, 2005
  2. Alan Silver
    Replies:
    1
    Views:
    3,755
    Alan Silver
    Aug 2, 2005
  3. gaurav tyagi
    Replies:
    14
    Views:
    1,306
    gaurav tyagi
    Jan 20, 2006
  4. Replies:
    0
    Views:
    859
  5. James Edward Gray II

    Net::HTTP working on Windows, but not Unix

    James Edward Gray II, Mar 2, 2006, in forum: Ruby
    Replies:
    3
    Views:
    131
    Park Heesob
    Mar 3, 2006
Loading...

Share This Page