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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use of malloc()

Status
Not open for further replies.

neos

Programmer
Oct 28, 2000
36
CA
I am new to C. I am trying to get an understanding of the malloc function for creating variables while the program is running. The program I wrote below, I don't know if it works or not, is the new variable (array in this case)being created?

#include <stdio.h>

void create_array() {
int i;
int *ptr=(int)malloc(sizeof(int)*50);
printf(&quot;Array being created any initialized...\n&quot;);
for(i = 0; i < 51; ++i) {
ptr = i;
printf(&quot;%d\n&quot;, i);
}
}

int main() {
int create;
printf(&quot;Would you like to create an array of type int with 50 elements?\n1 - Yes\t0 - No: &quot;);
scanf(&quot;%d&quot;, &create);
if(create) {
create_array();
}
}

shaun
 
1.You should write :
int *ptr=(int*)malloc(sizeof(int)*50);
Here only the required memory to store 50 integers is allocated. No default values are put in each slot. You have to take care to initialize each slot with a value e.g. what you do by assigning the value in each slot equal to the position of the slot.

2.To see what you assigned in each slot of the memory pointed by ptr , you have to put in the loop:
printf(&quot;%d\n&quot;, ptr); or printf (&quot;%d\n&quot;, *(ptr+i));

Do you see the equivalence between two forms : ptr and *(ptr + i) ?
3. To be Okay you have to free the allocated memory somewhere and I see (as the program is written) this could be done only in the create_array() function. So you have to add :
if (ptr)
{
free ptr;
}

-obislavu-
 
The code you've written is what's called a &quot;memory leak&quot;. That is, you allocate memory in a function, but lose handles to it, so it keeps eating memory.

Your malloc() is in create_array(), but as soon as that function returns, you have no reference to the newly allocated memory, so you can't use it, and it's being used anyway.

Something like this would be better...
[tt]
#include <stdio.h>

int * create_array() {
int j;
int *ptr=(int)malloc(sizeof(int)*50);
printf(&quot;Array being created any initialized...\n&quot;);
for(j = 0; j < 51; ++j) {
ptr[j] = j;
printf(&quot;%d\n&quot;, j);
}
return( ptr );
}

int main() {
int create;
int thearray[];

printf(&quot;Would you like to create an array of type int with 50 elements?\n1 - Yes\t0 - No: &quot;);
scanf(&quot;%d&quot;, &create);
if(create) {
thearray = create_array();
}
}
[/tt]
Or something like that.

Hope this helps.

 
> int *ptr=(int)malloc(sizeof(int)*50);
1. The cast is wrong - it should be (int*)
2. The cast is unnecessary in ANSI-C.
At best, it does nothing
At worst, it hides the fact that you didn't include stdlib.h

> for(j = 0; j < 51; ++j) {
This steps off the end of the array by 1 integer
Use
for(j = 0; j < 50; ++j) {

> int thearray[];
You mean
int *thearray;

--
 
Thanks for your help guys.

-obislavu-: I was not sure why I had to put the
if(ptr) {
free ptr;
}
into the code. I would only free that allocated memory if I wasn't going to use the array later, right?

shaun
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top