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 6994083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:50:48+00:00 2026-05-27T19:50:48+00:00

I’ve got this piece of code, where I gather device id from different types

  • 0

I’ve got this piece of code, where I gather device id from different types of devices supported by my game and set lua global to have value of id of current device.
When I get id of the iOS device I receive a const char* from a mixed C++/Objective-C class and pass it on to the Lua stack. It all works fine.
However I receive std::string from a piece of code responsible for getting Android device id. When I push deviceId.c_str() I get nil in Lua.
I’ve tried passing const char* from the code responsible for getting the device id, but then it seems something wrong goes on with the pointer when it’s returned from function [that’s why I decided to return string, it works fine this way].

What should I do to allow passing const char* out of std::string without problems?

EDIT:
I’ve tried using strcpy but it didn’t work :/ still having the same problem.

So.. the code responsible for gathering deviceId from different devices looks like this:

#include "DeviceInfo.h"
#include "DeviceInfoIOS.h"
#include "DeviceInfoAndroid.h"
#include <string>


USING_NS_CC;

extern "C" {

const char *getDeviceId() {

    const char *deviceId;

    CCLog("test");

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    DeviceInfoIOS ios;
    deviceId = ios.getIOSDeviceId();
    CCLog("iOS platform %s", deviceId);

#endif  // CC_PLATFORM_IOS

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

    CCLog("Android platform");
    std::string tempId = getAndroidDeviceId();
    CCLog("Android platform test %s", tempId.c_str());
    char y[tempId.size() + 1];
    strcpy(y, tempId.c_str());
    deviceId = (const char*) y;
    CCLog("Android platform %s", deviceId);


#endif  // CC_PLATFORM_ANDROID
    CCLog("Finished platform check");
    return deviceId;
}

}

Just a small note: All the logs look ok. Devie id is passed fine.

This is how I pass device id to Lua:

//deviceInfo
CCLog("DeviceInfo load");
const char *deviceId = getDeviceId();
CCLog("DeviceInfo %s", deviceId);
lua_pushstring(d_state, deviceId);
lua_setglobal(d_state, "DEVICE_ID");

Also in here, logfile contains the device id.

  • 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-05-27T19:50:49+00:00Added an answer on May 27, 2026 at 7:50 pm

    Your getDeviceId function is broken. Both tempId and y are stack variables. They will be destroyed once you return. Returning pointers to stack variables is always a bad idea.

    Your function ought to return a std::string. Failing that, it should return a char* array that it allocates with new, and that the user is expected to deallocate with delete. That’s generally why it’s preferable to just return a std::string. Alternatively, you could delcare y as a static local variable using a fixed size (rather than one based on the string).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.