Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically Allocate Array of Intergers

Status
Not open for further replies.

donniea21

MIS
Feb 16, 2001
75
US
Here is the problem....I need to have a program that Dynamically Allocatea an Array of Intergers. The size of the array should be input from the keyboard. The elements of the array should be assigned values input from the keyboard. I then need to reallocate the memory for the array to 1/2 of the current number of elements. I know this is long but any help would be greatly appreciated.
 
This sounds like a homework assignment. Please submit your best attempt and we'll help. To get you started, look at fgets() and strtol() to get all user input (much less of a hassle than scanf()), look at malloc() for your initial "dynamic array" allocation and look at realloc() when you need to 1/2 the array.

Russ
bobbitts@hotmail.com
 
THis is for a extra credit homework asignment. I am having a hard time getting started.. My book does not really cover this to well. I was planning on using calloc at first then realloc like rbobbitt suggested. I just can't get started here. If someone could help with just the variables and initalization, that might get me going. thanks.
 
1. You would declare the pointer and "array" size parameter like this:

int *array;
size_t size;

2. Get the user input for the array size and store it in the variable size.

3. Do the inital allocation like this:

array=malloc(size * sizeof *array);

4. Loop (from 0 to 49) get the user values, storing each value in the corresponding array element.

5. 1/2 the size of the array:

int *tmp;

tmp=realloc(array,(size/2) * sizeof *tmp);

if (tmp!=NULL) {
array=tmp;
} else {
/* realloc() failed */
}

This is incomplete, but hopefully enough to get you on the right track.

Russ
bobbitts@hotmail.com
 
this is what i have gotten so far...i need to print out the new array after i reallocate the size. i.e. The origional array is
5
6
7
8
After i half the memory i should print out
5
6
Right???

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>


int main()
{
long *array;
size_t size;
int i;
int element;

printf(&quot;Enter the number of elements in the array: &quot;);
scanf(&quot;%d&quot;, &size);


array=(long*)calloc(size, (size* sizeof(long)));

if( array != NULL ){

for (i=+1; i<=size; i++){

array = i;

printf(&quot;Enter element Number%d: &quot;, i);

scanf(&quot;%d&quot;, &element);

array = element;}


printf(&quot;\n\nThe elements of the array are:\n\n&quot;);

for (i=+1; i<=size; i++)

printf(&quot;%d\n\n&quot;, array);

size = _msize( array );

printf(&quot;The Memory Allocated for this array is:%u\n&quot;,
size );

array = (long*)realloc(array,(size*.5));

size = _msize( array );

printf(&quot;the new size is:%u\n&quot;, size);

}

free (array);


return 0;
}
 
When you post code, make sure you turn off TGML processing or surround your code with
Code:
tags (I think). I've tried to reconstruct your code as TT interprets the as &quot;start italics&quot; and therefore has wiped out any you had in your code.

>#include <stdio.h>
>#include <malloc.h>
>#include <stdlib.h>


>int main()
>{
> long *array;
> size_t size;
> int i;
> int element;
>
> printf(&quot;Enter the number of elements in the array: &quot;);

fflush(stdout);

Since output is line buffered, you can't be sure the user will see your prompt before being able to enter input. A call to fflush() ensures this.

> scanf(&quot;%d&quot;, &size);>
>
> array=(long*)calloc(size, (size* sizeof(long)));

A few things here:

1. You don't need to call calloc() here, malloc() will do fine. Since you iterate through the entire array anyway, the initial value of the elements isn't important.

2. You don't need to cast the return value of calloc().

3. You're call to calloc() isn't quite right. Since you're requesting size elements each of size long, you shouldn't multiply in the 2nd argument:

array=calloc(size,sizeof(long));

Or better for code maintenance, in case the data type of array should change:

array=calloc(size,sizeof *array);

But, you don't really need calloc(), so:

array=malloc(size * sizeof *array);

>
> if( array != NULL ){
>
> for (i=+1; i<=size; i++){

for (i=0;i<size;i++) {

Indexing starts at 0 and extends to 1 less than size.

>
> array = i;

This is unnecessary.

>
> printf(&quot;Enter element Number%d: &quot;, i);

fflush(stdout);

>
> scanf(&quot;%d&quot;, &element);
>
> array = element;}

You could make this into one statement:

scanf(&quot;%d&quot;,&array);

But this should be:

scanf(&quot;%ld&quot;,&array);

the data type of each element is long

>
>
> printf(&quot;\n\nThe elements of the array are:\n\n&quot;);
>
> for (i=+1; i<=size; i++)

Again:

for (i=0;i<size;i++)

>
> printf(&quot;%d\n\n&quot;, array);

printf(&quot;%ld\n\n&quot;,array);

Again, long not int

> size = _msize( array );

Apparently _msize() returns the amount of memory that array points to, but you already know this, so why call a function to tell you it again - even more a function that makes your code non-portable.

>
> printf(&quot;The Memory Allocated for this array is:%u\n&quot;,
> size );

To print variables of type size_t, you should use a cast:

printf(&quot;The Memory Allocated for this array is:%lu\n&quot;,
(unsigned long)size );

size_t is guaranteed to be of typed unsigned integer, but the exact data type (unsigned int, unsigned long) is implementation-defined, so going with unsigned long is good.

> array = (long*)realloc(array,(size*.5));

A few things here:

1. You should use a temporary pointer when calling realloc(). If you don't and realloc() fails and returns NULL, you create a memory leak.

2. No need to cast the return value of realloc()

3. As mentioned above, the call to _msize is superfluous as size already contained what you needed to know to &quot;halve the array.&quot; So, pretending size wasn't assigned the return value of _msize, here's how you would do it:

long *tmp; /* declared at the top of main() */

/* ... */

tmp=realloc(array,(size/2) * sizeof *tmp);

if (tmp!=NULL) {
array=tmp;
} else {
/* realloc() failed, but you can still release
* the memory pointed to by array
*/
}

> size = _msize( array );

> printf(&quot;the new size is:%u\n&quot;, size);

Again, you don't need _msize to tell you this:

printf(&quot;The new size is %lu\n&quot;,
(unsigned long)(size * sizeof *array)));

> }

> free (array);

This is technically Ok, but doesn't read well. In other words, you call free on array regardless of whether or not the initial allocation succeeded. It's a no-op and safe to call free on a NULL pointer, but is a little sloppy (IMO).

>
> return 0;
>}

One other thing. You may be having difficulties with scanf(), particularly the calls in your loop to get the element values. It can be a difficult function to implement robustly, so fgets() with a subsequent call to strtol() would be preferable. If you haven't experienced problems, try entering arbitrary input in the loop and you'll see what I mean.

Hope this helped,

Russ
bobbitts@hotmail.com
 
Oops, a correction:

printf(&quot;The new size is %lu\n&quot;,
(unsigned long)(size * sizeof *array)));

Change to:

printf(&quot;The new size is %lu\n&quot;,
(unsigned long)((size/2) * sizeof *array)));

Russ
bobbitts@hotmail.com
 
rbobbitt,
Thank you for all of your help but i still have one probelm, I need to print the array after i reallocated the memory, when i do this i get a 0. It should print out half of the origional array. Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top