473,508 Members | 2,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strange behaviour from a typedef in loop

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.

Apr 11 '07 #1
3 1676
go*********@gmail.com 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 11 '07 #2
In article <11*********************@e65g2000hsc.googlegroups. com>,
<go*********@gmail.comwrote:
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 11 '07 #3
go*********@gmail.com wrote:
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

Apr 11 '07 #4

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

Similar topics

5
1560
by: RCS | last post by:
I have this Functor: typedef std::vector < std::string > String_vec; typedef std::vector < String_vec > StrVec_vec; class Compare : public std::unary_function<String_vec, void> { public:...
7
1472
by: Hamish | last post by:
Hello, lately I've been having a lot of trouble with the std::vector. Seems to create unpredictable behaviour within my code. Example: struct Switch{ int i; int j; double d; };
2
2393
by: Dave | last post by:
I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not sure if this is correct behavior or not, and I'm using the gcc STL and compiler. When calling vector<int>::push_back(0),...
5
1289
by: Levent | last post by:
Hi, When compiled with gcc 3.3.3 and lower on various systems (tried cygwin, linux, aix) the following code behaves strangely: #include <iostream> class Foo { public: typedef int subs;
3
2333
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
31
2590
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
2
1523
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
8
1810
by: manochavishal | last post by:
Hi, I have a structure MAX_ID is 5 /**Structure for Copies*/ typedef struct NodeVideoCopy * NodeVideoCopyPtr ; typedef struct NodeVideoCopy {
8
3187
by: FBM | last post by:
Hi there, I am puzzled with the behavior of my code.. I am working on a networking stuff, and debugging with eclipse (GNU gdb 6.6-debian).. The problem I am experiencing is the following: ...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7124
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7385
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1558
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.