Please Help Dynamic Memory Allocation.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 15
Reputation: warpstar is an unknown quantity at this point 
Solved Threads: 0
warpstar warpstar is offline Offline
Newbie Poster

Please Help Dynamic Memory Allocation.

 
0
  #1
Oct 25th, 2007
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.

  1. int *copy_array ( int a[], int length )
  2. {
  3. int i;
  4. int b[length];
  5. for (i =0; i <= length, i++ )
  6. b[i] = a[i];
  7. return b[0];
  8. }

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: Please Help Dynamic Memory Allocation.

 
0
  #2
Oct 25th, 2007
>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++:
  1. int *copy_array ( int a[], int length )
  2. ...
  3. 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*.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 15
Reputation: warpstar is an unknown quantity at this point 
Solved Threads: 0
warpstar warpstar is offline Offline
Newbie Poster

Re: Please Help Dynamic Memory Allocation.

 
0
  #3
Oct 25th, 2007
What about in C. É


Originally Posted by Ptolemy View Post
>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++:
  1. int *copy_array ( int a[], int length )
  2. ...
  3. 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*.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Please Help Dynamic Memory Allocation.

 
0
  #4
Oct 25th, 2007
Originally Posted by warpstar View Post
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: Please Help Dynamic Memory Allocation.

 
0
  #5
Oct 25th, 2007
>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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC