mercredi 7 décembre 2022

How to Build Open DNP3 Library?

I need to Build the Open DNP3 Lib but the problem coz there is a lot of .cpp and .h files in the library and I don't know how the add the lib to a Console C++ Project and build it

Iam a beginner in C++ and any thing will help me ; so fell free to leave any comment if you don't know the exact answer

Why is my Java program running faster than my C++ program , both doing the same thing [duplicate]

I wrote a program in both C++ and Java to print "Hello World" 100,000 times, but I noticed that the C++ code takes too long compared to the Java code; The Java code takes about 6 seconds averagely and the C++ code takes about 18 seconds averagely, both run from the command line; Can someone please explain why, thanks.

The name of the program is first.java and first.cpp for Java and C++ respectively I used: java first.java; and first.exe; both from the command line

g++ --version g++ (Rev6, Built by MSYS2 project) 11.2.0

java --version java 13.0.2, 2020-01-14

Java Code

class first {
    public static void main(String... args) {
        long start = System.currentTimeMillis();

        for (int i = 0; i < 100000; i++) {
            System.out.println("Hello World");
        }

        long end = System.currentTimeMillis();

        long dur = end - start;
        System.out.println(dur / 1000);
    }

}

C++ Code

#include <iostream>
#include <string>
#include <chrono>

using namespace std;

int main()
{
    auto start = std::chrono::system_clock::now();
    for (int i = 0; i < 100000; i++)
    {
        cout << "Hello World" << endl;
    }
    auto end = std::chrono::system_clock::now();

    std::chrono::duration<double> elapsed_seconds = end - start;
    cout << elapsed_seconds.count() << endl;
}

mardi 6 décembre 2022

warning: friend declaration 'void traverse(List

i have a Node template class in one "Node.cpp" and included in file "List.cpp" and List template class i have to create a traverse function which is friend to the List class to traverse all the node in a List but facing an error here is my error proner

List Class Decleration

template<typename T>
class List{
    private:
        Node<T> *headNode;
        Node<T> *currentNode;
        Node<T> *lastCurrentNode;
        int size;
    public:
        List();
        void add(T);
        T get();
        bool next();
        friend void traverse(List<T>); // **there is an issue**
};

Friend function Intialization

template <typename T>
void traverse(List <T>list)
{
    Node<T> *savedCurrentNode = new Node<T>();
    list.currentNode = list.headNode;

    for(int i=1; list.next(); i++)
    {
        cout << "\n Element " << i << " " << list.get();
    }
    list.currentNode = savedCurrentNode;
}

i am a bigner and try to implement using templates but stuck into a problem now i have no idea how to solve this

Pointer metadata for sized, aligned storage

I am writing an allocator (for simplicity, let's assume nothing fails or throws):

struct AllocationHeader
{
  std::size_t size;
  std::size_t align;
};

void *aligned_allocate(std::size_t requested_size, std::size_t alignment)
{
  constexpr auto header_size = sizeof(AllocationHeader);
  const auto     total_size  = header_size + requested_size + alignment;
  auto           alloced_ptr = ::new unsigned char[total_size];

  ::new (alloced_ptr) AllocationHeader{requested_size, alignment};
  
  auto ret_ptr        = static_cast<void *>(alloced_ptr + header_size);
  auto remaining_size = total_size - header_size;
  return std::align(alignment, requested_size, ret_ptr, remaining_size);
}

When I deallocate I want to recover the planted metadata

void aligned_deallocate(void *ptr)
{
  constexpr auto header_size  = sizeof(AllocationHeader);
  // surely this is wrong
  auto           header_start = reinterpret_cast<unsigned char *>(ptr) - header_size;
  auto           header       = reinterpret_cast<AllocationHeader *>(header_start);
  
  do_something_with(header->size, header->align);
  // rest of deallocation...
}

But surely the conversion back to AllocationHeader is wrong. The original align could in theory be anything (assuming power of 2) including > std::max_align_t.

So how do I portably recover how much std::align() shifted the pointer by? I'm limited to C++11.

EDIT:

I suppose I can do the shifting first:

void *aligned_allocate(std::size_t requested_size, std::size_t alignment)
{
  constexpr auto header_size    = sizeof(AllocationHeader);
  const auto     total_size     = header_size + requested_size + alignment;
  auto           alloced_ptr    = ::new unsigned char[total_size];
  auto           ret_ptr        = static_cast<void *>(alloced_ptr + header_size);
  auto           remaining_size = total_size - header_size;
  
  std::align(alignment, requested_size, ret_ptr, remaining_size);
  ::new (reinterpret_cast<unsigned char *>(ret_ptr) - header_size) AllocationHeader{requested_size, alignment};
  return ret_ptr;
}

But then AllocationHeader may constructed misaligned...

socketio c++ : how to you pass ssl certificate

I am trying the socketIO C++ client ; but i dont know how to use certificates . The SocketIO server needs valid certificate to answer Has someone managed to do it ? There is no documentation at all ... Thanks

lundi 5 décembre 2022

If you have a dynamic array within a dynamic array, and you delete the outside dynamic array. Will it delete the inner dynamic array?

Just a general question but I was wondering if you delete the outer dynamic arrays data, will the inner dynamic arrays be deleted as well?

I did do it in a personal project, but I'm just unsure if it deleted the inner dynamic array.

Why am I getting error: expected class-name before ‘{’ token [closed]

I have a really simple file, and I keep getting an error on it: IRequestFrame.h:20:65: error: expected class-name before ‘{’ token And I've been trying to find the problem all day, but I can;t find it. I keep checking but it looks to me that I did everything right... But computer says no.

This is the file (without the namespaces):

#ifndef SRC_POWERSOFT_KSERIES_MESSAGE_IREQUESTFRAME_H_
#define SRC_POWERSOFT_KSERIES_MESSAGE_IREQUESTFRAME_H_

#include "IFrame.h"
#include "../../serde/ISerializable.h"
#include "../packet/IRequestPacket.h"

namespace amp {
namespace kseries {
namespace message {
namespace frame {

class IRequestFrame: public IFrame, public amp::kseries::serde::ISerializable {
                                                                           // ^ position of error
public:
    virtual std::size_t serializeTo(char* const buffer, std::size_t buff_size) = 0;
    virtual packet::IRequestPacket* getPacket() const = 0;
    virtual ~IRequestFrame();
};

} // frame
} // message
} /* namespace kseries */
} /* namespace amp */

#endif /* SRC_POWERSOFT_KSERIES_MESSAGE_IREQUESTFRAME_H_ */

These are the includes:

IFrame.h

#ifndef SRC_POWERSOFT_KSERIES_MESSAGE_IFRAME_H_
#define SRC_POWERSOFT_KSERIES_MESSAGE_IFRAME_H_
#include "../packet/IPacket.h"

namespace amp {
namespace kseries {
namespace message {
namespace frame {

class IFrame {
public:
    virtual unsigned int getAddress() const = 0;
    virtual ~IFrame();
};

} // frame
} // message
} /* namespace kseries */
} /* namespace amp */

#endif /* SRC_POWERSOFT_KSERIES_MESSAGE_IFRAME_H_ */

ISerializable .h

#ifndef SRC_POWERSOFT_KSERIES_SERDE_ISERIALIZABLE_H_
#define SRC_POWERSOFT_KSERIES_SERDE_ISERIALIZABLE_H_
#include <cstddef>
#include <vector>

namespace amp {
namespace serde {

class ISerializable {
protected:
    static std::vector<char> intToChars(int input, int base, int size);
public:
    virtual std::size_t serializeTo(char* const buffer, std::size_t buff_size) = 0;
    virtual ~ISerializable();
};

} /* namespace serde */
} /* namespace amp */

#endif /* SRC_POWERSOFT_KSERIES_SERDE_ISERIALIZABLE_H_ */

IRequestPacket.h (This one is more for completeness, but I guess it isn't part of the problem

#ifndef SRC_POWERSOFT_KSERIES_MESSAGE_PACKET_IREQUESTPACKET_H_
#define SRC_POWERSOFT_KSERIES_MESSAGE_PACKET_IREQUESTPACKET_H_

#include "IPacket.h"
#include "../payload/IRequestPayload.h"
#include "../../serde/ISerializable.h"

namespace amp {
namespace kseries {
namespace message {
namespace packet {

class IRequestPacket: public IPacket, public serde::ISerializable {
public:
    virtual payload::IRequestPayload* getPayload() = 0;
    virtual ~IRequestPacket();
};

} /* namespace packet */
} /* namespace message */
} /* namespace kseries */
} /* namespace amp */

#endif /* SRC_POWERSOFT_KSERIES_MESSAGE_PACKET_IREQUESTPACKET_H_ */