| | |
Please Help Dynamic Memory Allocation.
![]() |
•
•
Join Date: Oct 2007
Posts: 15
Reputation:
Solved Threads: 0
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.
CORRECT:
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.
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.
C++ Syntax (Toggle Plain Text)
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.
>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++:
>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*.
>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++:
C++ Syntax (Toggle Plain Text)
int *copy_array ( int a[], int length ) ... int b[length];
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*.
•
•
Join Date: Oct 2007
Posts: 15
Reputation:
Solved Threads: 0
What about in C. É
•
•
•
•
>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++:
>Also can some someone explain each step of line 1 and 2.C++ Syntax (Toggle Plain Text)
int *copy_array ( int a[], int length ) ... int b[length];
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*.
Last edited by Ancient Dragon; Oct 25th, 2007 at 6:51 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- dynamic memory allocation (C++)
- Problem with memory allocation =( (C)
- Static and Dynamic Memory Allocations...Advan's & Disadvan's??? (Computer Science)
- regarding dynamic memory allocation (C)
- memory allocation ptr to array? how? (C)
- Dynamic memory allocation homework (C++)
Other Threads in the C++ Forum
- Previous Thread: Need Help Fast!!!
- Next Thread: fibonacci
| Thread Tools | Search this Thread |
api array auto based binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count delay-loading delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






