Jump to content

  • Log In with Google      Sign In   
  • Create Account

TheComet

Member Since 02 Oct 2013
Offline Last Active Aug 26 2016 03:39 PM

#5251310 Convincing AntiVirsus, im not a virus

Posted by on 09 September 2015 - 04:41 AM

Have you tried using the C library?

#include <stdio.h>

int main(int argc, char* argv[])
{
    FILE* fp;
    if(!(fp = fopen("test.txt", "w")))
    {
        fprintf(stderr, "oh oh (failed to open file)");
        return -1;
    }

    fprintf(fp, "hello world!");
    fclose(fp);

    return 0;
}

It's a silly suggestion. As you've been saying over and over, it works fine with VS8 but not with VS10. Seeing as I know nothing of VS I can only suggest to try disabling/enabling options until it starts working.




#5250983 C++11 lambda, modifying copied value

Posted by on 07 September 2015 - 07:49 AM

It makes sense if you want to do something like:

int counter = 10;
std::function<void()> fooFunction = [=]() {
    while(counter--) {
        // do important things
    }
}



#5250559 Best comment ever

Posted by on 04 September 2015 - 06:22 AM


IMO if you can't properly read a simple function pointer declaration like that you have no business writing C or C++.

This.

 

The comment doesn't contribute in any way to what can already clearly be extracted from reading the code. It's just as dumb as

int a = 6; // set a to 6

A useful comment would have been to explain what VFunc is and how it is used.




#5250129 C++ cross platform resolve hostname to ip library

Posted by on 01 September 2015 - 09:53 AM

As SeanMiddleditch pointed out, getaddrinfo() is probably your best bet. The following is a great guide on socket programming. I used it extensively as a reference when I was learning about sockets.

http://beej.us/guide/bgnet/output/print/bgnet_A4.pdf

 

If you scroll down to page 20 (16) (Chapter 5.1. getaddrinfo()) there's a complete example which does exactly what you asked for.

 

You may have to adjust a few things here and there to get it working on Windows (e.g. call WSAStartup() and WSACleanup() and include the right headers), but by and large it works the same way on Windows.

 


anyone who considers compiling on MingW to be "ported to Windows" should be thrown out of a window

Can you elaborate some more on why you take this stance?

 

I've come to the conclusion that writing cross platform software is a lot easier for everyone if you drop support for the MSVC compiler. It's non-compliant in many areas and is a pain to configure properly. As such it only causes headaches and additional overhead when writing your build scripts.

 

I find it disappointing that porting an entire POSIX compliant environment and toolchain to Windows is easier to do than supporting the MSVC compiler, but that's how it is.

 

EDIT:

Just got the code from the PDF file I linked to work using MSVC:

#include <stdio.h>
#include <string.h>
#include <WinSock2.h>
#include <WS2tcpip.h>  // INET6_ADDRSTRLEN

static WSADATA g_wsa_data;

char net_init(void)
{
    return (WSAStartup(MAKEWORD(2, 2), &g_wsa_data) == NO_ERROR);
}

void net_deinit(void)
{
    WSACleanup();
}

void net_dns_resolve(const char* hostname)
{
    struct addrinfo hints, *results, *item;
    int status;
    char ipstr[INET6_ADDRSTRLEN];

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;  /* AF_INET6 to force version */
    hints.ai_socktype = SOCK_STREAM;

    if((status = getaddrinfo(hostname, NULL, &hints, &results)) != 0)
    {
        fprintf(stderr, "failed to resolve hostname \"%s\": %s", hostname, gai_strerror(status));
        return;
    }

    printf("IP addresses for %s:\n\n", hostname);

    for(item = results; item != NULL; item = item->ai_next)
    {
        void* addr;
        char* ipver;

        /* get pointer to the address itself */
        /* different fields in IPv4 and IPv6 */
        if(item->ai_family == AF_INET)  /* address is IPv4 */
        {
            struct sockaddr_in* ipv4 = (struct sockaddr_in*)item->ai_addr;
            addr = &(ipv4->sin_addr);
            ipver = "IPv4";
        }
        else  /* address is IPv6 */
        {
            struct sockaddr_in6* ipv6 = (struct sockaddr_in6*)item->ai_addr;
            addr = &(ipv6->sin6_addr);
            ipver = "IPv6";
        }

        /* convert IP to a string and print it */
        inet_ntop(item->ai_family, addr, ipstr, sizeof ipstr);
        printf("  %s: %s\n", ipver, ipstr);
    }

    freeaddrinfo(results);
}

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        fprintf(stderr, "usage: %s <hostname>\n", argv[0]);
        return -1;
    }

    net_init();
    net_dns_resolve(argv[1]);
    net_deinit();

    return 0;
}

Don't forget to link Ws2_32.lib under Configuration Properties->Linker->Input.

 

Here's the code ported to Linux:

#include <stdio.h>
#include <string.h>
#include <sys/socket.h> 
#include <netdb.h>




char net_init(void)
{
    return 1; /* nothing to do */
}


void net_deinit(void)
{
    /* nothing to do */
}


void net_dns_resolve(const char* hostname)
{
    struct addrinfo hints, *results, *item;
    int status;
    char ipstr[INET6_ADDRSTRLEN];


    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;  /* AF_INET6 to force version */
    hints.ai_socktype = SOCK_STREAM;


    if((status = getaddrinfo(hostname, NULL, &hints, &results)) != 0)
    {
        fprintf(stderr, "failed to resolve hostname \"%s\": %s", hostname, gai_strerror(status));
        return;
    }


    printf("IP addresses for %s:\n\n", hostname);


    for(item = results; item != NULL; item = item->ai_next)
    {
        void* addr;
        char* ipver;


        /* get pointer to the address itself */
        /* different fields in IPv4 and IPv6 */
        if(item->ai_family == AF_INET)  /* address is IPv4 */
        {
            struct sockaddr_in* ipv4 = (struct sockaddr_in*)item->ai_addr;
            addr = &(ipv4->sin_addr);
            ipver = "IPv4";
        }
        else  /* address is IPv6 */
        {
            struct sockaddr_in6* ipv6 = (struct sockaddr_in6*)item->ai_addr;
            addr = &(ipv6->sin6_addr);
            ipver = "IPv6";
        }


        /* convert IP to a string and print it */
        inet_ntop(item->ai_family, addr, ipstr, sizeof ipstr);
        printf("  %s: %s\n", ipver, ipstr);
    }


    freeaddrinfo(results);
}


int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        fprintf(stderr, "usage: %s <hostname>\n", argv[0]);
        return -1;
    }


    net_init();
    net_dns_resolve(argv[1]);
    net_deinit();


    return 0;
}



#5248981 Outputting to a file

Posted by on 26 August 2015 - 07:40 AM

Reminds me of the time my antivirus decided to delete all DLL files in my python site-packages while I was debugging. "Unable to call function: Function was deleted from your computer"




#5245705 Worst Hotfixes

Posted by on 11 August 2015 - 05:26 AM

In this thread we share stories of some of the worst hotfixes we've seen and/or applied.

 

In a game I had to present I was experiencing an extremely obscure bug where after some time pointers would randomly point to garbage values, crashing the game. I had an hour to get it working before the presentation.

 

I spent 45 minutes trying to reproduce it with no success. It happened at seemingly random times, but for some reason it was always the same two pointers that were modified.

 

Seeing as I was running out of time, I ended up inserting checks which would replace the garbage value (when it occurred) with the correct value again - the correct value I knew because I saw it in the debugger and it seemed to remain consistent.

if(game->settings_doc != 0x63e1b0)
    game->settings_doc = 0x63e1b0; /* from debugger */

After the presentation I sat down with valgrind and found the problem. A buffer overrun was writing into memory it wasn't supposed to.




#5245674 Can someome please tell me why this crashes?

Posted by on 11 August 2015 - 02:01 AM

If you happen to have a cygwin shell installed (e.g. if you installed git) you could also just use:

find /c/Users/admin/Documents -name "*.mp4" -exec basename {} \; > video_filenames.txt

Maybe someone can translate that to the Windows cmd equivalent.




#5245518 Breaking out of a nested loop

Posted by on 10 August 2015 - 11:53 AM

Why is my thread now in coding horrors biggrin.png What have you people done to my beautiful thread?




#5244395 Constructor Initializer List and Heap Allocation

Posted by on 03 August 2015 - 04:30 PM

Also note that if the allocation fails and you have more than one heap allocation in the initializer list (and said allocation throws std::bad_alloc or you throw an exception in the constructor body), you've got yourself a nice memory leak, because the destructor will never get called.

class Test
{
    Model* model1;
    Model* model2;
public:
    Test() :
        model1(new Model),
        model2(new Model)  // this allocation fails
    {}

    ~Test()  // this will never get called
    {
        if(model1) delete model1;
        if(model2) delete model2;
    }
};

You're on the safe side if you use smart pointers.

class Test
{
    std::unique_ptr<Model> model1;
    std::unique_ptr<Model> model2;
public:
    Test() :
        model1(new Model),
        model2(new Model)
    {}

    ~Test()
    {}
};



#5241733 How is cross platform done

Posted by on 21 July 2015 - 11:20 AM

That's about as hard to answer as "how do people cook."

Most of it boils down to [...]

I see what you did there




#5238807 Multiple heritage on python

Posted by on 07 July 2015 - 10:21 AM

Still, it does not answer my question if super() is correct and how it works.

 

super() finds the next method in the MRO at each step. The proper way to call the base class __init__ methods is for every class to call super(), like this:
 
 

class A(object):
 def __init__(self):
  super(A, self).__init__()
  print('A init')


class B(object):
 def __init__(self):
  super(B, self).__init__()
  print('B init')


class C(A, B):
 def __init__(self):
  super(C, self).__init__()
  print('C init')


if __name__ == '__main__':
 c = C()

 
Perhaps this piece of code can shed some light:
 

class A(object):
 def foo(self):
  print('foo from A')


class B(object):
 def foo(self):
  print('foo from B')


class C(A, B):
 def foo(self):
  print('foo from C')

If we print out the MRO (method resolution order) of class C, we see the following order:
 

>>> C.__mro__
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)

That's [C, A, B, object].

 

If we instantiate class C and call foo(), we get the expected output "foo from C":

>>> instance = C()
>>> instance.foo()
foo from C

super() can be used to call foo() from a base class. By specifying C as the class type, super() will return the next object in line in instance's MRO.

>>> instance.__class__.__mro__
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
>>> super(C, instance).foo()
foo from A

Because instance is of type C and C was specified in super(), A is the next in line in [C, A, B, object].
 
By specifying A instead of C in super(), the next object after A in line in instance's MRO will be returned:

>>> super(A, instance).foo()
foo from B

Because instance is of type C and A was specified in super(), B is the next in line in [C, A, B, object].

 

By specifying B to super(), the next in line is object, which of course doesn't have the method foo():

>>> super(B, instance).foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'super' object has no attribute 'foo'



#5238677 Multiple heritage on python

Posted by on 06 July 2015 - 04:23 PM

This answers your question: http://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance




#5237707 Unit setup

Posted by on 30 June 2015 - 11:55 AM

It's arbitrary. 1 meter can be 1 unit, but 1 meter could also be 45.7 units. It depends on how everything is scaled relative to everything else.




#5237335 Memory management patterns in C++

Posted by on 28 June 2015 - 03:50 PM

Have you tried to make a garbage collector or used reference counting?

 

Have you tried it?

 

"There will be a lot of bugs": Well yeah, but you can say that about anything. The arguments you're using have no sustenance. They are just opinions based on nothing.




#5232923 Copy file/folder cross-platform

Posted by on 05 June 2015 - 04:37 AM

You could do something as simple as:

 

#include <iostream>
#include <string>
#include <stdlib.h>


int copy_directory(std::string a, std::string b)
{
#if defined(_WIN32)
    return system(std::string("copy " + a + " " + b).c_str());
#elif defined(__APPLE__) || defined(__linux__)
    return system(std::string("cp -r " + a + " " + b).c_str());
#else
#   error copy_directory does not support this OS!
#endif
}   


int main()
{
    if(copy_directory("a", "b"))
        std::cout << "something went wrong" << std::endl;
    return 0;
}

 

If you want more control and don't want to use any external libs then you'll have to write multiple implementations using the filesystem API for each platform you want to support. In my project, I placed each implementation in a separate source file and had one common header file. Depending on the platform, the correct source file is compiled.






PARTNERS