value of struct member changes during runtime 
Author Message
 value of struct member changes during runtime

Hi,

I have looked at FAQ and old posting of this newsgroup, couldn't find
any similar therefore I thought I have to post my problem.

My problem is that I have a pointer to structure which member content
is changed during runtime, although I don't do it. There is neither
compile time warning/error nor run time warning/error.

The structure is:

typedef struct dnf_struct {
    int var_num;
    int cube_num;
    int func_num;

Quote:
} *DNFarray;

In main:

int main (int argc, char *argv [])
  bdd *boole_bdd;
  DNFarray my_dnf_array;
  ...
  my_dnf_array = gen_dnf_array (pla_file);
  ...

gen_dnf_array is returning DNFarray and it perfectly assigns an
address to pointer to struct. (pla_file is another pointer to struct
and there is no problem with pla_file at all.)

The rest of the main is interesting:
...
printf ("Func num: %d.\n", my_dnf_array->func_num);
boole_bdd = (bdd *) calloc (my_dnf_array->func_num, sizeof (bdd));
printf ("Func num: %d.\n", my_dnf_array->func_num);
...
During runtime, first printf above produces 2, but 2nd printf
produces 3!! And I have compiled/run the code hundred times,
"value change" is there and they are all the same!!

There is no function or whatever that changes my_dnf_array members,
between the initialization with gen_dnf_array and bool_bdd calloc.

I wonder if anybody knows the reason for that and how to overcome
this problem.

HW is UltraSPARC 80. RAM is 2 GB. Stack size is 64 MB.
OS is Sun Solaris 2.6 and GCC 2.95.2. I am using C90.

Utku



Mon, 01 Dec 2003 13:52:30 GMT  
 value of struct member changes during runtime

wrote in comp.lang.c:

Quote:
> Hi,

> I have looked at FAQ and old posting of this newsgroup, couldn't find
> any similar therefore I thought I have to post my problem.

> My problem is that I have a pointer to structure which member content
> is changed during runtime, although I don't do it. There is neither
> compile time warning/error nor run time warning/error.

> The structure is:

> typedef struct dnf_struct {
>     int var_num;
>     int cube_num;
>     int func_num;
> } *DNFarray;

> In main:

> int main (int argc, char *argv [])
>   bdd *boole_bdd;

What's a bdd?

Quote:
>   DNFarray my_dnf_array;
>   ...
>   my_dnf_array = gen_dnf_array (pla_file);

You say (below) that gen_dfn_array() returns a valid pointer to one or
more DNFarray structures, and that it does it perfectly.  But
something in your code isn't working perfectly even though you think
it is.  The problem might be in that function.  Then again it might
not be.

Quote:
>   ...

> gen_dnf_array is returning DNFarray and it perfectly assigns an
> address to pointer to struct. (pla_file is another pointer to struct
> and there is no problem with pla_file at all.)

> The rest of the main is interesting:
> ...
> printf ("Func num: %d.\n", my_dnf_array->func_num);
> boole_bdd = (bdd *) calloc (my_dnf_array->func_num, sizeof (bdd));

By any chance have you neglected to include <stdlib.h>?  The cast on
the return value of calloc() is decidedly suspicious.  Do you get a
message from the compiler about converting an int to a pointer without
a cast if you leave it out?

The recommended comp.lang.c method of calling calloc() is:

boole_bdd = calloc(my_dfn_array->func_num, sizeof *boole_bdd);

Are you checking the return value of the calloc() call for NULL?

- Show quoted text -

Quote:
> printf ("Func num: %d.\n", my_dnf_array->func_num);
> ...
> During runtime, first printf above produces 2, but 2nd printf
> produces 3!! And I have compiled/run the code hundred times,
> "value change" is there and they are all the same!!

> There is no function or whatever that changes my_dnf_array members,
> between the initialization with gen_dnf_array and bool_bdd calloc.

> I wonder if anybody knows the reason for that and how to overcome
> this problem.

> HW is UltraSPARC 80. RAM is 2 GB. Stack size is 64 MB.
> OS is Sun Solaris 2.6 and GCC 2.95.2. I am using C90.

> Utku

Where is the function gen_dfn_array() getting the address is perfectly
assigns to a pointer to the struct?  Is it dynamically allocated?
Does it have static storage duration?  Or is it just an automatic
local variable defined in the function that goes out of scope when the
function returns, leaving main() with a pointer to invalid storage?

There are far, far to many possible sources of error to offer much
help.  Think of it this way:  you are looking at your code and can't
find the bug in it.  It is not likely that we can find the bug without
seeing it at all.

If none of the suggestions I made above get you anywhere, you are
going to need to post more of your code, especially the code that
returns the pointer.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Mon, 01 Dec 2003 14:33:57 GMT  
 value of struct member changes during runtime

Quote:
> > int main (int argc, char *argv [])
> >   bdd *boole_bdd;

> What's a bdd?

It is defined in bdd.h and I already use #include <bdd.h> for that. It is

typedef int BDD;
typedef BDD bdd;

Quote:
> >   DNFarray my_dnf_array;
> >   ...
> >   my_dnf_array = gen_dnf_array (pla_file);

> You say (below) that gen_dfn_array() returns a valid pointer to one or
> more DNFarray structures, and that it does it perfectly.  But
> something in your code isn't working perfectly even though you think
> it is.  The problem might be in that function.  Then again it might
> not be.

The code of gen_dnf_array is as follows:

typedef struct generic_text_array {
    char **text_array;
    int line_num;

Quote:
} *TextArray;

DNFarray gen_dnf_array (TextArray pla_file)
{
    ...
    my_dnf_array = (DNFarray) malloc (sizeof (DNFarray));
    if (my_dnf_array == NULL) {puts ("Unable..."); exit (0);
    my_dnf_array->var_num = arg_in_str (pla_file);
    my_dnf_array->func_num = arg_in_str (pla_file);
    my_dnf_array->cube_num = arg_in_str (pla_file);
    ...
    /* there are some if-checks for pla_file controlled by
    my_dnf_array members */
    ...
    return (my_dnf_array);

Quote:
}
> By any chance have you neglected to include <stdlib.h>?  The cast on
> the return value of calloc() is decidedly suspicious.  Do you get a
> message from the compiler about converting an int to a pointer without
> a cast if you leave it out?

No, I haven't. All the functions are defined in a custom *.h file. The *.h files and
*.c file where "main" have all #includes. In ipcp.c, where int main (...) is:

#include <bdd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

Quote:
> The recommended comp.lang.c method of calling calloc() is:

> boole_bdd = calloc(my_dfn_array->func_num, sizeof *boole_bdd);

Ok, I didn't do that. In general, I use following coding, which are, I think, not good:

char **array;
array = (char **) calloc (nelem, sizeof (char *));
*(array + offset) = (char *) calloc (nelem, sizeof (char));

Quote:
> Are you checking the return value of the calloc() call for NULL?

Yes, NULL check is there:

if (boole_bdd == NULL) {puts ("Unable..."); exit (0);}

Quote:
> > printf ("Func num: %d.\n", my_dnf_array->func_num);
> > ...
> > During runtime, first printf above produces 2, but 2nd printf
> > produces 3!! And I have compiled/run the code hundred times,
> > "value change" is there and they are all the same!!

> > There is no function or whatever that changes my_dnf_array members,
> > between the initialization with gen_dnf_array and bool_bdd calloc.

> > I wonder if anybody knows the reason for that and how to overcome
> > this problem.

> > HW is UltraSPARC 80. RAM is 2 GB. Stack size is 64 MB.
> > OS is Sun Solaris 2.6 and GCC 2.95.2. I am using C90.

> > Utku

> Where is the function gen_dfn_array() getting the address is perfectly
> assigns to a pointer to the struct?  Is it dynamically allocated?

DNFarray gen_dnf_array (TextArray pla_file); dynamically allocates the pointer to struct.

Quote:
> Does it have static storage duration?  Or is it just an automatic
> local variable defined in the function that goes out of scope when the
> function returns, leaving main() with a pointer to invalid storage?

I don't use any "static" keyword in my codes but in bdd.h. gen_dnf_array just initializes
my_dnf_array locally and returns it to main. The caller, main, uses it for its purposes.
I have checked my_dnf_array with printf ("%p\n", (void *) my_dnf_array); and it returns
a non-zero value. The member value of struct are meaningful, so it seems it is not
invalid.

Quote:
> There are far, far to many possible sources of error to offer much
> help.  Think of it this way:  you are looking at your code and can't
> find the bug in it.  It is not likely that we can find the bug without
> seeing it at all.

> If none of the suggestions I made above get you anywhere, you are
> going to need to post more of your code, especially the code that
> returns the pointer.

Thank you very much, Jack, for your info in advance.

Utku



Mon, 01 Dec 2003 15:13:59 GMT  
 value of struct member changes during runtime


Quote:
>     my_dnf_array = (DNFarray) malloc (sizeof (DNFarray));

There's your error. A DNFarray [why "array"?] is a pointer-to-struct
[with three integer elements, according to your previous post]. Your
`sizeof` tells you how big the *pointer* is, but you need to know how
big the *structure* is. If the structure is bigger than the pointer
(likely) then you haven't got enough store, and the place you *think*
the later elements live in can get re-used by a later allocation.

    my_dnf_array = malloc( sizeof (*my_dnf_array) );

would be better.

--
Chris "may be other errors, of course" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html



Mon, 01 Dec 2003 16:31:23 GMT  
 value of struct member changes during runtime

Quote:



> >     my_dnf_array = (DNFarray) malloc (sizeof (DNFarray));

> There's your error. A DNFarray [why "array"?]

Yes, you're right, name was because of "backward compatibility".

Quote:
> is a pointer-to-struct
> [with three integer elements, according to your previous post]. Your
> `sizeof` tells you how big the *pointer* is, but you need to know how
> big the *structure* is. If the structure is bigger than the pointer
> (likely) then you haven't got enough store, and the place you *think*
> the later elements live in can get re-used by a later allocation.

>     my_dnf_array = malloc( sizeof (*my_dnf_array) );

> would be better.

Thank you very much Chris. I haven't paid attention to struct initialization.

I have learned invaluable things from you, Chris and Jack.
Thank you very much for your contributions.

A last question: which one is better, in terms portability, re-usability etc.?

typedef struct some_struct { ... } *myStruct;
typedef struct some_struct { ... } myStruct;

Utku



Mon, 01 Dec 2003 18:37:49 GMT  
 value of struct member changes during runtime


Quote:
> A last question: which one is better, in terms portability, re-usability etc.?

> typedef struct some_struct { ... } *myStruct;
> typedef struct some_struct { ... } myStruct;

Neither: it depends.

There are Respectable posters who advocate not using typedefs for structs,
and Respectable posters who dislike "hiding" pointer types behind typedefs.

The best advice I can offer is: make sure it's clear to your intended
audience what's going on, and listen to advice from *multiple* people
on what you should do before you make a decision. Read existing code
to see what works and what doesn't.

Now, in the code *I* am writing, I *do* use typedefs for structures,
and I *do* "hide" pointers behind typedefs. I have (what I think are)
good reasons for doing this:

    * almost all the non-primitive types that get used in the code
      are pointer types, essentially reference-to-object types: without
      "hiding" their pointerness, most variable & parameter declarations
      would be `Spoo *` rather than `Spoo`. I don't think that would
      add clarity to the code.

    * outside their defining translation unit, access to properties of
      values is made by function-call (or what *looks* like function-call).
      This is the usual "protect me from knowing about the insides"
      encapsulation.

    * different implementations may need to choose different types for
      eg the "object references". For example, a `MillValue` (which
      represents a value which may be a small integer or a reference
      to an "object") is implemented in the current code as a tagged
      pointer (don't look inside *that*!). But it might be necessary
      to replace it by an integer index into an array later. It is
      not wise to require that the *'s on the variable types get removed
      at that time ... [the implementation module is a different case].

If that's confusing, ignore it for now, and listen to the other advice
that will doubtless be along on the next bus.

PS read all articles by Chris Torek.

--
Chris "he is Respectable" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html



Mon, 01 Dec 2003 19:09:15 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Loading a VARIANT member of struct during variable instantiation

2. Change mdi background during runtime

3. adodc - changing the filter during runtime is possible?

4. Changing a Dialog's CAPTION during runtime

5. How To change a LTEXT Resource during runtime?

6. How to change language of interface during runtime?

7. Best way to change text attributes during runtime?

8. adodc: changing filter during runtime is possible?

9. Q Menu item change during runtime (2)

10. Q: How can I change menu items during runtime

11. Changing Tab Order during runtime

12. How To change a LTEXT Resource during runtime?

 

 
Powered by phpBB® Forum Software