Thread: [Dev-C++] Stress with structs and pointers.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: David M. <ci...@ya...> - 2003-06-12 00:15:58
|
Ok, I need some clarification on a couple topics invloving pointers and pointers.
1) when a struct is created. All that can be assumed is that it contains trash right? What is a portable way to initalize everything to 0? mset?
2) when a structure contains a pointer. Access to the address (not the value) comes in the form {struct_variable_name}.{member_name}?
3) how do you de-refrence the pointer in #2 to get the value of the variable? {struct_variable_name}.*{member_name}?
4) if you have a pointer to the structure you access the members via '->'. If a member of the structure is a pointer the address is gotten via {struct_varible_name}->{member_name}? If so how does one get the value of the varible? {struct_variable_name}->*{member_name}?
Thanks in Advance
David McKen
P.S. Was begining to run out of hair!
---------------------------------
Do you Yahoo!?
Free online calendar with sync to Outlook(TM). |
|
From: Glen V. <gve...@pa...> - 2003-06-12 07:17:06
|
1) Structs also support constructors (see below for example program)
2) Don't know what you mean. But if you mean that it can take any type
of members: yes. If you want to get to that member, just do it the same
way: struct.membername. It's the same way for pointers or other variables.
3) *(struct.membername). It's very easy to test this yourself.
4) plain logic: you got a pointer to struct, so use struct->member. The
variable is a pointer, so dereference the endresult: *(struct->member).
*((*struct).member) would be an alternative way (dereference your
pointer to struct, get its member, dereference that) :)
I wrote a little test to be sure (and in the process I learned stuff too
:) ). This incorporates everything you need to know:
#include <iostream>
#include <cstdlib>
using namespace std;
typedef struct test{
public:
int* a;
test();
};
test::test(){
a = new(int);
*a = 5;
cout << "In constructor: " << *a << endl;
}
int main(){
test *x = new test();
cout << "*(x->a): " << *(x->a) << endl;
cout << "*((*x).a): " << *((*x).a) << endl;
system("pause");
}
David McKen wrote:
> Ok, I need some clarification on a couple topics invloving pointers
> and pointers.
>
> 1) when a struct is created. All that can be assumed is that it
> contains trash right? What is a portable way to initalize everything
> to 0? mset?
>
> 2) when a structure contains a pointer. Access to the address (not the
> value) comes in the form {struct_variable_name}.{member_name}?
>
> 3) how do you de-refrence the pointer in #2 to get the value of the
> variable? {struct_variable_name}.*{member_name}?
>
> 4) if you have a pointer to the structure you access the members via
> '->'. If a member of the structure is a pointer the address is gotten
> via {struct_varible_name}->{member_name}? If so how does one get the
> value of the varible? {struct_variable_name}->*{member_name}?
>
> Thanks in Advance
> David McKen
>
> P.S. Was begining to run out of hair!
> ------------------------------------------------------------------------
> Do you Yahoo!?
> Free online calendar
> <http://us.rd.yahoo.com/mail_us/tag/*http://calendar.yahoo.com> with
> sync to Outlook(TM).
|
|
From: Ricardo A. <ri...@te...> - 2003-06-12 08:11:22
|
> ). This incorporates everything you need to know:
>
> #include <iostream>
> #include <cstdlib>
> using namespace std;
>
> typedef struct test{
> public:
> int* a;
> test();
> };
>
> test::test(){
> a = new(int);
> *a = 5;
> cout << "In constructor: " << *a << endl;
> }
>
> int main(){
> test *x = new test();
> cout << "*(x->a): " << *(x->a) << endl;
> cout << "*((*x).a): " << *((*x).a) << endl;
> system("pause");
> }
>
Not bad, but you should free the memory previously allocated:
int main(){
test *x = new test();
cout << "*(x->a): " << *(x->a) << endl;
cout << "*((*x).a): " << *((*x).a) << endl;
system("pause");
delete x->a
delete x;
}
Regards
--
---------------------------------------------------
Ricardo Amores Hernández
ICQ: 19463735
MSN: zheo_ (at) hotmail (dot) es
---------------------------------------------------
|
|
From: David M. <ci...@ya...> - 2003-06-12 15:01:07
|
You sure constructors will work in a C enviroment? That is
one pre-requisite that I am hating.
With respect to #2, I was asking how you would get to the
value pointed to by a pointer that is part of a struct.
--- Glen Vermeylen <gve...@pa...> wrote:
> 1) Structs also support constructors (see below for
> example program)
> 2) Don't know what you mean. But if you mean that it can
> take any type
> of members: yes. If you want to get to that member, just
> do it the same
> way: struct.membername. It's the same way for pointers or
> other variables.
> 3) *(struct.membername). It's very easy to test this
> yourself.
> 4) plain logic: you got a pointer to struct, so use
> struct->member. The
> variable is a pointer, so dereference the endresult:
> *(struct->member).
> *((*struct).member) would be an alternative way
> (dereference your
> pointer to struct, get its member, dereference that) :)
>
> I wrote a little test to be sure (and in the process I
> learned stuff too
> :) ). This incorporates everything you need to know:
>
> #include <iostream>
> #include <cstdlib>
> using namespace std;
>
> typedef struct test{
> public:
> int* a;
> test();
> };
>
> test::test(){
> a = new(int);
> *a = 5;
> cout << "In constructor: " << *a << endl;
> }
>
> int main(){
> test *x = new test();
> cout << "*(x->a): " << *(x->a) << endl;
> cout << "*((*x).a): " << *((*x).a) << endl;
> system("pause");
> }
>
> David McKen wrote:
>
> > Ok, I need some clarification on a couple topics
> invloving pointers
> > and pointers.
> >
> > 1) when a struct is created. All that can be assumed is
> that it
> > contains trash right? What is a portable way to
> initalize everything
> > to 0? mset?
> >
> > 2) when a structure contains a pointer. Access to the
> address (not the
> > value) comes in the form
> {struct_variable_name}.{member_name}?
> >
> > 3) how do you de-refrence the pointer in #2 to get the
> value of the
> > variable? {struct_variable_name}.*{member_name}?
> >
> > 4) if you have a pointer to the structure you access
> the members via
> > '->'. If a member of the structure is a pointer the
> address is gotten
> > via {struct_varible_name}->{member_name}? If so how
> does one get the
> > value of the varible?
> {struct_variable_name}->*{member_name}?
> >
> > Thanks in Advance
> > David McKen
> >
> > P.S. Was begining to run out of hair!
> >
>
------------------------------------------------------------------------
> > Do you Yahoo!?
> > Free online calendar
> >
>
<http://us.rd.yahoo.com/mail_us/tag/*http://calendar.yahoo.com>
> with
> > sync to Outlook(TM).
>
>
>
__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com
|
|
From: Ioannis V. <iv...@em...> - 2003-06-12 07:56:51
|
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of David McKen
Sent: Thursday, June 12, 2003 3:16 AM
To: dev...@li...
Subject: [Dev-C++] Stress with structs and pointers.
Ok, I need some clarification on a couple topics invloving pointers and
pointers.
1) when a struct is created. All that can be assumed is that it contains
trash right? What is a portable way to initalize everything to 0? mset?
struct whatever_type s={0, 0, 0 ...};
2) when a structure contains a pointer. Access to the address (not the
value) comes in the form {struct_variable_name}.{member_name}?
Yes.
3) how do you de-refrence the pointer in #2 to get the value of the
variable? {struct_variable_name}.*{member_name}?
*struct_variable_name.pointer_name=...
or
*(struct_variable_name.pointer_name)=...
to make it easier to understand. I use the second.
4) if you have a pointer to the structure you access the members via '->'.
If a member of the structure is a pointer the address is gotten via
{struct_varible_name}->{member_name}?
Yes
If so how does one get the value of the varible?
{struct_variable_name}->*{member_name}?
*struct_variable_name->pointer_name=...
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
<http://www.noicys.freeurl.com/>
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net <http://www.noicys.cjb.net/>
|
|
From: David M. <ci...@ya...> - 2003-06-12 15:05:14
|
Ok. Now to apply this stuff and hope I don't crash this MUD
again :P. BTW I apoligize if I came across as a newbie who
did not RTM. All I really needed was #4. The first 3 were
to make sure I had my prior facts straight.
--- Ioannis Vranos <iv...@em...> wrote:
>
>
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On
> Behalf Of David McKen
> Sent: Thursday, June 12, 2003 3:16 AM
> To: dev...@li...
> Subject: [Dev-C++] Stress with structs and pointers.
>
>
> Ok, I need some clarification on a couple topics
> invloving pointers and
> pointers.
>
> 1) when a struct is created. All that can be assumed is
> that it contains
> trash right? What is a portable way to initalize
> everything to 0? mset?
>
>
> struct whatever_type s={0, 0, 0 ...};
>
>
> 2) when a structure contains a pointer. Access to the
> address (not the
> value) comes in the form
> {struct_variable_name}.{member_name}?
>
>
> Yes.
>
>
> 3) how do you de-refrence the pointer in #2 to get the
> value of the
> variable? {struct_variable_name}.*{member_name}?
>
>
> *struct_variable_name.pointer_name=...
>
> or
>
> *(struct_variable_name.pointer_name)=...
>
>
> to make it easier to understand. I use the second.
>
>
>
> 4) if you have a pointer to the structure you access the
> members via '->'.
> If a member of the structure is a pointer the address is
> gotten via
> {struct_varible_name}->{member_name}?
>
>
> Yes
>
>
> If so how does one get the value of the varible?
> {struct_variable_name}->*{member_name}?
>
>
>
> *struct_variable_name->pointer_name=...
>
>
>
> Ioannis Vranos
>
> * Programming pages: http://www.noicys.freeurl.com
> <http://www.noicys.freeurl.com/>
> * Alternative URL 1: http://run.to/noicys
> * Alternative URL 2: http://www.noicys.cjb.net
> <http://www.noicys.cjb.net/>
>
>
>
=====
Signed
David Mcken
Life Sucks
Live with it
__________________________________
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com
|
|
From: <or...@vp...> - 2003-06-12 10:16:18
|
David McKen wrote:
> Ok, I need some clarification on a couple topics invloving pointers and
> pointers.
>
> 1) when a struct is created. All that can be assumed is that it contains
> trash right? What is a portable way to initalize everything to 0? mset?
That is the simplest, if you have a LARGE structure.
#include <mem.h> /* or string.h, I'm not sure */
struct mystruct {
int a,b,c;
char d,e,f;
long g,h,i,j,k,l,m,n;
/* ... */
};
int main()
{
struct mystruct ms;
memset(&ms,0,sizeof(struct mystruct));
return 0;
}
The above code is pure C. In C++, you don't need the "struct" word, when
using mystruct. So, in C++, the first 2 line in main():
mystruct ms;
memset(&ms,0,sizeof(mystruct));
memset() is also portable, and it will always work, even if you alter
the definition of your structure (add new elements). This way, you don't
have to use a LONG list to set everything to 0.
--
Greetings,
Balázs
|
|
From: Per W. <pw...@ia...> - 2003-06-12 16:57:33
|
memset() works very well in C programs. However, there are some
things to remember about memset() on structures.
If the code is later moved to C++, and the struct gets virtual methods,
the memset() code will be deadly, since it will clear the address of the
virtual method table (VMT), which will make calls to the methods kill the
program. Also, the VMT is normally used for run-time type checking, such
as dynamic cast.
One more thing about memset() of structures is that in C++, the struct may
contain members that are themself classes with a constructor. The work
done by these constructors will be overwritten.
Memset of structures that are part of a multiple-inherritance aggregate is
also dangerous, since it kills the hidden fields the compiler added to
safely locate the addresses of the different base classes.
Note that most of the time, it is enough to just write:
struct mystruct s =3D {0};
and then the compiler will assign zero to all members of the structure.
This kind of initialization is directly portable to c++ without any
dangerous side effects.
/Per W
On Thu, 12 Jun 2003, OROSZI Bal=E1zs wrote:
> David McKen wrote:
> > Ok, I need some clarification on a couple topics invloving pointers and
> > pointers.
> >
> > 1) when a struct is created. All that can be assumed is that it contain=
s
> > trash right? What is a portable way to initalize everything to 0? mset?
>
> That is the simplest, if you have a LARGE structure.
>
> #include <mem.h> /* or string.h, I'm not sure */
>
> struct mystruct {
> int a,b,c;
> char d,e,f;
> long g,h,i,j,k,l,m,n;
> /* ... */
> };
>
> int main()
> {
> struct mystruct ms;
> memset(&ms,0,sizeof(struct mystruct));
> return 0;
> }
>
>
> The above code is pure C. In C++, you don't need the "struct" word, when
> using mystruct. So, in C++, the first 2 line in main():
>
> mystruct ms;
> memset(&ms,0,sizeof(mystruct));
>
> memset() is also portable, and it will always work, even if you alter
> the definition of your structure (add new elements). This way, you don't
> have to use a LONG list to set everything to 0.
>
> --
> Greetings,
> Bal=E1zs
>
>
>
> -------------------------------------------------------
> This SF.NET email is sponsored by: eBay
> Great deals on office technology -- on eBay now! Click here:
> http://adfarm.mediaplex.com/ad/ck/711-11697-6916-5
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: <or...@vp...> - 2003-06-12 19:16:13
|
Per Westermark wrote:
> memset() works very well in C programs. However, there are some
> things to remember about memset() on structures.
>
> If the code is later moved to C++, and the struct gets virtual methods,
> the memset() code will be deadly, since it will clear the address of the
> virtual method table (VMT), which will make calls to the methods kill the
> program. Also, the VMT is normally used for run-time type checking, such
> as dynamic cast.
>
> One more thing about memset() of structures is that in C++, the struct may
> contain members that are themself classes with a constructor. The work
> done by these constructors will be overwritten.
>
> Memset of structures that are part of a multiple-inherritance aggregate is
> also dangerous, since it kills the hidden fields the compiler added to
> safely locate the addresses of the different base classes.
Thanks for this info, I didn't know about this :)
Indeed is useful.
But, as a last note, if you (the original asker) use PURE C only, then
you can safely use memset().
But if you are in the C++ world, you should rarely use the standard C
library anyway (where memset() is for example).
> Note that most of the time, it is enough to just write:
>
> struct mystruct s = {0};
>
> and then the compiler will assign zero to all members of the structure.
> This kind of initialization is directly portable to c++ without any
> dangerous side effects.
Are you sure, this works 100% in just C? I don't know, that's why I ask.
What happens, when I have a struct with 5 elements, and I write:
struct mystruct s = {0,0,0}; /* 3 only */
How many will be initialised?
Thanks in advance.
--
Greetings,
Balázs
|
|
From: Per W. <pw...@ia...> - 2003-06-12 19:47:28
|
When the declaration of a struct variable contains an "=3D { xx }"
assignment. the compiler will automatically add zero-values for the fields
that hasn't been specifically given a value. If { xx } contains values for
the three first fields, but the struct has five fields, the compiler will
assume zero for the remaining two fields.
A consequence of this is that if an existing program is modified, and new
member fields are added to an existing structure, you should either add
the new fields last, or specifically check all initialization of
corresponding struct variables, to make sure that a { member1 =3D 10,
member2 =3D 0, member3 =3D 12, ... } initialization doesn't suddenly starts=
to
assign to the wrong member fields.
Don't forget that the compiler is required to clear all global,
non-initialized, variables to zero on startup. A global variable declared
as "struct mystruct s;" will set all member fields of s to zero in a C
program. In C++, the program might override this behaviour by supplying a
constructor for the struct/class type.
/Per W
On Thu, 12 Jun 2003, OROSZI Bal=E1zs wrote:
> Per Westermark wrote:
> > memset() works very well in C programs. However, there are some
> > things to remember about memset() on structures.
> >
> > If the code is later moved to C++, and the struct gets virtual methods,
> > the memset() code will be deadly, since it will clear the address of th=
e
> > virtual method table (VMT), which will make calls to the methods kill t=
he
> > program. Also, the VMT is normally used for run-time type checking, suc=
h
> > as dynamic cast.
> >
> > One more thing about memset() of structures is that in C++, the struct =
may
> > contain members that are themself classes with a constructor. The work
> > done by these constructors will be overwritten.
> >
> > Memset of structures that are part of a multiple-inherritance aggregate=
is
> > also dangerous, since it kills the hidden fields the compiler added to
> > safely locate the addresses of the different base classes.
>
> Thanks for this info, I didn't know about this :)
> Indeed is useful.
>
> But, as a last note, if you (the original asker) use PURE C only, then
> you can safely use memset().
> But if you are in the C++ world, you should rarely use the standard C
> library anyway (where memset() is for example).
>
> > Note that most of the time, it is enough to just write:
> >
> > struct mystruct s =3D {0};
> >
> > and then the compiler will assign zero to all members of the structure.
> > This kind of initialization is directly portable to c++ without any
> > dangerous side effects.
>
> Are you sure, this works 100% in just C? I don't know, that's why I ask.
> What happens, when I have a struct with 5 elements, and I write:
>
> struct mystruct s =3D {0,0,0}; /* 3 only */
>
> How many will be initialised?
> Thanks in advance.
>
> --
> Greetings,
> Bal=E1zs
>
>
>
> -------------------------------------------------------
> This SF.NET email is sponsored by: eBay
> Great deals on office technology -- on eBay now! Click here:
> http://adfarm.mediaplex.com/ad/ck/711-11697-6916-5
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Abhijit S. <mu...@gm...> - 2003-06-12 17:27:01
|
Greets, David. > 1) when a struct is created. All that can be assumed is that it contains trash right? What is a portable way to initalize everything to 0? mset? You could use constructors, or external functions, called something fancy like `foo_zero_all()' to zero out members of your struct. The human touch - The Safest Thing(tm). ________________________________________ Abhijit Shylanath http://mudeth.tripod.com/ |