•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 423,380 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,839 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 718 | Replies: 4
![]() |
•
•
Join Date: Oct 2007
Posts: 15
Reputation:
Rep Power: 1
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.
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++:
cplusplus 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:
Rep Power: 1
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.cplusplus 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: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,123
Reputation:
Rep Power: 38
Solved Threads: 929
Last edited by Ancient Dragon : Oct 25th, 2007 at 5:51 pm.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- dynamic memory allocation (C++)
- Problem with memory allocation =( (C)
- Static and Dynamic Memory Allocations...Advan's & Disadvan's??? (Computer Science and Software Design)
- 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



Linear Mode