Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8212597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:48:42+00:00 2026-06-07T10:48:42+00:00

I define a struct for the move typedef struct { MsgType msgType; int newFallenStonesSize;

  • 0

I define a struct for the move

typedef struct {
    MsgType msgType;
    int newFallenStonesSize;
    char *newFallenStones;
} MsgMove;

And send the data like this:

MsgMove message;
message.msgType = MsgTypeMove;     
message.newFallenStones = (char *)malloc(nrNewFallenStones*sizeof(char));
for (int i=0; i<nrNewFallenStones; i++) {
    message.newFallenStones[i]=newFallenStones[i];
}
message.newFallenStonesSize = nrNewFallenStones;

NSData *data = [NSData dataWithBytes:&message length:(2*sizeof(int)+message.newFallenStonesSize*sizeof(char))];          

[[KKGameKitHelper sharedGameKitHelper] sendDataToAllPlayers:data reliable:YES]; 

the data is correct at the moment of sending, but when I receive it like this:

else if (msg->msgType == MsgTypeMove)
{
    MsgMove *msgMove = (MsgMove *) [data bytes]; 
    for (int i=0; i<msgMove->newFallenStonesSize; i++) {
        NSLog(@"New Stone received:%i",msgMove->newFallenStones[i]);
    }
}

The values have changed. For example 1, 6, 3 and I receive 76, 105 98.

Anyone knows why this is happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:48:43+00:00Added an answer on June 7, 2026 at 10:48 am

    The main issue is that you are sending a pointer to your data over the network rather than the actual data. When you malloc space for message.newFallenStones it will be set to some apparently random location in memory that is not adjacent to your MsgMove structure. You then write your data in this other memory location. What you package up to transfer is the MsgMove structure (with a pointer to somewhere else in memory) plus whatever random bytes happens to immediately follow it in memory.

    The typical way this is handled is to instead have your entire message be malloc’ed together and write the data into the end of it. More like:

    typedef struct {
        MsgType msgType;
        int newFallenStonesSize;
        char newFallenStones;        // The first newFallenStones value
    } MsgMove;
    

    and then send with

    MsgMove *message;
    message = (MsgMove *)malloc(sizeof(MsgMove)+nrNewFallenStones-1);
    message->msgType = MsgTypeMove;     
    message->newFallenStonesSize = nrNewFallenStones;
    char *newStones = &MsgMove->newFallenStones;
    for (int i=0; i<nrNewFallenStones; i++) {
        newStones[i]=newFallenStones[i];
    }
    
    NSData *data = [NSData dataWithBytes:message length:(sizeof(MsgMove)+nrNewFallenStones-1)];          
    
    [[KKGameKitHelper sharedGameKitHelper] sendDataToAllPlayers:data reliable:YES]; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a struct like this: define struct _Fragment{ int a; char *seq; }Fragment;
struct { char a; int b; } x; Why would one define a struct
I have this struct; #define BUFSIZE 10 struct shared_data { pthread_mutex_t th_mutex_queue; int count;
Consider this piece of code: struct Trade { float Price; char* time; int shares;
#define MAX 100 struct bs{ int ab; int ac; }be; struct s{ be b;
Is is possible to define a macro BREF(...): struct bits { int b0:1; int
I'm curious why C++ does not define void via : typedef struct { }
Is this legal and/or good practice? #define SOFTWARE_VERSION_NUMBER 7.0v1.1 Want struct to always contain
If I defined a struct like this: struct rgb { double r; double g;
i need to help with structures, inheritance and definition. //define struct struct tStruct1{ int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.