+
Yes this seems good and he handles even LittleEndian and BigEndian issues thus making it possible to deserialize data that has been serialized on a system supporting different endianness. This buffer doesnt support edits and writes in between btw. Again if it is efficiency that you seek then memory mapping may be your best option.
so the steps I should follow is first create a shared memory with some extra size (like file_size*2 should be enough) read the actual file in this new created space then start to reading from start of shared memory and make needed changes (insert new char if needed) btw with this way I need to store the offset of where I currently are for later reads, right? and also I can r/w struct from shared memory just like how we do it in ifstream and ofstream? and at the end save data from start of shared memory to where the data are right?
Anonymous
so the steps I should follow is first create a shared memory with some extra size (like file_size*2 should be enough) read the actual file in this new created space then start to reading from start of shared memory and make needed changes (insert new char if needed) btw with this way I need to store the offset of where I currently are for later reads, right? and also I can r/w struct from shared memory just like how we do it in ifstream and ofstream? and at the end save data from start of shared memory to where the data are right?
You should read the portion of the file that you require, make any changes and then read the next part of the file at the next memory location ... make changes and so on. If on the other hand, if you need to read the whole file to determine what changes you have to make, then you have to also ensure that inserts in between must move the data beyond to create space for the inserts. So it depends on your use case. With ifstream and ofstream, this part is handled for you by the OS and hence it is much easier for you to code it but can be inefficient when you have multiple buffer flushes because writes to disk are slower than changes in memory. Memory is just a sequence of bytes. You have to know what is stored at a particular memory location before you attempt to read it.
Manav
Which is the best book for dsa
[BLĀNK]
Which is the best book for dsa
I would also like to know :)
[BLĀNK]
Just practice any algorithm that you see dude
Anonymous
Ok
Anonymous
Hello guys. Considering the follow simple snippet: node** pointer = (node**)malloc(sizeof(node*)); // Do stuff with pointer free(pointer); I was wondering why valgrind calls "invalid free" on free(pointer)
Prakhar
Given a weighted undirected graph, you have to find the shortest distance between source and destination given you can remove k edges from the path.
updull
Thanks admin❤️
DEV 7
#include<iostream> #include<string> using namespace std; int main(){ string st; int count=0; cout<<"enter the name"<<endl; cin>>st; for (int i = 1; i <=st.length(); i++) { if ((st.at(i)='a')||(st.at(i)='e')||(st.at(i)='i')||(st.at(i)='o')||(st.at(i)='u')) { count=count++; } } cout<<"no of vowels"<<count; return 0; }
DEV 7
program to calculate all vowels in string, but it did not work
Anonymous
Hello guys. Considering the follow simple snippet: node** pointer = (node**)malloc(sizeof(node*)); // Do stuff with pointer free(pointer); I was wondering why valgrind calls "invalid free" on free(pointer)
Valgrind is not always accurate. If it says there is a memory leak, you cant be sure there is one. You just do code analysis manually and if you are sure there is none, you just ignore it. It is not possible for tools like valgrind to determine non lexical scopes and hence there are a lot of false positives erring on the side of caution
ꍏꈤꀸ
Hello guys. Considering the follow simple snippet: node** pointer = (node**)malloc(sizeof(node*)); // Do stuff with pointer free(pointer); I was wondering why valgrind calls "invalid free" on free(pointer)
i think you should firstly clear data inside node, and then node itself. simply, try free(*pointer) free(pointer) btw, if you can avoid using malloc and free, would be better ( use new and delete instead)
Anonymous
Is the function st.at(i) the same as st[i]?
No. st.at[i] throws an exception when there is an invalid index while st[i] just leads to Undedined Behavior
Anonymous
No. st.at[i] throws an exception when there is an invalid index while st[i] just leads to Undedined Behavior
So, its better to use at than square brckets to prevent the program from output that we don't want it, right?
Anonymous
Exception handling can be expensive. So it is better to check your indexes before accessing the index rather than using at and let it throw an exception. You would use at only in those functions where in your preconditions it is a requirement that the index passed in to the function be valid. You would declare such a function noexcept and use at. In such cases when the user passes in an invalid index, it is a precondition violation and thus all bets are off. So an exception out of this noexcept function would terminate the program which is acceptable Behavior.
Anonymous
the problem is elsewhere, this is fine
Any tip to find out where?
Stanislav
https://en.cppreference.com/w/cpp/string/byte/strtoul
Six
Hey i'm trying to understand why this function has two different outputs when the condition is met. add(); makes a random number 1-3. result(); is outputing "Works" when random number is 3 and "No work" when 1 or 2. I have put the result(); function inside of add(); where it outputs correctly when 3, 2 and 1. But the result(); inside of my main(); function always "no work" even when its 3. Attached a pastebin of the code. https://pastebin.com/Qmvm1kHn
Anonymous
Any tip to find out where?
🤷 look at the code
DEV 7
best resource for template and stl in c++
Anonymous
best resource for template and stl in c++
C++ Templates: The Complete Guide by Nicolai Josuttis et al. (2nd edition covers upto C++17) for C++20 changes (most important imo is Concepts) look at Rainer Grimm's blog.
Anonymous
as for the library, all good C++ books cover them. look at cppreference for a complete reference.
Anonymous
C++ Templates: The Complete Guide by Nicolai Josuttis et al. (2nd edition covers upto C++17) for C++20 changes (most important imo is Concepts) look at Rainer Grimm's blog.
be sure to check out "What You Should Know Before Reading This Book" in the book to make sure you can actually read the book because it is pretty advanced
Deleted Account
makefile how to read .depend file?
Anonymous
🤷 look at the code
Is there any way to keep an eye on an allocated block of memory?
Anshul
accessing elements in a deque by offsetting a pointer to another element causes undefined behavior.
Anshul
what is the meaning of this sentence
Anonymous
accessing elements in a deque by offsetting a pointer to another element causes undefined behavior.
This is because deque is not implemented using contiguous memory. It is typically implemented as a linked list of vectors (but this varies). So suppose say you are at the boundary of one of these vectors. Then offsetting a pointer using pointer arithmetic is not guaranteed to point to the previous or the next element. That is why it is Undefined Behavior.
Anshul
How deque is implemented internally in stl if it doesn't have it's elements in contiguous memory! the cplusplus refrence site says they're implemented using dynamic arrays but then why the memory is no contiguous
Anshul
"Therefore, they provide a functionality similar to vectors, but with efficient insertion and deletion of elements also at the beginning of the sequence, and not only at its end. But, unlike vectors, deques are not guaranteed to store all its elements in contiguous storage locations: accessing elements in a deque by offsetting a pointer to another element causes undefined behavior."
Anonymous
okay! so then how the operator[ ] is overloaded
Internally deque maintains a pointer to its internal vectors. So when you ask for an element at a specific index, it knows which vector it will be in. It then fetches the element from that vector. Btw this is just one of the possible ways of implementing a deque. There are others too.
Anonymous
And when I use the term vector, I actually mean a contiguous storage. It could be a dynamic array or a STL vector
Anshul
from where i can learn how deques are implemented internally??
Anonymous
from where i can learn how deques are implemented internally??
By looking at libcxx code repository for Clang or libcpp code repository for GCC or some similar ones for Microsoft
Anshul
i'm not able to understand anything by looking at that code.
Anshul
#ifndef _GLIBCXX_DEQUE #define _GLIBCXX_DEQUE 1 #pragma GCC system_header #include <bits/stl_algobase.h> #include <bits/allocator.h> #include <bits/stl_construct.h> #include <bits/stl_uninitialized.h> #include <bits/stl_deque.h> #include <bits/range_access.h> #include <bits/deque.tcc> #ifdef _GLIBCXX_DEBUG # include <debug/deque> #endif #ifdef _GLIBCXX_PROFILE # include <profile/deque> #endif #endif /* _GLIBCXX_DEQUE */
Anonymous
i'm not able to understand anything by looking at that code.
Well hard luck then. Read more about advanced programming and then look at the code again
Anshul
is there no simple understandable implementation available anywhere? actually i couldn't find anything in the DSA course i'm enrolled in
Anonymous
is there no simple understandable implementation available anywhere? actually i couldn't find anything in the DSA course i'm enrolled in
Usually they dont cover deques in DSA courses. Queues need them. But DSA courses teach queues using a circular array implementation. Could be because it is much more easier for someone who is just starting out with DSA. Perhaps time for them to upgrade their courses.
Anshul
You're right!
Anonymous
Is there any way to keep an eye on an allocated block of memory?
https://clang.llvm.org/docs/AddressSanitizer.html
Kishore
What are the topics should I need to cover in Dsa part? Could anyone tell me?
Anonymous
What are the topics should I need to cover in Dsa part? Could anyone tell me?
Open a good DSA book and skip ahead to the Table of Contents. That should be it.
Anonymous
this has some explanations in the comments too https://github.com/electronicarts/EASTL/blob/master/include/EASTL/deque.h
Hanz
#ASK Simple question, what does for (;;) means? Is that different from while (1)?
Hanz
they mean the exact same
but why someone choose the first one over the former?
Ludovic 'Archivist'
but why someone choose the first one over the former?
You can pick whichever you want, they are the exact same. It comes to personnal preference/coding style here
Hanz
Thanks for your answer 🙏
bunny
hello guys i need small help. Like i want to set a range of a variable so that after incrementing its value from the last it should jump back to the first value.
bunny
just like 12 for month
bunny
if we increment from 12 it should jump back again to 1
bunny
/report
Anonymous️
Sry
Harsh
if we increment from 12 it should jump back again to 1
Before you increment the variable just check if it's less than the max value it can have if it's already say 12 then instead of incrementing it change it to 1
Hanz
that's it!
Anonymous
// member initialization #include <iostream> using namespace std; class Circle { double radius; public: Circle(double r) : radius(r) { } double area() {return radius*radius*3.14159265;} }; class Cylinder { Circle base; double height; public: Cylinder(double r, double h) : base (r), height(h) {} double volume() {return base.area() * height;} }; int main () { Cylinder foo (10,20); cout << "foo's volume: " << foo.volume() << '\n'; return 0; }