954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Finding base address of struct

Hi all,
How to find the base address of a structure ,just by knowing the name of the structure and a address of one of its members.

Thanks in advance

marirs07
Light Poster
36 posts since Nov 2009
Reputation Points: 7
Solved Threads: 0
 

This gives the address of an instance of the structure named one, of type breakfast. I just need to know the name of it - one.

#include <iostream>


struct breakfast
{ int eggs;
  int beans;
};


int main()
{

  breakfast one;
  std::cout << &one;
  return 0;
}
Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
 

The instance name is not known!!!! Only the name of the struct(breakfasr) and address of one of its members say &one.eggs is known.Using these two data the base address needs to be found out!!!

marirs07
Light Poster
36 posts since Nov 2009
Reputation Points: 7
Solved Threads: 0
 

you basically can't 'find out the address' of something that does not exist, you need an instance of the given class/struct to take out it's address
when you say, for example

struct MyStruct
{
    int someVarName;
};

you don't declare anything(you just Define), thus you can't take it's address unless you do what Moschops showed you.

dospy
Junior Poster in Training
95 posts since Jun 2009
Reputation Points: 65
Solved Threads: 6
 

If you know nothing else about the structure, it could have a million members or it could have one. Even if you knew the members, you would have to guess based on the compiler and the system architecture and what struct packing options were used and the order the members were declared in the source code.

Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
 

This is the xact problem given to me:

Struct{

  Int a,

 Int b,

 char c,

} Check;

 

Void Func(int *ptr)

{

 /* How will you get the base address of the structure*/

 You know only the name of the structure

}

 

 

Main()

{

 Check test;

Test.b = 0x01;

Func(&Test.b)

}


QUESTION:

Passing an element of a structure from a function to another function how will you get the base address of the structure itself.

You Know:

1) Only the name of the structure

marirs07
Light Poster
36 posts since Nov 2009
Reputation Points: 7
Solved Threads: 0
 

If it will always be &test.b passed in, you can make a struct of your own inside the function, calculate the difference between the address of your new struct's base address and its b member, and then apply that difference to the value passed in.

Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
 

No,any address of members can be passed in.We dont know that the address is of b.

Thanks for ur reps!!!

marirs07
Light Poster
36 posts since Nov 2009
Reputation Points: 7
Solved Threads: 0
 

Can any1 find a soluution to this problem!!!!

marirs07
Light Poster
36 posts since Nov 2009
Reputation Points: 7
Solved Threads: 0
 

that's not the name of the structure, it's an instance of and unnamed structure
basically when you declare a structure without naming it, you are not allowed to create any instances of it, thus the only way to create and instance is by putting names(separated by commas) after the struct declaration AND before the semicolon.
so,

struct{
int a; // note semicolon after declaration, not comma
int b;
char c;
} Check; // note here; it could be Check, Check2, Check3 etc before the semicolon


taking the address is rather simple from now; ex:

#include <iostream>

struct{
int a; // note semicolon after declaration, not comma
int b;
char c;
} Check;

int main( )
{
    std::cout << "Address of the unnamed structure instance named 'Check': " << &Check;
    return 0;
}


so, if you need to pass the address to the function, just use '&Check';

dospy
Junior Poster in Training
95 posts since Jun 2009
Reputation Points: 65
Solved Threads: 6
 

that's not the name of the structure, it's an instance of and unnamed structure basically when you declare a structure without naming it, you are not allowed to create any instances of it, thus the only way to create and instance is by putting names(separated by commas) after the struct declaration AND before the semicolon. so,

struct{
int a; // note semicolon after declaration, not comma
int b;
char c;
} Check; // note here; it could be Check, Check2, Check3 etc before the semicolon

taking the address is rather simple from now; ex:

#include <iostream>

struct{
int a; // note semicolon after declaration, not comma
int b;
char c;
} Check;

int main( )
{
    std::cout << "Address of the unnamed structure instance named 'Check': " << &Check;
    return 0;
}

so, if you need to pass the address to the function, just use '&Check';


I just got here and I'm thinking the same thing. I'm glad someone finally noticed.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

yea, the problem is i doubt that's what they wanted, but we can't really help much unless they give us really clean explanation of it

EDIT: now i figured out what they'r problem is, they want to find out the address of a structure by knowing the address of one of it's members
for ex we have

struct
{
    int _a; // let's assume that this int's address is 0x02F48C
} Check;

// how do we find out the address of Check without using '&Check'?


i've heard of something like structures have an unusual way to store variables in memory(one right after another, just like an array), but i never really knew it for sure and i haven't even found a tutorial on this(although i'd like to find out about this thing).

sry guys, i can't help you here...
wait for more experienced programmers to answer your questions since it beats me

dospy
Junior Poster in Training
95 posts since Jun 2009
Reputation Points: 65
Solved Threads: 6
 

Ya ur rite!!! i know the address of one of its members and a structure name say for ex.

struct cook
{
int brad;
char egg;
}eat;
i will pass the address &eat.egg to a function , knowing this address and the structure name i.e cook ,i need to find the base address of the structure in that function

Waiting for d reps.
Thanks in advance

marirs07
Light Poster
36 posts since Nov 2009
Reputation Points: 7
Solved Threads: 0
 

There is no solution with just functions. You need a macro, very similar in fact to offsetof .

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

do we have nay such macro like offsetof . In offsetof we give the structure name and the member name,am asking for a macro which gets the structure name and the address of a member.or atlest is it possible to write a code for that.

:)

marirs07
Light Poster
36 posts since Nov 2009
Reputation Points: 7
Solved Threads: 0
 

You don't need anything more than the offsetof macro in

#include <cstddef>
#include <iostream>

struct A
{
    int a ;
    int b ;
    char c ;
    // ...
};

void foo( int* p ) // address of A::b
{
    A* pa = reinterpret_cast<A*>( reinterpret_cast<char*>(p) - offsetof(A,b) ) ;
    std::cout << "foo::pa: " << pa << '\n' ;
}

int main()
{
    A aa ;
    std::cout << "&main::aa: " << &aa << '\n' ;
    foo( &aa.b) ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

If you know the name of the *member* of the structure and you know the type of the structure, you can use offsetof(type,member) and do some pointer arithmetic.

It is impossible to get the base of the structure in C (or C++) without knowing what member it is and what type it is.

You must be mistaken that that is the "exact" problem given to you. The structure is unnamed, and you don't know what member *ptr is pointing to.

Please double check that you have posted the exact problem. The answer to the current assignment given is, "it's impossible".

MAYBE you can cheat and use decltype(Check) to get the type of the unnamed type (never tried to do something silly like that, not sure it works). Then if you miraculously knew that it was member 'b' in Func, then you could use:

void Func(void *p)
{
    typedef decltype(Check) StructType;
    size_t memberofs = offsetof(StructType,b);
    void *base = (void*)((char*)p - memberofs);
    cout << "The base address is " << base;
}


If you're cheating that much you might as well just say &Check is the base address.

doug65536
Light Poster
35 posts since Sep 2011
Reputation Points: 28
Solved Threads: 3
 

:)

The thing is that we dont know that the address p is address of b,then how can we put the member name b in offsetof() i.e inside the function i know only the name of the structure (A ,in this case) and address *p.

marirs07
Light Poster
36 posts since Nov 2009
Reputation Points: 7
Solved Threads: 0
 

> The thing is that we dont know that the address p is address of b

Then there is no way to reliably determine the address of the struct object.

vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

Exactly, which is why I said "if you miraculously knew that it was member 'b' in Func"...

The problem given is impossible because:
1) The struct is unnamed
2) We can't assume that it's the Check instance of the structure. If we could then we can ignore *p and just say &Check is the base!
3) From a raw address it is impossible to determine a struct offset without knowing what member it is.
4) Without an offset, calculating the base is impossible.

Double check that you have posted the actual problem correctly.

doug65536
Light Poster
35 posts since Sep 2011
Reputation Points: 28
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
 
View similar articles that have also been tagged: