Re: creating a pointer to a imbeded struct causes C2065



If you are doing it with a socket connection, you are reading BYTEs, not chars.

Your protocol would be to read the header bytes (which may take more than one Receive; see
my essay on multithreaded sockets to see the code required), and then read that many
elements, or that many bytes (depending on what this count means), possibly extending the
size of the buffer, or preallocating a buffer of the right size, which is easy, so the
whole "fixed buffer size 100" problem goes away, then read the bytes of the message,w hich
can take many Receive operations, and when the entire message has been received,
processing it.

See how I do this in my example code.

I still say you have taken a fundamentally simple problem and tried to make it
gratuitously difficult.
joe

On Wed, 28 Nov 2007 18:59:00 -0800, amccombs <amccombs@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:



"Giovanni Dicanio" wrote:


"amccombs" <amccombs@xxxxxxxxxxxxxxxxxxxxxxxxx> ha scritto nel messaggio
news:1904E75B-987A-46C4-86E5-BF7770C5173C@xxxxxxxxxxxxxxxx

char szBuffer[100];

typedef struct T_Smoo
{
struct
{
int a;
}H;
struct
{
int b;
int c;
int d;
}D;
} Smoo;

Your code seems to me C code (not C++), in fact you are using the syntax:

typedef struct ...

In C++, you can have just struct, without typedef.
I would suggest you to use C++ instead of C for application development.
You may also program "in C style", but if you use a .cpp extension for your
source codes, the C++ compiler (not the C compiler) will be used, and I do
think that you could have benefits, also using just "C++ as a better C".
For example, you can use "struct" without the more verbose "typedef".

However, I would write code a bit differently, e.g.

struct HBlock
{
int a;
};

struct DBlock
{
int b;
int c;
int d;
};

struct Smoo
{
HBlock H;
DBlock D;
};

Smoo *pSmoo = (Smoo*) szBuffer;

I fail to understand that... could you please clarify your goals?
Why are you type-casting from a char array to a Smoo custom class?

If you want to create a new Smoo instance, you may do:

Smoo * pSmoo = new Smoo();

You should release somewhere the object, using:

delete pSmoo;
pSmoo = NULL; // avoid dangling referneces

Frankly speaking, I would not use raw pointers, but I would prefer using a
smart pointer, like boost::shared_ptr (with it, basically, you don't need to
spend time thinking about delete).

pSmoo->H.a = 1;
pSmoo->D.b = 2;
pSmoo->D.c = 3;
pSmoo->D.d = 4;

OK

int offset = sizeof(pSmoo->H);
Smoo::D *p = (&Smoo::D) &szBuffer[offset];

If you want p to be a pointer to DBlock, and point to the "D" member in
Smoo, you may do like so:

DBlock * p = &(pSmoo->D);

p->D.b = 5;
p->D.c = 6;
p->D.d = 7;

No, it's: p->b = ...; p->c = ....; p->d = ...;

Giovanni



Smoo *pSmoo = (Smoo*) szBuffer;
why? because szBuffer will be sent through a socket connection. This is how
I know how to populate the buffer.

I am going to have 1 or more DBlocks, so I want a pointer that I can move to
the next offset in the szBuffer so that I can populate it.
****
And what's wrong with my solution for this? You are confusing "advancing a pointer" (an
implementation concept in C) with "advancing to the next element" (an abstraction). The
solution I gave that puts an array at the end of the struct works just fine because it is
a very clean implementation of the abstraction, and doesn't require kludgy substructures,
weird pointer manipulations, or other unnecessarily complex actions that are coupled to
pointer manipulation.
joe
..
****

Allan
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • Re: Another spinoza challenge
    ... You should test against the int type's limits: ... typedef struct complex ... a pointer to an integer ... A macro is preferable because it is replaced by inline code, ...
    (comp.lang.c)
  • Re: Another spinoza challenge
    ... int main{ ... using struct and typedef. ... a pointer to an integer ... A macro is preferable because it is replaced by inline code, ...
    (comp.lang.c)
  • Re: [RFC][PATCH 1/6] memcg: fix pre_destory handler
    ... returns struct cgroup of id. ... SwapCgroup uses array of "pointer" to record the owner of swaps. ... struct cgroup_id is freed by RCU. ... changed interface from pointer to "int" ...
    (Linux-Kernel)
  • gcc, aliasing rules and unions
    ... struct B {int x, y;}; ... A struct pointer can be converted to another ... Also I don't know what the "effective type" of a union member is. ...
    (comp.lang.c)

Loading