pointer points elsewhere, but by changed it?

Discussion in 'C Programming' started by Pietro Cerutti, Aug 17, 2007.

  1. 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
    Pietro Cerutti, Aug 17, 2007
    #1
    1. Advertising

  2. Pietro Cerutti

    Chris Dollin Guest

    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
    Chris Dollin, Aug 17, 2007
    #2
    1. Advertising

  3. 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
    Pietro Cerutti, Aug 17, 2007
    #3
  4. Pietro Cerutti <> wrote:
    > 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 ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Aug 17, 2007
    #4
  5. Pietro Cerutti

    Chris Dollin Guest

    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
    Chris Dollin, Aug 17, 2007
    #5
  6. 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
    Pietro Cerutti, Aug 17, 2007
    #6
  7. Pietro Cerutti

    Chris Dollin Guest

    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
    Chris Dollin, Aug 17, 2007
    #7
  8. Pietro Cerutti wrote:

    > 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?
    Ivan Gotovchits, Aug 17, 2007
    #8
  9. 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:>

    Direct replies will be blacklisted. Replace "spamtrap" with my name to
    contact me directly.
    Clever Monkey, Aug 17, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    682
    Rosanne
    Oct 11, 2005
  2. nice.guy.nige
    Replies:
    3
    Views:
    433
    Tina - AffordableHOST, Inc.
    Jan 5, 2005
  3. G
    Replies:
    3
    Views:
    438
    Sylvester Hesp
    Jan 8, 2007
  4. Patrick Li

    Works in irb but not elsewhere

    Patrick Li, Aug 8, 2008, in forum: Ruby
    Replies:
    9
    Views:
    98
    Robert Klemme
    Aug 9, 2008
  5. Ted Byers
    Replies:
    2
    Views:
    75
    Thomas 'PointedEars' Lahn
    Aug 25, 2009
Loading...

Share This Page