I do not understand why the pointer addition is failing.

char *pipebuf=new char[40001];
	
	CommandRobot *cr= (CommandRobot*)pipebuf;
	cr->command=2;
	DWORD *rooms=(DWORD*)(cr+1);
								
	*rooms=100;

For some reason after executing the value of pipebuf only contains the value of command, it does not contain the value of 100;

Yet, when I remove the new keyword to make it not a dynamic pointer, it works fine...Why?

DWORD is an unsigned int

Command is a class with a DWORD variable of command.. something like this

Class Command {
public:
DWORD command;
};

Recommended Answers

All 11 Replies

Why are we casting the pipebuf into a class??

It makes it easier to read/understand the code...It might be bad style, but it shouldn't be the problem I don't think...

For some reason after executing the value of pipebuf only contains the value of command, it does not contain the value of 100

You're adding 1 to cr, which changes the Command object to which the pointer points. What you probably intended is DWORD *rooms=(DWORD*)cr; .

It might be bad style, but it shouldn't be the problem I don't think...

That's debatable.

You're adding 1 to cr, which changes the Command object to which the pointer points. What you probably intended is DWORD *rooms=(DWORD*)cr; .


That's debatable.

What I intend to do is change the char array pipebuf, so that it contains the values of 2 AND 100. By incrementing it by one, the value of 100 won't overwrite the value of 2..that is they will both in the array. If pipebuf were an array of unsigned ints/DWORDS then you could say i want pipebuf[0]=2, and pipebuf[1]=100. If i did DWORD *rooms=(DWORD*)cr; and set what rooms is pointing to to 100 wouldn't that just overwrite the 2? I want both in the array.

I'll also try it without the class Command and just use DWORD and see if that fixes the problem

Same problem even with casting pipebuf into DWORD.

What is your intent with these two lines?

char *pipebuf=new char[40001];
CommandRobot *cr= (CommandRobot*)pipebuf;

What is your intent with these two lines?

char *pipebuf=new char[40001];
CommandRobot *cr= (CommandRobot*)pipebuf;

Oh crap, I meant for the class Command shown in the first post to be CommandRobot...Maybe thats why there seems to be misunderstanding...The class Command in the first post should be CommandRobot.

I basically want to modify the first four bytes of pipebuf using the pointer cr.

Dude, wtf. If you're punning an array of char into an array of DWORD, you don't need an intermediate class:

char *pipebuf=new char[40001];
DWORD *rooms=(DWORD*)pipebuf;

rooms[0] = 2;
rooms[1] = 100;

But keep in mind that this stores the DWORD value of 2 and 100 into the array, so if you try to reach those two values with pipebuf[0] and pipebuf[1], you're not going to get 2 and 100.

Are you trying to create an array of commands for the Robot to execute?

Dude, wtf. If you're punning an array of char into an array of DWORD, you don't need an intermediate class:

char *pipebuf=new char[40001];
DWORD *rooms=(DWORD*)pipebuf;

rooms[0] = 2;
rooms[1] = 100;

But keep in mind that this stores the DWORD value of 2 and 100 into the array, so if you try to reach those two values with pipebuf[0] and pipebuf[1], you're not going to get 2 and 100.

Well, the intermediate will be necessary later, but for debugging purposes i tried out what you said.
Maybe something is wrong with my visual studios but the problem still persists. Whats most frustrating is that changing it to either char *pipebuf[100]; OR char pipebuf[100] fixes the issue. Whats even stranger is that during debugging mode with char *pipebuf=new char[100], I can only see the "2" in pipebuf when i hover over it, but when I print out pipebuf, it prints the correct changed array. For char *pipebuf[100]; OR char pipebuf[100] hovering over it shows the 100 as well =/

Are you trying to create an array of commands for the Robot to execute?

Sort of...the first 4 bytes of the array is the command to "move". Then the rest of the array are the room numbers to move to...in 4 byte chunks...

Post a complete test program, along with your expectations for output. I tested my code on Visual Studio with no issues. So either I don't understand what your expecting to happen, or your implementation of the suggested solution is incorrect.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.