Click here to Skip to main content
15,898,134 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Thread Pin
CPallini12-Aug-09 2:38
mveCPallini12-Aug-09 2:38 
GeneralRe: Thread Pin
Rajesh R Subramanian12-Aug-09 2:45
professionalRajesh R Subramanian12-Aug-09 2:45 
GeneralRe: Thread Pin
CPallini12-Aug-09 3:28
mveCPallini12-Aug-09 3:28 
QuestionCWebBrowser - OnWindowClosingBrowser Method.. Pin
Member 383463011-Aug-09 23:44
Member 383463011-Aug-09 23:44 
AnswerRe: CWebBrowser - OnWindowClosingBrowser Method.. Pin
Bacon Ultimate Cheeseburger12-Aug-09 20:10
Bacon Ultimate Cheeseburger12-Aug-09 20:10 
GeneralRe: CWebBrowser - OnWindowClosingBrowser Method.. Pin
Member 383463012-Aug-09 22:29
Member 383463012-Aug-09 22:29 
GeneralRe: CWebBrowser - OnWindowClosingBrowser Method.. Pin
Bacon Ultimate Cheeseburger12-Aug-09 23:03
Bacon Ultimate Cheeseburger12-Aug-09 23:03 
QuestionQuestion on strict-aliasing Pin
Hing11-Aug-09 23:40
Hing11-Aug-09 23:40 
Hi experts,

I meet a question about strict-aliasing problem when compiling a code using gcc. The problem is described below:

#include <stdio.h>
#include <stdlib.h>

unsigned long queue[10];
int queue_len = 0;

int main()
{
	unsigned long a = 0x00010002;
	unsigned short* b = (unsigned long*)(void*)&a; // we need a "(void*)" here, because we want to skip the strict-aliasing rule check.

	b[0] = 6;
	b[1] = 5;

	printf("hello world %x", (int)a); // 6 5
	return 0;
}


When you compile the code using the option below:

gcc -O3 -Wall <code file>


The result of "a" would not be changed.

However, when you compile the code using the option below:

gcc -O3 -Wall -fno-strict-aliasing <code file>


The result of "a" is changed.

This behaviour can be explained in the aliasing rule of GCC standard. The intention of strict-aliasing rule is to prevent the case that a pointee is being pointed to by a pointer of different type (the so-called type-punning error). In the case above, the pointer type of "&a" is (long*) while it is being accessed by pointer of type (short*), the action that being taken in (short*) will not take effect to the original pointee. This is a kind of trap introduced in GCC.

So, up to now, we can still explain the behavior, however, for the case below:

#include <stdio.h>
#include <stdlib.h>

typedef struct _MyStruct
{
	unsigned short a;
	unsigned short b;
}MyStruct;

typedef struct _MyStruct2
{
	unsigned char a;
	unsigned char b;
	unsigned char c;
	unsigned char d;
}MyStruct2;

int main()
{
	MyStruct a = {0x0001, 0x0002};
	MyStruct2* b = (MyStruct2*)(void*)&a;// = malloc(sizeof(unsigned short*));

	b->a = 0;
	b->b = 5;
	b->c = 0;
	b->d = 6;


	printf("hello world %x %x\r\n", (int)a.a, (int)a.b); // <-- 6 5
	return 0;
}


It should suffer from the same effect, however, the content of "a" is changed no matter you turned on the strict-aliasing rule or not.

Do anyone know what is happening in the compiler side?

You can find the open-std for C here:

http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1336.pdf[^]

Take a look to section 6.3.2.3, Point 7, it states that:

A pointer to an object or incomplete type may be converted to a pointer to a different
object or incomplete type. If the resulting pointer is not correctly aligned for the
pointed-to type, the behavior is undefined.

Thanks a lot.
AnswerRe: Question on strict-aliasing Pin
Stuart Dootson12-Aug-09 6:14
professionalStuart Dootson12-Aug-09 6:14 
GeneralRe: Question on strict-aliasing [modified] Pin
Hing12-Aug-09 16:02
Hing12-Aug-09 16:02 
QuestionExamining a Message Queue Pin
ForNow11-Aug-09 22:53
ForNow11-Aug-09 22:53 
AnswerRe: Examining a Message Queue Pin
Stuart Dootson11-Aug-09 23:26
professionalStuart Dootson11-Aug-09 23:26 
QuestionVirtual Serial Port Question Pin
Programm3r11-Aug-09 22:24
Programm3r11-Aug-09 22:24 
AnswerRe: Virtual Serial Port Question Pin
Cedric Moonen11-Aug-09 23:01
Cedric Moonen11-Aug-09 23:01 
QuestionRe: Virtual Serial Port Question Pin
Programm3r11-Aug-09 23:27
Programm3r11-Aug-09 23:27 
AnswerRe: Virtual Serial Port Question Pin
Randor 12-Aug-09 9:31
professional Randor 12-Aug-09 9:31 
GeneralRe: Virtual Serial Port Question Pin
Programm3r12-Aug-09 20:01
Programm3r12-Aug-09 20:01 
Questionhow to display the IplImage into Picture control box. Pin
DevelopmentNoob11-Aug-09 22:13
DevelopmentNoob11-Aug-09 22:13 
AnswerRe: how to display the IplImage into Picture control box. Pin
zhu_lin11-Aug-09 22:59
zhu_lin11-Aug-09 22:59 
QuestionMaximum option to find a string/substring ,or compare option. Pin
Le@rner11-Aug-09 21:01
Le@rner11-Aug-09 21:01 
AnswerRe: Maximum option to find a string/substring ,or compare option. [modified] Pin
Adam Roderick J11-Aug-09 22:45
Adam Roderick J11-Aug-09 22:45 
AnswerRe: Maximum option to find a string/substring ,or compare option. Pin
zhu_lin11-Aug-09 23:10
zhu_lin11-Aug-09 23:10 
Questionload mp3 and seek by samples Pin
javad_200511-Aug-09 20:08
javad_200511-Aug-09 20:08 
AnswerRe: load mp3 and seek by samples Pin
zhu_lin11-Aug-09 23:12
zhu_lin11-Aug-09 23:12 
GeneralRe: load mp3 and seek by samples Pin
javad_200512-Aug-09 1:37
javad_200512-Aug-09 1:37 

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.