DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Please Help Dynamic Memory Allocation. (http://www.daniweb.com/forums/thread94302.html)

warpstar Oct 25th, 2007 5:13 pm
Please Help Dynamic Memory Allocation.
 
Each of the following program segments might
have syntax, logic or other kinds of errors. If there are errors,
correct, otherwise answer "no error". Assume all function headers
are correct.

Function copy_array receives an integer array a and
its size length, as parameters. It copies the array a into
another integer array b newly created, and returns the address
of the new array b.

int *copy_array ( int a[], int length )
{
int i;
int b[length];
for (i =0; i <= length, i++ )
b[i] = a[i];
return b[0];
}

CORRECT:

int *copy_array ( int a[], int length )
{
int i;
int *b = calloc( length, sizeof(int));  line 1
/* or int *b = malloc( length * sizeof(int)); */  line 2
for (i =0; i < length; i++ )
b[i] = a[i];
return b;
/* or return &b[0] */
}

My Question is that is dynamic memory allocation really necessaryÉ since you already know the size of the array which is being passed by the caller. Also can some someone explain each step of line 1 and 2. Thanks in advance.

Ptolemy Oct 25th, 2007 5:28 pm
Re: Please Help Dynamic Memory Allocation.
 
>is dynamic memory allocation really necessaryÉ since you already
>know the size of the array which is being passed by the caller
Really? How do you know? If you're talking about the length parameter, then yes, dynamic allocation really is necessary because array sizes must be compile-time constants. This isn't legal C++:
int *copy_array ( int a[], int length )
...
int b[length];
>Also can some someone explain each step of line 1 and 2.
Line 1 allocates length * sizeof(int) bytes and assigns it to b.
Line 2 allocates length * sizeof(int) bytes and assigns it to b.

Both lines are incorrect and won't compile as C++ because C++ doesn't support an implicit conversion from void*, and both calloc and malloc return void*.

warpstar Oct 25th, 2007 5:41 pm
Re: Please Help Dynamic Memory Allocation.
 
What about in C. É


Quote:

Originally Posted by Ptolemy (Post 457440)
>is dynamic memory allocation really necessaryÉ since you already
>know the size of the array which is being passed by the caller
Really? How do you know? If you're talking about the length parameter, then yes, dynamic allocation really is necessary because array sizes must be compile-time constants. This isn't legal C++:
int *copy_array ( int a[], int length )
...
int b[length];
>Also can some someone explain each step of line 1 and 2.
Line 1 allocates length * sizeof(int) bytes and assigns it to b.
Line 2 allocates length * sizeof(int) bytes and assigns it to b.

Both lines are incorrect and won't compile as C++ because C++ doesn't support an implicit conversion from void*, and both calloc and malloc return void*.


Ancient Dragon Oct 25th, 2007 6:44 pm
Re: Please Help Dynamic Memory Allocation.
 
Quote:

Originally Posted by warpstar (Post 457447)
What about in C. É

what is that language? Do you mean C# (maybe your keyboard doesn't have a '#' symbol?)

>> because array sizes must be compile-time constants
Not any more -- C99 has changed that for C language, not sure if it will also apply to C++ or not.

Ptolemy Oct 25th, 2007 8:55 pm
Re: Please Help Dynamic Memory Allocation.
 
>Not any more -- C99 has changed that for C language
While C++ has a certain measure of compatibility with C, it's with C89, not C99. In C++, array sizes must be compile-time constants.


All times are GMT -4. The time now is 6:52 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC