dynamic memory

This is a discussion on dynamic memory within the C Programming forums, part of the General Programming Boards category; I need help for this code, i tried many times but i couldn't get it working. Write C program fragnments ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    8

    dynamic memory

    I need help for this code, i tried many times but i couldn't get it working.

    Write C program fragnments to allocate dynamic memory for an integer array with 100 elements and then initialise each of the array elements to -1.

    This is what i wrote

    int*array = (int*)malloc(sizeof(int)*100);
    for (int i = 0; i<100; i++
    *(array + i) = -1;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    Moved to C programming forum.

    How does the code not work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    8
    Hello Laserlight

    Thanks, is my answer look alright for that code?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    Close, but not quite. I would write:
    Code:
    int *array = malloc(sizeof(*array) * 100);
    int i;
    for (i = 0; i < 100; i++)
        array[i] = -1;
    I move the declaration of i to before the loop for maximum portability. The part that I fixed was the semi-colon that made the for loop have an empty body. My changes to the first line help in maintenance such that you can change the int to say, double, with less changes needed. Note that you should eventually free(array);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    437
    It's always a good idea to check that malloc actually gave you some memory to work with before you try assgining it values. Check that array is not NULL after the malloc call if you want to be safe.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Check malloc return value.
    Don't cast malloc return value.
    #include stdlib.h

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    437
    Why shouldn't malloc be cast to the appropriate type?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    Quote Originally Posted by KBriggs
    Why shouldn't malloc be cast to the appropriate type?
    It is not required (unless one wants the code to be compilable as C++), and it could hide a failure to #include <stdlib.h>, potentially resulting in incorrect code due to the compiler assuming that the return type of malloc is int instead of void*.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    437
    What could cause a failure to include a library? Other than actually forgetting to include it...

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    Quote Originally Posted by KBriggs
    What could cause a failure to include a library? Other than actually forgetting to include it...
    Nothing, but forgetfulness is bad enough.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [BEGINNER] dynamic memory dumb question
    By arthurhess in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2009, 01:53 PM
  2. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 09:57 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21