Click here to Skip to main content
16,004,927 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: how to create client and server winsock in one project or use 2 winsock in one project? Pin
Richard MacCutchan26-Sep-10 1:04
mveRichard MacCutchan26-Sep-10 1:04 
QuestionMemory aligned bit fields Pin
Trevor Johansen24-Sep-10 21:34
Trevor Johansen24-Sep-10 21:34 
AnswerRe: Memory aligned bit fields Pin
Luc Pattyn24-Sep-10 23:02
sitebuilderLuc Pattyn24-Sep-10 23:02 
AnswerRe: Memory aligned bit fields Pin
Alain Rist24-Sep-10 23:53
Alain Rist24-Sep-10 23:53 
AnswerRe: Memory aligned bit fields Pin
Aescleal25-Sep-10 0:56
Aescleal25-Sep-10 0:56 
AnswerRe: Memory aligned bit fields Pin
Trevor Johansen25-Sep-10 6:51
Trevor Johansen25-Sep-10 6:51 
AnswerRe: Memory aligned bit fields Pin
Aescleal25-Sep-10 7:26
Aescleal25-Sep-10 7:26 
GeneralRe: Memory aligned bit fields Pin
Trevor Johansen25-Sep-10 8:40
Trevor Johansen25-Sep-10 8:40 
Thanks for that breakdown, no wonder I was having an issue with the first example Im still pretty bad when it comes to points to pointers and I get lost in whats data and whats an address.

So to access an already initialized piece of memory I use Type *Type *ptr(reinterpret_cast( location )); Creating a double pointer with the first layer being accessible to you but your changes flowing to the bottom layer in the cast? Hopefully thats right.


I have whipped up some code with the union structure (I forgot a unions members occupy the same memory location, duh so that was easy) I have tested it by using the code below and I can use printf to dump the flags and reg and it works perfectly. The problem with this is that I need to initialize every flag manually however the hardware address I am pointing to is all zeros by default so is it safe to ignore this??
#include <stdio.h>

typedef struct
{
  unsigned RX0E:1;
  unsigned TX0E:1;
  unsigned RX1E:1;
  unsigned TX1E:1;
  unsigned RX2E:1;
  unsigned TX2E:1;
  unsigned EOFF:1;
  unsigned FNEF:1;
} COMM;

typedef union
{
  COMM flags;
  unsigned reg;
} COMM_FR;

int main()
{  
  // Init myflags ptr to hardware register
  COMM_FR myflags = new( (void *)(0x1400000) ) COMM_FR;

  // Access flags as a whole or by bit
  myflags.flags.RX0E = 1;
  myflags.reg = 0x2A;

  return 0;
}


However when I change things around so the flags are initilized via a union constructor and make the flags a pointer it compiles without error but when run i get exception 6392.
#include <stdio.h>

typedef struct
{
  unsigned RX0E:1;
  unsigned TX0E:1;
  unsigned RX1E:1;
  unsigned TX1E:1;
  unsigned RX2E:1;
  unsigned TX2E:1;
  unsigned EOFF:1;
  unsigned FNEF:1;
} COMM;

typedef union
{
  COMM *flags;
  unsigned reg;
  // Constructor to init flags to zero
  void COMM_FR()
  {
	flags->RX0E = 0;
	flags->RX1E = 0;
	flags->RX2E = 0;
	flags->TX0E = 0;
	flags->TX1E = 0;
	flags->TX2E = 0;
	flags->EOFF = 0;
	flags->FNEF = 0;
	reg = 0x00;
  };
} COMM_FR;

int main()
{  
  // Init myflags ptr to hardware register
  COMM_FR *myflags = new( (void *)(0x1400000) ) COMM_FR;

  // Access flags as a whole or by bit
  myflags->flags->RX0E = 1;
  myflags->reg = 0x2A;

  return 0;
}


I would prefer to use the second example as the rest of my handling is done via -> and with a compiler that does not support auto-complete having combinations of -> and . member access is infuriating.
AnswerRe: Memory aligned bit fields Pin
Aescleal25-Sep-10 10:07
Aescleal25-Sep-10 10:07 
GeneralRe: Memory aligned bit fields Pin
Trevor Johansen25-Sep-10 11:15
Trevor Johansen25-Sep-10 11:15 
AnswerRe: Memory aligned bit fields Pin
Luc Pattyn25-Sep-10 8:55
sitebuilderLuc Pattyn25-Sep-10 8:55 
GeneralRe: Memory aligned bit fields Pin
Trevor Johansen25-Sep-10 9:34
Trevor Johansen25-Sep-10 9:34 
GeneralRe: Memory aligned bit fields Pin
Luc Pattyn25-Sep-10 9:45
sitebuilderLuc Pattyn25-Sep-10 9:45 
GeneralRe: Memory aligned bit fields Pin
Trevor Johansen25-Sep-10 10:57
Trevor Johansen25-Sep-10 10:57 
AnswerRe: Memory aligned bit fields Pin
Luc Pattyn25-Sep-10 11:18
sitebuilderLuc Pattyn25-Sep-10 11:18 
GeneralRe: Memory aligned bit fields Pin
Trevor Johansen25-Sep-10 11:56
Trevor Johansen25-Sep-10 11:56 
GeneralRe: Memory aligned bit fields Pin
Luc Pattyn25-Sep-10 12:14
sitebuilderLuc Pattyn25-Sep-10 12:14 
GeneralRe: Memory aligned bit fields Pin
Alain Rist25-Sep-10 9:57
Alain Rist25-Sep-10 9:57 
GeneralRe: Memory aligned bit fields Pin
Aescleal25-Sep-10 10:19
Aescleal25-Sep-10 10:19 
AnswerRe: Memory aligned bit fields Pin
Alain Rist25-Sep-10 11:26
Alain Rist25-Sep-10 11:26 
GeneralRe: Memory aligned bit fields Pin
Aescleal25-Sep-10 22:31
Aescleal25-Sep-10 22:31 
GeneralRe: Memory aligned bit fields Pin
Trevor Johansen25-Sep-10 10:51
Trevor Johansen25-Sep-10 10:51 
GeneralRe: Memory aligned bit fields Pin
Aescleal25-Sep-10 22:34
Aescleal25-Sep-10 22:34 
QuestionDirectX - Antialiasing and Plotting Pixels Query [Moved] Pin
simon alec smith24-Sep-10 9:52
simon alec smith24-Sep-10 9:52 
QuestionHow to process multiple commands on CAccessor. Pin
Spawn@Melmac24-Sep-10 8:36
Spawn@Melmac24-Sep-10 8:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.