Unsolved Object initialize
-
Hi friends,
I am trying to initialize array of class object to null i am getting error.
variable sized object cannot be initialized how can i check .channelinfoobj *m_nchannelinfoobj[4] == NULL;
cananybody tell how to initialize.
-
@satyanarayana143 said in Object initialize:
channelinfoobj *m_nchannelinfoobj[4] == NULL;
This is not an assignment.
And you can't just assign nullptr to an array as it has several elements.channelinfoobj *m_nchannelinfoobj[4] { nullptr, nullptr, nullptr, nullptr };
-
@jsulm said in Object initialize:
4
channelinfoobj *m_nchannelinfoobj[4] { nullptr, nullptr, nullptr, nullptr };
i can give like this but 4 value i t will come dynamically for understanding i have given 4. it will come channelcnt.
-
@satyanarayana143 said in Object initialize:
i can give like this but 4 value i t will come dynamically for understanding i have given 4. it will come channelcnt.
You can always use a loop to initialise the elements:
channelinfoobj *m_nchannelinfoobj[4]; for (int i = 0; i < 4; ++i) m_nchannelinfoobj[i] = nullptr;
-
channelinfoobj *m_nchannelinfoobj[4];
for (int i = 0; i < 4; ++i)
{
m_nchannelinfoobj[i] = nullptr;
m_nchannelinfoobj[i] = new channelinfoobj ();
}this loop function will call every 5 sec then i have to validate new is already created or not . m_nchannelinfoobj[i] = nullptr; every time it will be null.
m_nchannelinfoobj[i] using by other . -
@satyanarayana143 said in Object initialize:
every time it will be null
No, because you're assigning it a valid pointer just after assigning nullptr (I have no idea why you're doing this!), so it will be always != nullptr (unless you assign nullptr somewhere else):
for (int i = 0; i < 4; ++i) { m_nchannelinfoobj[i] = nullptr; // Why?! Next line you're assigning a pointer to it. m_nchannelinfoobj[i] = new channelinfoobj (); // Now it is not nullptr any-more }
"then i have to validate new is already created or not " - isn't that as simple as:
if (m_nchannelinfoobj[i]) { // Do something }
?
Can you please explain better what you want to achieve and what the problem is?
-
void EdgeClient::processPendingDatagrams()
{
struct uftp_h *header;
int packetlen = 0;header = (struct uftp_h *)malloc(sizeof(struct uftp_h)); struct ChannelInfo_Recieve* channelData; QByteArray datagram; datagram.resize(udpSocket->pendingDatagramSize()); udpSocket->readDatagram(datagram.data(),datagram.size()); unsigned char *pCmdBuffer = (unsigned char*)datagram.data(); memcpy(header,pCmdBuffer,sizeof(struct uftp_h)); packetlen+= sizeof(struct uftp_h); qDebug() << "Socket Read Bytes:" << datagram.size() << "####### Header Info #########" << "Vernum:" << header->version << "Announce:" << header->func <<"channel Count:" << header->channelcnt ; channelInfoStruct *m_nchannelInfoStruct[header->channelcnt]; for(int i = 0;i < header->channelcnt;i++) { m_nchannelInfoStruct[i] = new channelInfoStruct(); channelData = (struct ChannelInfo_Recieve *)malloc(sizeof(struct ChannelInfo_Recieve)); uint16_t *pgrp = (uint16_t *)malloc(channelData->groupsize*sizeof(uint16_t)); if(packetlen+sizeof(struct ChannelInfo_Recieve) > MAX_BUFFERSIZE) { qDebug() << "######### Receive Buffer size Exceeded ##############"; exit(0); } memcpy(channelData,pCmdBuffer+packetlen,sizeof(struct ChannelInfo_Recieve)); packetlen+= sizeof(struct ChannelInfo_Recieve); struct in_addr Inaddr = {channelData->InputIPAddress}; struct in_addr outaddr = {channelData->outputIPAddress}; qDebug() << "######################################################"; qDebug() <<"ChannelType:" << channelData->channelType << "OutputPort:" << channelData->OutputPort <<"Inputport:" << channelData->Inputport << "GroupSize:" << channelData->groupsize <<"InputIPAddress:"<<inet_ntoa(Inaddr)<< "outputIPAddress:"<<inet_ntoa(outaddr) << endl; qDebug() << "OutputPort:" << channelData->OutputPort; if(packetlen+(channelData->groupsize*sizeof(uint16_t)) > MAX_BUFFERSIZE) { qDebug() << "######### Receive Buffer size Exceeded ##############"; exit(0); } memcpy(pgrp,pCmdBuffer+packetlen,channelData->groupsize*sizeof(uint16_t)); packetlen+= channelData->groupsize*sizeof(uint16_t); // for(int gid = 0;gid < schData->groupsize;gid++) // { // qDebug() << "sGroupID:" << pgrp[gid]; // } QString m_SystemIp ; struct ifaddrs *ifaddr, *ifa; int family, s; char host[NI_MAXHOST]; if (getifaddrs(&ifaddr) == -1) { perror("getifaddrs"); exit(EXIT_FAILURE); } for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) { if (ifa->ifa_addr == nullptr) continue; s = getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, nullptr,0, NI_NUMERICHOST); if( ((strcmp(ifa->ifa_name,"wlan0")==0)&&(ifa->ifa_addr->sa_family==AF_INET)) || ((strcmp(ifa->ifa_name,"enp3s0")==0)&&(ifa->ifa_addr->sa_family==AF_INET)) || ((strcmp(ifa->ifa_name,"eth0")==0)&&(ifa->ifa_addr->sa_family==AF_INET))) { if (s != 0) { qDebug("getnameinfo() failed: %s\n", gai_strerror(s)); exit(EXIT_FAILURE); } qDebug("Interface : %s ",ifa->ifa_name ); qDebug("Address : %s", host); m_SystemIp = host; qDebug() << "m_SystemIp:" << m_SystemIp; } } freeifaddrs(ifaddr); if(channelData->channelType == FILETYPE) { if(m_nchannelInfoStruct[i]->FilePublicIp == inet_ntoa(Inaddr) && m_nchannelInfoStruct[i]->FileOutputport == channelData->OutputPort) { qDebug() << "########## File MultiCast IpAddress and port Used i ############"; } else { qDebug() << "########## File MultiCast IpAddress and Port not Used i ############"; //Store File PublicMulticast IP and Port if (m_nchannelInfoStruct[i]->m_nchannelProcess.state() == QProcess::NotRunning) { m_nchannelInfoStruct[i]->FilePublicIp = inet_ntoa(Inaddr); m_nchannelInfoStruct[i]->FileOutputport = channelData->OutputPort; qDebug().noquote() << "############ File Process not Running i ##############" << i << "FileOutputport:" << m_nchannelInfoStruct[i]->FileOutputport << "FilePublicIp:" << m_nchannelInfoStruct[i]->FilePublicIp; QString program = qApp->applicationDirPath()+"/uftpd"; QStringList arguments; arguments << "-M"<< m_nchannelInfoStruct[i]->FilePublicIp << "-p" << QString::number(m_nchannelInfoStruct[i]->FileOutputport) << "-D" << RECEIVE_FILE_PATH; qDebug() <<"File Program:"<< program <<"File arguments:" << arguments; m_nchannelInfoStruct[i]->m_nchannelProcess.start(program,arguments); } else { qDebug().noquote() << "############ File Process Already Running i ##############"; } } } else if(channelData->channelType == LIVESTREAMTYPE) { if (m_nchannelInfoStruct[i]->m_nchannelProcess.state() == QProcess::NotRunning) { qDebug().noquote() << "############ OnStream Process not Running j ##############"; if(m_nchannelInfoStruct[i]->onStreamIP == inet_ntoa(outaddr) && m_nchannelInfoStruct[i]->onStreamport == channelData->OutputPort) { qDebug() << "############## Stream Ip and Port Already Used ###############"; } else { m_nchannelInfoStruct[i]->onStreamIP = inet_ntoa(outaddr); m_nchannelInfoStruct[i]->onStreamport = channelData->OutputPort; } QString Streamfeed = "feed"+QString::number(i+1); QStringList argumentsList ; if(m_nchannelInfoStruct[i]->onStreamIP >= "224.0.0.0 " && m_nchannelInfoStruct[i]->onStreamIP <= "239.255.255.255") { argumentsList << m_nchannelInfoStruct[i]->onStreamIP << QString::number(m_nchannelInfoStruct[i]->onStreamport) << Streamfeed; } else { argumentsList << m_nchannelInfoStruct[i]->onStreamIP << QString::number(m_nchannelInfoStruct[i]->onStreamport) << Streamfeed; } QString program = qApp->applicationDirPath()+"/StreamTest.sh"; qDebug() <<"Porcess and Arguments" << program << argumentsList; m_nchannelInfoStruct[i]->m_nchannelProcess.start(program,argumentsList); } } else { qDebug() << "########### Invalid Channel Type ############"; } }
}
this function will call every 5 sec once
m_nchannelInfoStruct[i]->m_nchannelProcess.start() it will be running state if c create new it will be destroyed what is running in process.
-
@satyanarayana143 said in Object initialize:
datagram.resize(udpSocket->pendingDatagramSize());
What is the problem? I don't have time to analyse all this code, please be so kind and tell what the problem is, or which specific part of the code you mean.
Just a note: you're writing quite low-level code (C style, memcpy, malloc), shouldn't be necessary with Qt/C++.
-
channelInfoStruct *m_nchannelInfoStruct[header->channelcnt];
for(int i = 0;i < header->channelcnt;i++)
{
m_nchannelInfoStruct[i] = new channelInfoStruct();
} -
-
channelInfoStruct *m_nchannelInfoStruct[header->channelcnt];
for(int i = 0;i < header->channelcnt;i++)
{
m_nchannelInfoStruct[i] = new channelInfoStruct();
}
m_nchannelInfoStruct[i] i am starting process it is in running mode foe every 5 sec i am calling this function new of m_nchannelInfoStruct[i] is creating when i create new if process is running i am getting Qprocess destroyed i want to check
if m_nchannelInfoStruct[i] = new channelInfoStruct(); already created i dont want to create new again. -
@satyanarayana143 said in Object initialize:
if m_nchannelInfoStruct[i] = new channelInfoStruct(); already created i dont want to create new again.
I already provided code for that, did you read it?
Here once more:if (m_nchannelinfoobj[i]) { // Do something }
So, in your case:
if (!m_nchannelinfoobj[i]) { m_nchannelInfoStruct[i] = new channelInfoStruct(); }
-
if (!m_nchannelinfoobj[i]) {
m_nchannelInfoStruct[i] = new channelInfoStruct();
}when i create like this and starting QProcess it is crashing
-
@satyanarayana143 said in Object initialize:
when i create like this and starting QProcess it is crashing
Then use debugger to see what happens...
Also, if you delete m_nchannelInfoStruct[i] you need to set it to nullptr:delete m_nchannelInfoStruct[i]; m_nchannelInfoStruct[i] = nullptr;
And: m_nchannelInfoStruct is a local variable, so it goes out of scope and is destroyed as soon as it leaves its scope...
m_ prefix is usually used to mark member variables but m_nchannelInfoStruct is a local variable in your code... -
delete m_nchannelInfoStruct[i];
m_nchannelInfoStruct[i] = nullptr;i am not calling delete
-
@satyanarayana143
As @jsulm is telling you:void EdgeClient::processPendingDatagrams() { channelInfoStruct *m_nchannelInfoStruct[header->channelcnt]; for(int i = 0;i < header->channelcnt;i++) { m_nchannelInfoStruct[i] = new channelInfoStruct(); m_nchannelInfoStruct[i]->m_nchannelProcess.start(program,argumentsList); } // m_nchannelInfoStruct destroyed here }
m_nchannelInfoStruct
is a local variable inprocessPendingDatagrams()
function. When that exits the array is destroyed.Then your
m_nchannelProcess
, you are starting a new process, on the sameQProcess
variable, each time round. Dunno what's going on there.