pointer points elsewhere, but by changed it?

P

Pietro Cerutti

Hi group.
I have a problem in a program structured like this:

void function_1(my_data_t *data);
my_data_t *create_data(void);
void library_function(void);

int main(void)
{
my_data_t *data;
data = create_data();

/* data is OK here */

function_1(data);

/* data points to nowhere now!! */

return (0);
}

void
function_1(my_data_t *data)
{
/*
do something with data
*/

/* data is OK here */

library_function();

/* data in main() points to nowhere now!! */
}

my_data_t *
create_data(void)
{
/* malloc an object of type my_data_t and
* return a pointer to it
*/
}

Is there any other possible reasons apart from errors / memory
corruptions in library_function() which could cause the pointer "data"
in main to change it's address?

Thank you!
 
C

Chris Dollin

Pietro Cerutti wrote:

(big quote, small answer)
Hi group.
I have a problem in a program structured like this:

void function_1(my_data_t *data);
my_data_t *create_data(void);
void library_function(void);

int main(void)
{
my_data_t *data;
data = create_data();

/* data is OK here */

function_1(data);

/* data points to nowhere now!! */

return (0);
}

void
function_1(my_data_t *data)
{
/*
do something with data
*/

/* data is OK here */

library_function();

/* data in main() points to nowhere now!! */
}

my_data_t *
create_data(void)
{
/* malloc an object of type my_data_t and
* return a pointer to it
*/
}

Is there any other possible reasons apart from errors / memory
corruptions in library_function() which could cause the pointer "data"
in main to change it's address?

Calling the library function with incorrect parameters. I find it
unlikely that your /actual/ library function takes no arguments and
delivers no results, so I /suspect/ that your call doesn't match
its requirements. At which point All Bets Are Off.
 
P

Pietro Cerutti

Chris said:
Pietro Cerutti wrote:

(big quote, small answer)


Calling the library function with incorrect parameters. I find it
unlikely that your /actual/ library function takes no arguments and
delivers no results, so I /suspect/ that your call doesn't match
its requirements. At which point All Bets Are Off.

The pseudo-code posted is not the actual one.
My goal was just to show the program structure. Actually,
library_function takes parameters and deliver output, but they are both
unrelated to "data".
 
J

Jens Thoms Toerring

Pietro Cerutti said:
I have a problem in a program structured like this:
void function_1(my_data_t *data);
my_data_t *create_data(void);
void library_function(void);
int main(void)
{
my_data_t *data;
data = create_data();
/* data is OK here */

/* data points to nowhere now!! */
return (0);
}
void
function_1(my_data_t *data)
{
/*
do something with data
*/
/* data is OK here */

/* data in main() points to nowhere now!! */
}
my_data_t *
create_data(void)
{
/* malloc an object of type my_data_t and
* return a pointer to it
*/
}
Is there any other possible reasons apart from errors / memory
corruptions in library_function() which could cause the pointer "data"
in main to change it's address?

None that I would see. Since 'data' is passed by value (i.e. a
copy is made of this pointer) from main() to fucntion_1() its
value in main() can't be changed from within function_1() with-
out a bug somewhere in function_1() or the functions called by
it. So you're probably writing somewhere over memory you're not
allowed to change (since 'data' will probably be stored on the
stack one possibility is that it gets messed up by writing out-
side of the bounds of an automatic array).

Regards, Jens
 
C

Chris Dollin

Pietro said:
(fx:snip)


The pseudo-code posted is not the actual one.
My goal was just to show the program structure. Actually,
library_function takes parameters and deliver output, but they are both
unrelated to "data".

And if you've got the arguments to that library function wrong, it
/doesn't matter/ whether they look related to `data` or not.

Maybe that's covered by your "apart from errors / memory corruptions"
above -- but it need not be that the library function is broken,
just that your call of it is; eg passing null when a proper pointer
is required, passing (the address of) an array with insuffiently
many elements, forgetting to initialise some pointer/index, etc.
 
P

Pietro Cerutti

Chris said:
And if you've got the arguments to that library function wrong, it
/doesn't matter/ whether they look related to `data` or not.

Exactly that!
The library function expected a pointer to an array of a certain size.
My array variable was smaller, so memory past the end of the array was
written.
My "data" variable just happened to be in that area...

Thanks ;-)
 
C

Chris Dollin

Pietro said:
Chris Dollin wrote:

Exactly that!
The library function expected a pointer to an array of a certain size.
My array variable was smaller, so memory past the end of the array was
written.
My "data" variable just happened to be in that area...

Thanks ;-)

Pleased to be able to help.

(fx:rubs-crystal-ball)
 
I

Ivan Gotovchits

Pietro said:
Hi group.
I have a problem in a program structured like this:

void function_1(my_data_t *data);
my_data_t *create_data(void);
void library_function(void);

int main(void)
{
my_data_t *data;
data = create_data();

/* data is OK here */

function_1(data);

/* data points to nowhere now!! */

return (0);
}

void
function_1(my_data_t *data)
{
/*
do something with data
*/

/* data is OK here */

library_function();

/* data in main() points to nowhere now!! */
}

my_data_t *
create_data(void)
{
/* malloc an object of type my_data_t and
* return a pointer to it
*/
}

Is there any other possible reasons apart from errors / memory
corruptions in library_function() which could cause the pointer "data"
in main to change it's address?

Thank you!
Is your programm is multithreaded? May be the library is not thread-safe ...
And what library, by the way? Custom or official one?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top