Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Store Address of object pointed to

Reply
Thread Tools

Store Address of object pointed to

 
 
nicollete
Guest
Posts: n/a
 
      11-10-2009
I am trying to store the address of the memory location pointed to by
a union pointer

say I have

union header* p ;
No p is pointing to some memory location during the course of the
program and I need to store that address in a table , so that would be
a different struct.

what should be the variable I must use ? If I try to use (char*)p will
the code break?

please explain with a short example.

Thanks!
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      11-10-2009
On 2009-11-10, nicollete <(E-Mail Removed)> wrote:
> union header* p ;
> No p is pointing to some memory location during the course of the
> program and I need to store that address in a table , so that would be
> a different struct.


Why not just store a "union header *"?

> what should be the variable I must use ? If I try to use (char*)p will
> the code break?


You can use a pointer converted to (char *) or (void *), but if you need
to store a pointer to a union, a pointer to that particular union is of
course the most obvious data format to use.

struct { int i, union header *p; } table[] = {
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
};
...
union_header *p;
...
table[0].p = p;

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
 
 
 
Tarique.
Guest
Posts: n/a
 
      11-11-2009
...snip..

> Why not just store a "union header *"?
>
> > what should be the variable I must use ? If I try to use (char*)p will
> > the code break?

>
> You can use a pointer converted to (char *) or (void *), but if you need
> to store a pointer to a union, a pointer to that particular union is of
> course the most obvious data format to use.
>
> * * * * struct { int i, union header *p; } table[] = {
> * * * * * * * * { 0, 0 },
> * * * * * * * * { 0, 0 },
> * * * * * * * * { 0, 0 },
> * * * * * * * * { 0, 0 },
> * * * * };
> * * * * ...
> * * * * union_header *p;
> * * * * ...
> * * * * table[0].p = p;
>


Ok! I've got another silly question. Sorry Just can't figure it out!

Ive got the union in file memroutines.c:

union header {
struct {
union header* next;
union header* prev;
size_t size;
int h4byte1;
int h4byte2;
int h4byte3;
int h4byte4;
}s;
double dummy_align_var;
};

and the following function definition in TagTables.c:

void add_tag(char* file_name_p,
unsigned int line_number_p,
union header* real_address_p, /*get the warning here*/
union header* returned_address_p,
size_t size_p );

When I compile with /W4 on MS VC++ 08 ,I get a warning
1>cathname\tagtable.h(19) : warning C4115: 'header' : named type
definition in parentheses

The TagTable.h header file has the folloing content :

#ifndef TAG_TABLE_H
#define TAG_TABLE_H

#ifndef _MAIN_H
#include <stdio.h>
#include <stdlib.h>
#endif

#ifndef MEM_ROUTINES_H
#include "MemRoutines.h"
#endif

/*Other Functions*/
void add_tag(char* file_name_p,
unsigned int line_number_p,
union header* real_address_p,
union header* returned_address_p,
size_t size_p );

#endif



Compiling with /W3 however produces no warning. I'm confused !!!
What's wrong ?
Please Help!

Thanks!
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      11-11-2009
On 2009-11-11, Tarique. <(E-Mail Removed)> wrote:
> Ive got the union in file memroutines.c:


Uh.

How is any other module supposed to find that?

Put type definitions in headers and include them everywhere.

So, this:

> union header {
> struct {
> union header* next;
> union header* prev;
> size_t size;
> int h4byte1;
> int h4byte2;
> int h4byte3;
> int h4byte4;
> }s;
> double dummy_align_var;
> };


would go in something like "header.h" (or see below), and then:

> and the following function definition in TagTables.c:


Somewhere before this function, you'd

#include "header.h"

> void add_tag(char* file_name_p,
> unsigned int line_number_p,
> union header* real_address_p, /*get the warning here*/
> union header* returned_address_p,
> size_t size_p );


.... and you'd do the same thing in the file where you currently define
the union, so it can find the same definition.

> When I compile with /W4 on MS VC++ 08 ,I get a warning
> 1>cathname\tagtable.h(19) : warning C4115: 'header' : named type
> definition in parentheses


Hmm.

> The TagTable.h header file has the folloing content :


Okay, this isn't going to affect you right now, but:

In general, you should not rely on the ability to change case at
will. If the file is named "tagtable.h" (which is what the compiler
is reporting), you should not refer to it as TagTable.h -- while some
systems let you do this, others might not.

> #ifndef MEM_ROUTINES_H
> #include "MemRoutines.h"
> #endif


Don't do the #ifndef here. Have MemRoutines.h do it, and just #include
it unconditionally when you think you need it.

Most likely, you just need to move the union header definition into
MemRoutines.h.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-11-2009
"Tarique." <(E-Mail Removed)> writes:
<snip>
> Ive got the union in file memroutines.c:
>
> union header {
> struct {
> union header* next;
> union header* prev;
> size_t size;
> int h4byte1;
> int h4byte2;
> int h4byte3;
> int h4byte4;
> }s;
> double dummy_align_var;
> };
>
> and the following function definition in TagTables.c:
>
> void add_tag(char* file_name_p,
> unsigned int line_number_p,
> union header* real_address_p, /*get the warning here*/
> union header* returned_address_p,
> size_t size_p );
>
> When I compile with /W4 on MS VC++ 08 ,I get a warning
> 1>cathname\tagtable.h(19) : warning C4115: 'header' : named type
> definition in parentheses


Seebs has given you a good run-down of the most common solution, but
there is another. You don't have to put all of the declaration of
'header' into TagTables.h. You can just add:

union header;

at file scope. This says to the compiler "header is a union tag --
I'll say more about when it matters". Because you pass pointers to
objects of this union type, you don't have to say anything else right
now. The full declaration of the union can stay in the .c file that
needs the details.

Of course, this does not work if any other parts of the program need
to know the details, but if you can do it, it is a good idea because
it keep implementation details hidden. As result, you don't have to
recompile the parts the use TagTables.h when/if the declaration of the
header changes.

<snip>
--
Ben.
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      11-11-2009
On 2009-11-11, Ben Bacarisse <(E-Mail Removed)> wrote:
> Seebs has given you a good run-down of the most common solution, but
> there is another. You don't have to put all of the declaration of
> 'header' into TagTables.h. You can just add:
>
> union header;
>
> at file scope. This says to the compiler "header is a union tag --
> I'll say more about when it matters". Because you pass pointers to
> objects of this union type, you don't have to say anything else right
> now. The full declaration of the union can stay in the .c file that
> needs the details.


This is true, of course -- you and Scott both thought of it, and I didn't
until later.

That said, I have a sneaking suspicion that the function taking those
pointers is likely to try to use them in some way, or need to know the
size of what they point to.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Tarique.
Guest
Posts: n/a
 
      11-11-2009
Ah that worked like a charm! Another important lesson learned
Thank You Seebs,Scott & Ben.

Appreciate all your comments.

Thanks!

( PS: I've snipped the entire message I'm replying to . Hope that's
Ok!)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Modify pointed object method behaviour Arkaitz Jimenez C++ 1 06-11-2009 08:41 AM
Removing object from arraylist when pointed to by iterator nooneinparticular314159@yahoo.com Java 12 08-25-2008 02:50 PM
.NET winform application. email function is pointed to the SMTP server of Linux. But it showns "Pickup" folder is not existed. Benny Ng ASP .Net 1 03-30-2006 05:10 PM
What Happens When Native Memory Space Pointed By java.nio.ByteBuffer Is Deleted res7cxbi@verizon.net Java 5 01-02-2006 09:32 PM
create an instance of a class that pointed to by interface of that class? Matt Java 10 04-07-2004 01:38 PM



Advertisments