Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am newly to c++. I am trying to make udp client which read host and port from xml configuration and run the server on new thread.
What i did was first read the configuration. Than create new thread and pass the arguments(ip, port) as a struct to it. In some moment I realize that I was trying to send payload to broadcast, not to the host what I was configure in the xml. Later I was amazed, when in the process of investigation I found that after reading it from xml it was still correct but when I passed it to the udp thread it wasn't. Do you have any idea, what is the cause of this strange behavior?

Because of the lot of questions in the comments bellow I wrote the whole code example to be more clear:

inside problem_example.cpp
#include <pthread.h>
#include <iostream>
#include <stdio.h>
#include "./pseudo_do_somethinger.h"
using namespace std;
void* another_thread_cb(void* param)
{
    while(true)
    {
        //do stuff
    }
}

void* outbound_thread_cb(void * param)
{
    printf("param(global) inside cb function: %s\n", (const char*)param);
    printf("global inside cb function: %s\n", problem.global);

    long i = 34;
    while(i != 0)
    {
        //simulating work
        i--;
    }

    const char* y = problem.global;
    printf("global assigned after time: %s\n",y);
    while(true)
    {
        //do some stuff;
    }
}


int main()
{
    
    PseudoDoSomethinger::some_const_assign();
    
    pthread_t thread0;
    pthread_t thread1;
    pthread_create(&thread0, 0, another_thread_cb,   0 );
    pthread_create( &thread1, 0, outbound_thread_cb,    (void *)&problem.global );
    printf("global inside Main: %s\n", problem.global);
    
    pthread_join( thread0, NULL );
    pthread_join( thread1, NULL );
    return 0;
}


inside pseudo do_somethinger.cpp:
#include <stdio.h>
#include "./pseudo_do_somethinger.h"
example_struct problem{
.global = nullptr, // here as in State Evaluator code the global variable
// is global only for the main() and Example class 
};

void PseudoDoSomethinger::some_const_assign()
{
    problem.global = "55";
    //problem.global = temp;
    printf("global inside PseudoXmlParser: %s\n",problem.global);
    long long i = 888885;
    while(i != 0)
    {
        //simulating work
        i--;
    }
}


inside pseudo_dosomethinger.h:
typedef struct example_struct
{
    const char* global;
}example_struct;

extern example_struct problem;
class PseudoDoSomethinger
{
public:
static void some_const_assign();

};


And despite of exact extract of the whole idea, in real code even the problem.global inside outound_thread_cb does not exist.
Do you see my mistake, anywhere?

What I have tried:

i tried to heap allocate the struct , because I was thinking of some kind overwriting the value when it is in stack , but it is was not this..

I tried to assign the ip from udp_thread_args struct this way:
outbound_thread.ip = "192.168.0.112" , and it works, so my problem is somewhere else, but still without any logical idea why "I can't assign", (or more correctly I can't assign it for longer period than function scope) the member of the first structure with the member of the second structure , despite the identical types - const char*.

Also I think the cast to void* could affect the value of the const char * despite it constant value imperative could not be changed .
Posted
Updated 1-Dec-22 8:03am
v4
Comments
Richard MacCutchan 30-Nov-22 5:49am    
Where is the system_config structure created?
CPallini 30-Nov-22 6:19am    
How is udp_thread_args defined?
Why are your casting its ip member to (char *)?
longjmp 30-Nov-22 16:36pm    
cast const char* to void* will not modify its characters,
even casting from writable char* to void*,
the 'casting' just got an address value (a pointer),
but never change the contents that pointer referenced.
you can check it by code:
#include <iostream>

using namespace std;

int main(){

const char* p = "xxxxxxzzzzyy";
char c[] = "zUUUU";

cout << p << endl;
void* p2 = (void*)p;
cout << (char*)p2 << endl;
cout << (const char*)p2 << endl;


p2 = (void*)c;
cout << (char*)p2<
merano99 30-Nov-22 20:00pm    
I'm sure you don't have to explain to CPallini what a cast does.
longjmp 1-Dec-22 0:45am    
thank you merano99, I think I just replied to Деян Цонев's question:
"Also I think the cast to void* could affect the value of the const char * despite it constant value imperative could not be changed ."

1 solution

The problem you have here is that problem.global is already a char*, so passing its address to outbound_thread_cb means that you are passing a pointer to a pointer. So just change that call to:
C++
pthread_create( &thread1, 0, outbound_thread_cb,    (void *)problem.global ); // does not need & operator
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900