473,785 Members | 2,756 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 1696
go*********@gma il.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************ *********@e65g2 000hsc.googlegr oups.com>,
<go*********@gm ail.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
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 11 '07 #3
go*********@gma il.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
1572
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: StrVec_vec myData;
7
1489
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
2415
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), an iterator that I've set in a loop gets changed. Here's an example of the problem (sorry about the lack of indentation, posting this from Google): #include <vector> #include <iostream>
5
1303
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
2362
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. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
31
2642
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
1539
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. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
8
1832
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
3220
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: Whenever I declare the sockaddr_in structure inside the main, the debugger crashes at line X*, not being able to access argv parameters (see code below). It is very strange.. by only being there, sockaddr_in does not allow me to question argc...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4046
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 we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.