strange behaviour from a typedef in loop

G

gorbulastic

ok, i've created a typedef of somthing, and I put it through a for
loop to put the entries into it. They are stored as an array of the
typedef''s, with memory allocated via malloc command.

For some strange reason, one of the strings that make up the typedef,
ends up being exactly the same in every typdef in the array (and it
shouldn't). Ive been debugging and using printf's to view the before
and after values of this particular string, and all is well (they are
different for each typedef), but then 2 lines later i wrote another
small forloop for debugging to print that particular string out for
the entire array, and they are all the same! and the value they are,
is the last string value in the last entry of the array.

The two lines of code inbetween are as follows:
k++;
// which is an increment for the for loop
if (k == NUM_BUTS)
// which just tests for when the array is finished.

these two lines of code can't possibly alter one particular string
value in the entire array of typedefs. So what is going on? its acting
as if there is only one entry in the array and it defaults to the last
value that was set to it, except the other variables in each typedef
are all different as they should be.

Can anyone help? would this have anything to do with the memory
allocation, i using malloc, and free.
 
R

Richard Heathfield

(e-mail address removed) said:
ok, i've created a typedef of somthing, and I put it through a for
loop to put the entries into it.

typedef creates a synonym for an existing type. It is not a container.
They are stored as an array of the
typedef''s, with memory allocated via malloc command.

It is actually impossible to have an array of typedefs, because typedefs
are not objects - and malloc is a function, not a command.
For some strange reason, one of the strings that make up the typedef,

Strings don't make up typedefs.

Your question is impossible to fathom. I suggest you post the code that
you're struggling with, and perhaps someone will be able to offer hints
about how to fix it.
 
R

Richard Tobin

ok, i've created a typedef of somthing, and I put it through a for
loop to put the entries into it. They are stored as an array of the
typedef''s, with memory allocated via malloc command.

Presumably you mean you have defined a struct, and have an array of
structs. The fact that you use typedef to give a name to the struct
type is irrelevant.
For some strange reason, one of the strings that make up the typedef,
ends up being exactly the same in every typdef in the array (and it
shouldn't). Ive been debugging and using printf's to view the before
and after values of this particular string, and all is well (they are
different for each typedef), but then 2 lines later i wrote another
small forloop for debugging to print that particular string out for
the entire array, and they are all the same! and the value they are,
is the last string value in the last entry of the array.

My guess is that you have a char * in your struct, which you are
setting to the same value in each instance, and then strcpy()ing a
string to it. As a result you are overwriting the same memory with
each value in turn, and after you finish initialising they all point
to the same memory, which contains the last string.

But that's just a guess; you'll have to post the real code for a real
analysis.

-- Richard
 
C

Chris Dollin

ok, i've created a typedef of somthing, and I put it through a for
loop to put the entries into it. They are stored as an array of the
typedef''s, with memory allocated via malloc command.

(etc)

/Post code/. We can't see what you've done, so we can't see what
you've done wrong: we can only guess [1]. Make it complete, make it
small, make it properly indented with no tabs and no // comments
to wrap-round and confuse things.

[You also have a serious case of Shaky Nomenclature, but we'll soon
fix that. For example, there is no "malloc command"; `malloc` is
just a library /function/.]

[1] My money's on Richard Tobin's

My guess is that you have a char * in your struct, which you are
setting to the same value in each instance, and then strcpy()ing a
string to it.

or it's moral equivalent.

--
The second Jena user conference! http://hpl.hp.com/conferences/juc2007/
"He's dead, Jim, but not as we know it." Unsaid /Trek/

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

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top