Archived

This topic is now archived and is closed to further replies.

allocate point in sub funtion

This topic is 5586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a struct that is person_t, and have a sub function that is subfunction( person_t *person ); In subfunction I allocate person and write date to it, but after subfunction return, the date of person will be lost. example, in subfunction: person = new person_t[ numPerson ]; write date to person point.. check date success.. return In Main function: person_t *person; subfunction( person ); check date always failed.

Share this post


Link to post
Share on other sites
As far as I know, pointers aren''t any different from other variables in that their content is copied. If you want to change the value of the actual pointer that you pass to the function, you will have to pass it by reference or something of the kind. I''m not an expert, so I''m not sure, but I think this ought to more or less work:


  
subfunction(person_t **person)
{
*person = new person_t[ numPerson ];
//write date to person point..

//check date success..

//return

}

Share this post


Link to post
Share on other sites
in C (and C++), parameters to functions are passed by value, not by reference ... which means, if you pass a value to a function like this:

int x = 5;
SomeFunctionWhichChangesParameterTo7(x);

then the value of x after the function is 5, because C and c++ have copied it ... to change the value of x, you must pass a pointer or reference to it ... like this:

instead of:

void SomeFunc(int value);
// called with
SomeFunc(x);

use either:

void SomeFunc(int &value);
// called with
SomeFunc(x);

or

void SomeFunc(int *value);
// called with
SomeFunc(&x);

OK ... so now ... in your case, you are wanting to CHANGE the value of a pointer object, so you have to pass a pointer or reference to your pointer. Instead of:

subfunction(person_t *person);
// called like
subfunction(person);

you must use either:

subfunction(person_t **person);
// called like
subfunction(&person);

or

subfunction(person_t *&person);
// called like
subfunction(person);

understand?

... then it will work ... but realize ... you (the calling function) is now the owner of the memory .. and must delete it when it is done with it, and make sure and use "delete []" since you allocated it as an array.


Good Luck

Share this post


Link to post
Share on other sites

I still have some puzzle, please see strcpy function:

strcpy( char *src, const char *dest );

It arg is not "char *& " or "char **", than I suppose it not be allocate memory, though, if I don''t allocate src firte, it also make right result to me.

Share this post


Link to post
Share on other sites
yeah it does work, till the next moment it crashes.. u are using c++, so i suggest you bether use std::string in include ''string''.. it does do the memory management for you.. all the string.h strcpy and those don''t..

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
strcpy does not allocate any memory ... so it is up to YOU to pass a pointer for src that can be written on safely for at least as far as strlen(dest) ...

ALSO .. the two cannot overlap ... all clearly stated in a C reference manual ...

think of strcpy as just a simple memcpy ... except instead of taking a number of bytes to copy, it detects it by finding the ''\0'' (null) character.

Share this post


Link to post
Share on other sites