Help! Using dynamic allocation and malloc

This is a discussion on Help! Using dynamic allocation and malloc within the C Programming forums, part of the General Programming Boards category; I have to use dynamic memory allocation to return a newly-created array from a function. The return value cannot be ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    13

    Help! Using dynamic allocation and malloc

    I have to use dynamic memory allocation to return a newly-created array from a
    function. The return value cannot be a “dynamic array” (created by an ordinary
    array declaration), because such an array cannot safely be accessed from outside the function
    where it was declared. Memory space obtained from the malloc() function, however, can be
    passed around from one function to another. The cost of this freedom is that you need to be
    more careful with this explicitly allocated space; for example, you have to call free() when
    you’re finished with it.


    REQUIREMENTS
    • You must call malloc() to create the space for the new array returned by your function.
    • You must call free() in your main() function to release the space when you have
    finished using it.


    PROBLEM
    Write a function with this prototype:
    double *rowmaxes(int N, double matrix[N][N]);
    Rowmaxes() must return a new array containing in element i the largest element in row i of the
    N×N matrix called matrix.
    In the same file as your function, write a program (a main() function) that asks the user for
    whatever input is needed to try out the function. Print the results. Don’t forget to free the
    memory allocated in your function.

    Help pleaseeee!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,443
    Read our forum guidelines and homework policy.

    Give it a shot, post your code in code tags, and explain where you're stuck. Then you'll get some help.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    What have you done so far?
    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

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    An aside for the seasoned C programmers: Is it even possible to define a prototype with unknown dimensions of the array? Especially if the N parameter is determined during run-time, and the width of the matrix is dynamic on that basis. The compiler would not know how to calculate row offset.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,443
    C99 allows arrays of variable length. I believe the only requirement for parameters in this regard is that the parameter giving the length (N in this case) come before the array in the parameter list.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Awesome! I'll have to play with that. That means the compiler is generating code which calculates offsets on-the-fly rather than hard-coding them. Could come in handy if it's faster executing than linearizing arrays and doing the index calculations manually especially for multi dimensional arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Memory Allocation (malloc vs calloc)
    By lostandconfused in forum C Programming
    Replies: 10
    Last Post: 09-01-2010, 01:56 PM
  2. Problem with custom dynamic memory allocation routines
    By BLauritson in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2010, 06:26 AM
  3. Dynamic array Allocation
    By deepak_j in forum C Programming
    Replies: 3
    Last Post: 08-17-2009, 07:18 AM
  4. POSIX Threads and dynamic memory allocation
    By PING in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2009, 10:28 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 08:08 AM

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