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

A question here... Can I dynamicall

Status
Not open for further replies.

ralphtan

Technical User
Oct 15, 2002
15
SG
A question here... Can I dynamically allocate memory to a pointer in main() by calling a function that do allocation of memory.

eg codes:
/*******************/
#include <math.h>
#include <stdlib.h>
#include <stdio.h>


int mcalc(int* vect);

int main(){

int* ptr;
int i;

mcalc(ptr);

printf(&quot;ptr[3] = %i&quot;, ptr[3]);

return 0;
}

int mcalc(int* vect){

int x=0;

vect = (int*)malloc(10*sizeof(int));

for (x-0;x<10;x++){

vect[x] = x+1;
}
return 0;

}

/**********************/

Pls help. Thanks.
 
int* ptr = malloc(sizeof(int));

... use it

// free the memory allocated
free( ptr);

-pete
 
Hi, I have used that. But there is compiling error.

My ideas is to have a struct say...

typedef struct{
int len;
int *ptr;
} vect;

void main(){
vect a;

/*** allocate memory ***/
vect_allocate(a);
...
...
free (a.ptr);

}

void vect_allocate(vect a){
a.ptr = (int*)malloc(a.len*sizeof(int));
}

can I do this? it seem that i got some error. what is the problem?
 
>> void vect_allocate(vect a){

the problem is that in your function &quot;a&quot; is passed &quot;by value&quot; so the function has a &quot;copy&quot; of the outer scope variable. Therefore when you allocate the memory and assign it's address to the member of &quot;a&quot; in this function that does not change the value of the member of &quot;a&quot; in the calling scope.

To change the value of your struct you need to pass a pointer to it rather than the &quot;value&quot; of it

void vect_allocate( vect* ptr)

then...

/*** allocate memory ***/
vect_allocate(&a);

does that help?
-pete
 
I still got error...

/***********/
#include <math.h>
#include <stdlib.h>
#include <stdio.h>


typedef struct{
int *a;
int len;

} vect;

int mcalc(vect *ptr);

int main(){

vect data;

data.len = 10;
mcalc(&data);

return 0;
}

int mcalc(vect *ptr){

int x=0;
ptr.a = (int*)malloc(ptr.len);

for (x=0;x<10;x++){

ptr.a[x] = x+1;
}
return 0;

}
 
Don't cast malloc: include stdlib.h
instead.
Code:
   ptr.a = malloc(ptr.len * sizeof(int));

    if (ptr.a) {
        for (x=0 ; x < 10 ; x++) {
              ptr.a[x] = x+1;
        }
        return 0;
     }
return -1;
}




 
Duh..It just occurred to me what was wrong.

You are passing a pointer:
Your syntax is wrong:
Code:
   ptr->a = malloc(ptr->len * sizeof(int));

    if (ptr->a) {
        for (x=0 ; x < 10 ; x++) {
              ptr->a[x] = x+1;
        }
        return 0;
     }
return -1;
}
 
Thanks marsd, i got pulled into a meeting.. yuck :-(

-pete
 
Thanks guys.

Just another qustion... how can i pass pointer into a function by refernce?

say...

int* ptr;

g(ptr); /* this way? */

 
in standard C there is no &quot;reference&quot; you have to pass the address of the pointer so it becomes a pointer to a pointer

void g( int** pPtrInt);
// call g
int* pint;
g( &pint);

In C++ you can use reference like so
void g( int*& pPtrInt);
// call g
int* pint;
g( pint);

does that help?
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top