+ 2
Is there any possibility to point an int poniter to different data type like char, float,... etc...??
8 Answers
+ 2
Thank you all for ur answers...
+ 2
Ketan Lalcheta take a look at the comment on line number 28 in the code below
https://code.sololearn.com/c4ZPYbG1DrD1/?ref=app
+ 2
Based on the courses in your profile, I suppose you are asking about doing this in C.
Besides casting, you could use a union to recharacterize a pointer to different pointer types. That way pointer arithmetic can work correctly according to data type.
+ 2
Perfect Anthony Maina .... Got the purpose of allocator..... Yes , it should be just allocating and object constructor is seperate thing before / during we push into vector... Thanks for this useful information.. a side note can we say that reinterprete_cast is the only option in your example of allocator or anything else is also possible ? I am just checking for curiosity đ... And yes many thanks for this info as till now I was aware that reinterpret_cast is just there and no specific use case.
+ 1
Kesani pavan kumar Yes it's possible to for one to do that and at times it can be quite useful e.g when you want to treat a given region of memory as just a sequence of bytes.
For example:
int a;
char* b=reinterpret_cast<char*>(&a);
However this is expert territory since most errors that arise from improper use of this type of casting falls under the category of undefined behaviours.
+ 1
Ketan Lalcheta Yes new int would also work.I chose char since it's equal to a byte in most systems.
Why not just use new T[N]? Well,
1.The allocate function in an allocator is only meant to acquire(reserve) memory.Construction of objects on that memory can occur later.There's no need of wasting time on constructing new objects on a region of memory that might not be used.An example of this:vector's reserve() function.Also,this is what happens when string and vector objects acquire "extra" memory when the number of elements to be stored exceeds the structure's capacity.
2.Some types are not default constructible and as a result,there's no way new T[N]; would work for such a type.
0
Anthony Maina would it be possible to give some example of reinterpret_cast where it is extremely must....? I have not seen it being used except some MCQ on online questionnaire.... I always have been curious to know use of same... Way back on Sololearn app in my earlier days also I got answer that language has just provided the provision to typecast
0
~ swim ~ thanks a lot.... I will check again on serialisation later for sure....
Anthony Maina nice one as it refreshed my allocator.... I got your point that number of bytes required for memory allocation is perfectly asked by passing to argument of global object... But why ::new char (Would even work with ::new int and asking byte total /4 as well int has 4 size on 32 bit system ? To choose char was due to its 1 size on all os bits ?) over default ::new T type? To avoid constructor being called for T? May be I am still not getting as What the use of preventing Constructor call (and suppose my T is allocating resource so is it good to omit constructor call by using reinterpret_cast ) ?