android ndk crash when using stl map 's operator [] - android

I have a map: map < int64_t, Item* > m_items;
When using m_items[one_id], my app crashed.
But if I use map<int64_t, Item*>::iterator iter = m_items.find(one_id), it's ok.
If I need to add an item I must use: m_items.insert(make_pair(one_id, one_item));
Why is this?

Do you know what version of gcc you're using?
If it's gcc 4.5 or earlier this bug in gcc has caused Boost's unordered map's [] operator not to function correctly: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40145
Maybe you're seeing something similar.
If it's gcc 4.6 I'm not sure, but the C++11 at() method will be available to you, it's a little slower, but it is faster than find. http://www.cplusplus.com/reference/map/map/at/

Related

Android ndk 22 configuration

I'm trying to add the 22.0.7026061 NDK to Qt creator, but this version does not contain the platforms folder. Qt creator uses the following code to check the validity of the NDK and as a result I cannot add the 22nd version. In addition, I did not find any clear explanation why this paltrforms does not exist.
bool AndroidConfig::isValidNdk(const QString &ndkLocation) const
{
auto ndkPath = Utils::FilePath::fromUserInput(ndkLocation);
const Utils::FilePath ndkPlatformsDir = ndkPath.pathAppended("platforms");
return ndkPath.exists() && ndkPath.pathAppended("toolchains").exists()
&& ndkPlatformsDir.exists() && !ndkPlatformsDir.toString().contains(' ')
&& !ndkVersion(ndkPath).isNull();
}
What can I do? I need exactly the 22nd version, the versions below are normally added.
From Changelog-r21:
The legacy toolchain install paths will be removed over the coming
releases. These paths have been obsolete since NDK r19 and take up a
considerable amount of space in the NDK. The paths being removed are:
platforms
sources/cxx-stl
sysroot
toolchains (with the exception of toolchains/llvm)
As for what you can do? I don't think much, you'll have to patch this part of Qt to use newer directories (e.g toolchains/llvm for platforms). You can also report this as a bug to Qt devs.

std::trunc not available on Android?

Using C++'s std::trunc in android errors out saying trunc is not a member of std
Is there an alternative or a work around ?
<cmath> is included in my code.
My environment is:
NDK Version is r13b
ANDROID_NDK_PLATFORM = 23
ANDROID_NDK_TOOLCHAIN_VERSION = 4.9

How to use memset_s in Android NDK?

android.ndk {
moduleName = "hello-jni"
stl = "stlport_static"
CFlags.add("-std=iso9899:2011") // I have also used "-std=c11"
ldLibs.addAll(["android", "log"])
}
I still cannot see memset_s in jni C code. It says undefined reference.
In my c code i have also included string.h, stdlib.h, and stdio.h and also
#define STDC_WANT_LIB_EXT1 1
Still cannot get rid of the error undefined reference error.
If i add the flag Allow_Undefined_symbols it compiles but when ever i call the function memset_s it crashes.
The questions i would like to ask are as follows :
1) In which of the Android NDK tool chains can we get the C11 memset_s api?
2) The other question i have is how can we change the default tool chain for android in the latest android studio with experimental gradle alpha5?
This functionally of "Annex K" in the C11 standard is optional. It is not implemented in many C libraries.
You can test for conformance to Annex K by means of the macro __STDC_LIB_EXT1__.
I was not able to find any tool chain that supports C11 for NDK. So i have used a compliant solution for c99 from the following link:
https://www.securecoding.cert.org/confluence/display/c/MSC06-C.+Beware+of+compiler+optimizations
Which you can customize for your own purpose. I am adding this solution for any one in need.

Using -std=c++11 on VS2015

I have created a shared object for Android in Visual Studio 2015.
It works fine so far, but pop_back() for a wstring does not work:
wstring element = "JustATest!";
if (element.back() == L'!')
{
element.pop_back();
}
VS2015 tells me:
"no member named 'pop_back' in 'std::basic_string<wchar_t>'".
Can anybody tell me how to get rid of this error?
I have no idea why this should not work.
Is that because for some reason VS2015 does not use C++11 here?
Thank you for the help!
Edit: Another error:
When I try to use _wtoi, VS tells me: "use of undeclared identifier '_wtoi'.
Very very strange.
You need to turn on STL support. Turn on STL with Configuration Properties -> General -> Use of STL. Good options are LLVM libc++ static library (fewer features, more compatible with CLANG) and GNU STL static library (more features, I had an issue that required me to turn the CLANG optimizer to -Oz to prevent a segfault).

compile c++ for specific api level in android

I want to compile my code for specific api level. for example api level 7. and I use ndk-8. is there any option for this ?
now I use ndk-build.cmd command in windows console to compile. and I dont know how can I know which api level is supported.
This doesn't appear to be well documented (even in the NDK docs), but if you have an Application.mk (same directory as your root Android.mk), if you have a line APP_PLATFORM := android-7 (or whatever platform version you desire), it will build to that. That isn't documented in the NDK docs for Application.mk. According to the docs, if you put a TARGET_PLATFORM line in the Android.mk, it will use that, but there appears to be information out that that doesn't work.
excellent reponse, thank you Carl, you saved my life.
I use "APP_PLATFORM := android-7" and it works. when I added this line, compiling gave me an error that a function is not implemented. then, I put its implementation in my code then it works!
I think newer android version has that function but android-7 has not.
the function is wcstombs (it is in stdlib)
and its implementation is
size_t wcstombs(register char *s, register const wchar_t *pwcs, size_t n){
register int i = n;
while (--i >= 0) {
if (!(*s++ = *pwcs++))
break;
}
return n - i - 1;
}
thanks

Categories

Resources