Thread: dynamic memory

  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
    28,413
    Moved to C programming forum.

    How does the code not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    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
    486
    Why shouldn't malloc be cast to the appropriate type?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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*.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 02: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, 10:52 AM