473,326 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

pointer points elsewhere, but by changed it?

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!

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 17 '07 #1
7 1183
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.

--
Chris "It Could Be You -- Crashing And Burning" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Aug 17 '07 #2
Chris Dollin wrote:
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.
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".
--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 17 '07 #3
Pietro Cerutti <ga**@gahr.chwrote:
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?
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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Aug 17 '07 #4
Pietro Cerutti wrote:
Chris Dollin wrote:
>Pietro Cerutti wrote:

(big quote, small answer)
(fx:snip)
>>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.

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.

--
Chris "specificity" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Aug 17 '07 #5
Chris Dollin wrote:
Pietro Cerutti wrote:
>Chris Dollin wrote:
>>Pietro Cerutti wrote:

(big quote, small answer)

(fx:snip)
>>>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.
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.
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 ;-)
>
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.

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
Aug 17 '07 #6
Pietro Cerutti wrote:
Chris Dollin wrote:
>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 ;-)
Pleased to be able to help.

(fx:rubs-crystal-ball)

--
Chris "but the library function did nothing in the night" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Aug 17 '07 #7
Chris Dollin wrote:
Pietro Cerutti wrote:
>Chris Dollin wrote:
>>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 ;-)

Pleased to be able to help.

(fx:rubs-crystal-ball)
Dude, not in /public/.
--
clvrmnky <mailto:sp******@clevermonkey.org>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.
Aug 17 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: xuatla | last post by:
Hi, I have a class class myType { private: int size; double *elem; .....
10
by: quantdev2004 | last post by:
Hi all, I have been deling with this kind of code: class Foo { public: void NonConstMethod() {} };
22
by: Christopher Benson-Manica | last post by:
Is adding 0 to a pointer to non-void that is equal to NULL legal? int *p=NULL; p+=0; -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
4
by: mathon | last post by:
Hello, if i have these two declarations: 1)const int * test; and 2)int* const test;
7
by: william | last post by:
My question is: Specific memory block where my pointer pointing to changed strangely, seemingly that no statement changed it. Here are two examples I got: ***********1***************** I was...
50
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
6
by: Mahendra | last post by:
I have two cases - 1. When I have a pointer A pointing to a heap memory - What happens when I dereference the pointer A using free(A). It deallocated the heap memory the pointer was pointing...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.