Jump to content
  • Advertisement
Sign in to follow this  
GameCreator

Void Pointer

This topic is 756 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a simple test program like this which seems to work fine.

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

struct datastruct
{
    int packageID;
    int value;
} data;

void main()
{
    data.packageID=1;
    data.value=5;

    void *packet = malloc(sizeof(data));
    memcpy(packet, &data, sizeof(data));

    printf("%d\n", ((int *)packet)[0]);
    printf("%d\n\n", ((int *)packet)[1]);

    data.packageID=99999;
    data.value=99999;

    memcpy(&data, packet, sizeof(data));

    printf("%d\n", ((int *)packet)[0]);
    printf("%d\n", ((int *)packet)[1]);
}

But when I try to do similar in my other project, it compiles but crashes.  I send like so...

SteamNetworking()->SendP2PPacket(steamuser, &package1, sizeof(package1), k_EP2PSendReliable);

Then read like so.

    uint32 msgSize = 0;
    while(SteamNetworking()->IsP2PPacketAvailable(&msgSize))
    {
        printf("Packet available...\n");

        void *packet = malloc(msgSize);
        CSteamID steamIDRemote;
        uint32 bytesRead = 0;

        if(SteamNetworking()->ReadP2PPacket(&packet, msgSize, &bytesRead, &steamIDRemote))
        {
            printf("Message size: %d", msgSize);
            printf("Packet read with ID %d\n", ((int *)packet)[0]);   // Crashes on this line
            printf("and value %d\n", ((int *)packet)[1]);

The program prints out the correct message size but crashes when it gets to the next line.

 

I'm not sure what I'm doing wrong.  How would I read the two values?  (The eventual goal is to memcpy the packet read into the right struct, depending on the ID.)

 

 

 

Share this post


Link to post
Share on other sites
Advertisement

You probably want ReadP2PPacket(packet instead of ReadP2PPacket(&packet

 

The difference is writing to the memory you allocated versus the memory where the pointer is allocated.

Share this post


Link to post
Share on other sites

You were exactly right.  Thank you!  (I'm horrible with pointers and dereferencing and such.)

 

Funny enough, I fixed that and on one structure it worked but another it didn't.  Couldn't figure it out for a while, until I looked at how I defined the structs.  The IDs were alls... chars.  :blink:  It's a fluke one worked at all.  Switched them to ints and now it works as expected.  Pumped to get some coding done tomorrow.  Thanks again!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!