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)
{
}
}
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)
{
i--;
}
const char* y = problem.global;
printf("global assigned after time: %s\n",y);
while(true)
{
}
}
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,
};
void PseudoDoSomethinger::some_const_assign()
{
problem.global = "55";
printf("global inside PseudoXmlParser: %s\n",problem.global);
long long i = 888885;
while(i != 0)
{
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 .