« first day (1541 days earlier)      last day (1316 days later) » 

7:54 AM
as alignof (x) always equal with sizeof(x) ? where x is built in data type? Does this also rmain true if X is user defined data type ?
 
no, there's no reason to assume that alignof (x) == sizeof(x) holds true
it's especially not true for user defined types, since they're regularly larger than max align
 
 
5 hours later…
1:17 PM
can you give me an example where alignof(builtin_data_type) not equal with sizeof(the_same_builtin_data_type)?
 
nwp
I don't think there is one on current systems. The candidate is double with a size of 8 and an alignment of 4 on 32 bit systems, but it turns out that 8 byte aligned doubles are a tiny bit faster to read, so they made them 8 byte aligned. If you look through enough compilers and platforms you'll probably find one where it uses 4 byte alignment for doubles.
 
@CătălinaSîrbu on many 32bit platforms alignof(long long int) is 4 not 8
but many platforms also define vector types for specific targets
and those often are overaligned
 
ok... thanks
 
1:43 PM
if my data type has a size of 32 bytes and the alignment of 8 bytes what does it mean ?
I mean I was expecting an alignment of 32 :)) but as I can see from your answers it was not the case :)
from my data type the alignment is somehow the max alignment from the data types contained by my data type ?
for example here I have a double inside , and therefore the 8 byte alignemnt ?
or how does it work
 
@CătălinaSîrbu It means that the system has a natural alignment that is most useful to the processor of 8 bytes. 32 bytes would be massively wasteful
@CătălinaSîrbu Generally it aligns to the most aligned type in the struct or the max_align_t if the struct is larger than max_align_t
 
nwp
Gcc used to have alignof(double)==8 and alignof(struct { double d; }) == 4. Alignments are fancy.
Normally the compiler handles that for you and you don't need to care.
 
ok so in my case if alignof(my_type) equals 8 I am in the case where it is aligned to the most aligned type in my struct (which is double). But how can I come across to the case where the struct is aligned to max_align_t?
yes but I was talking some time ago about alignment with @Mgetz and told me about serializing data
and I believe in that case you need to know about alignment
 
nwp
Serialization means you turn your data into bytes. Bytes have an alignment of 1. When doing the stupid thing and just casting the byte array to whatever data type you want, alignment is one of the reasons why it doesn't work.
 
but if your data is made up by structures you have to specify the alignment of your bytes, right?
 
1:54 PM
@CătălinaSîrbu almost any object that is compound of multiple complex fields will have this
 
nwp
@CătălinaSîrbu Normally you don't need to specify that. The compiler already knows the alignment of all the data types.
 
yes it makes and it doesn't makes sense. I'm still searching for a little example that has meaning outside the entire process
 
nwp
It's only when you do weird things, like wanting an array of bytes to be aligned like a double that you can do shady casts with 1 less bug that you need to care about alignment.
 
@CătălinaSîrbu you're over worrying? 98% of the time it doesn't matter. In the 2% it does you should use a library that takes care of it for you. In the 0.1% beyond that... you're writing scientific or computational code.
 
yes because you usually don't find all the information you need online. Like ok, i find something but usually how to use is related to where to use. Sometimes you find how to use, sometimes you fine where to use , but almost never both :))
 
1:58 PM
@CătălinaSîrbu well you do, but not where you're thinking. It's all in protocol documentation because the writers assume you know the issues
or file format documentation
 
I don't think I understand
 
@CătălinaSîrbu so when you're defining a protocol you need to specify how things are passed for example
e.g. is it little endian or big endian?
what's the padding?
etc.
 
ok, I came across recently to protobufs :) sounds interesting
 
reading at the moment...
 
2:05 PM
The microsoft one is a bit complex... it's basically what it looks like when you take something like protobuf and build a file format
 
@Mgetz but here, how they align when a message is made up by 4 data types for example ?
 
@CătălinaSîrbu they force it? It's not intended to be fast to make up the buffer per se, it's meant to be fast to translate and easy to parse
 
so basically a protobuf is 1 byte aligned always? or what do you want to say :) ?
sorry if I misunderstand
 
@CătălinaSîrbu AFAIK yes
 
bc you dont want it to be fast you want it to be portable
and easy to use
 
2:13 PM
this is for efficiency reasons over the wire. But if you need something to be faster to parse between systems that you know the alignment of you might use specially crafted messages that are going to line up for that
correct
e.g. a PowerPC or ARM system should be able to parse a protobuf message a x86 machine crafted
 
2:49 PM
Is template <> needed to write template specialization ? Can't I just write the specialization without it ?
 
huh
 
@domocar1 also note that this isn't as simple as it sounds en.cppreference.com/w/cpp/language/overload_resolution
because templates
 
Pity. I thought there is a simple answer to this one.
 
@domocar1 there sort of is... if you specialize a namespaced method in a different namespace the specialization is taken first.
hence why you should specialize std::swap in your namespace and not in namespace std
but you specialize it as not a template
see the "until c++20" note in the middle
 
3:05 PM
hold on, I don't think we're on the same page
 
we are and we arent'
 
For example:
` template <typename T> void f(T x)`. Is there a difference between `template <> void f(int x)` and `void f(int x)` ?
 
@domocar1 yes... read the lookup link
the middle item will revert to the template
 
What do you mean by revert ?
I didn't expect this to be so damn complicated at all ..
 
@domocar1 See the bottom of this
you'll need to expand the section down there to see the explaination
 
3:11 PM
I think I found it
Thanks
 
Yeah... this crap is fun
hence the "just make a function called swap in your namespace that meets the requirements of swappable"
 
nwp
@domocar1 You cannot specialize function templates because reasons. Either use overloading or put the function in a struct and specialize the struct.
 
3:26 PM
Because reasons ? I don't get what you mean.
 
@domocar1 basically it massively screws up ADL, templates have priority for legacy reasons
 
Aha, I see now.
 
so it doesn't actually participate in ADL... it just bypasses it and takes hold. You get around that by doing a free function in the same namespace as the user (not the template)
because that bypasses ADL too
or at least does ADL in that namespace first
 
4:07 PM
Hi folks, is it always true that the address of the first data member of an object is the same as the address of the object itself?
Please kindly ping me if you respond my question above. Thank you!
 
@NotAZoomedImage nope, there's no such guarantee, e.g. there could be a vtable ptr before it
 
class Foo
{
public:
	Foo()
	{
		cout << "Foo: " << this << endl;
	}
};

class Goo
{
public:
	Foo foo;
	Goo()
	{
		cout << "Goo: " << this << endl;
	}
};


int main()
{
	Goo goo;
}
 
or inherited base classes
 
@PeterT: Thank you for you response. Could you give me a counter example?
 
@NotAZoomedImage gcc.godbolt.org/z/f5YT8d
 
4:18 PM
@PeterT: Thank you very much. Thinking....
 
Question regarding MFC: in the documentation of PostMessage, it says "If you send a message in the range below WM_USER to the asynchronous message functions [...], its message parameters cannot include pointers. Otherwise, the operation will fail. The functions will return before the receiving thread has had a chance to process the message and the sender will free the memory before it is used."
Do they actually mean "[...] its message parameters cannot include pointers to objects allocated and freed in the scope of the caller. [...]"?
I currently have some code that pass around a WM_SETICON message with a HICON stored in a long-lived object with SendMessage, and I'd rather use PostMessage instead.
 
 
1 hour later…
5:45 PM
@Vaillancourt no... the window message model assumes that all memory allocated on a thread is deallocated by that thread
 
6:15 PM
@Mgetz So how would I pass a string around?
I found this:
13
A: How to send a string via PostMessage?

MichaelYou can't pass the address of the string in PostMessage, since the string is probably thread-local on the stack. By the time the other thread picks it up, it could have been destroyed. Instead, you should create a new string or exception object via new and pass its address to the other thread (...

 
6:57 PM
@Vaillancourt this technically works... but holy crap you need to be careful because if something explodes you could lose a lot
 
@Mgetz What would explode? What would I lose? I guess this page is a good entry point to know about this?
 
@Vaillancourt consider exception safety, what happens if you're using raw new and something blows up?
 
A memory leak?
So a more appropriate pattern here would be to "find the MainFrameWnd, cast it to the expected type, store the string there, then post a message to the frame, telling it do something with the string that has just been stored"?
 
@Vaillancourt depends on if you had other things queued up, generally I allocate whatever I'm going to pass via std::make_unique and then have the smart pointer .release it directly into the method
but honestly if you need to pass that kind of data it might be better to use a different method
 
Something like this?
void
CallerClass::updateUiText(const char* newText)
{
  std::unique_ptr<std::string> str = std::make_unique<std::string>( newText );
  ::PostMessage( MyhWnd, CWM_UI_SHOULD_UPDATE, 0, reinterpret_cast<WPARAM>( str.release() ) );
}
L
 
7:13 PM
@Vaillancourt correct, then the other side would immediately recapture it in a unique_ptr
 
@Mgetz Yes, okay, I'm converting it back right to a unique_ptr on the other side 👍
Although on the "calling side" it's not a unique_ptr for now.
Is there another common pattern to pass data to MFC windows? (I found myself in need to update that code on our app, and I'm not familiar with the suggested patterns.)
 
@Vaillancourt just call the object directly and use locks?
you just have to be super careful
 
Hmm, right, ok, if it gets serious, I guess I'll have to do this. For now it appears that it is about only thing that is not used as just an event.
I'm trying to "decouple" the UI from the "3d simulation", so just calling the window directly is kind of the opposite of what I want to do, but I guess I could have a "single entry function" that would communicate with the window, and have the window function queue the messages, somehow.
I'll read more about this to reduce the chances of messing up.
Thanks!
 
7:34 PM
@Vaillancourt if you're passing lots of data it's probably better to set it up all at once on a thread and then move it off
 
@Mgetz yes, that's not the case here, at least for now!
 

« first day (1541 days earlier)      last day (1316 days later) »