#include <jni.h> #include <string> #include <stdio.h> #include <stdlib.h> #include "logs.h" char *fa() { //char *pa = "123456"; // pa 指针在栈区,“123456”在常量区,该函数调用完后指针变量 pa 就被释放了 char *p = NULL; //指针变量 p 在栈中分配 4 字节 p = (char *) malloc(100); //本函数在这里开辟了一块堆区的内存空间,并把地址赋值给 p strcpy(p, "wudunxiong 1234566"); //把常量区的字符串拷贝到堆区 return p; //返回给主调函数 fb(),相对 fa 来说 fb 是主调函数,相对 main 来说, //fa(),fb()都是被调用函数 } char *fb() { char *pstr = NULL; pstr = fa(); return pstr; //指针变量 pstr 在这就结束 } void selectSort(int *p, int n) { // LOGE("%d\n", p[0]); int tmp; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (p[i] > p[j]) { tmp = p[i]; p[i] = p[j]; p[j] = tmp; } } } } char *getMem(char **p, int n) { *p = (char *) (malloc(n)); if (*p == NULL) return NULL; return *p; } void displayArray(int(*p)[4], int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { //printf("%d ", *(*(p + i) + j)); LOGE("%d ", *(*(p + i) + j)); } putchar(10); } } int main_s() { LOGE("sizeof(char*) = %d\n", sizeof(char *)); LOGE("sizeof(shor*) = %d\n", sizeof(short *)); LOGE("sizeof(int*) = %d\n", sizeof(int *)); LOGE("sizeof(long long*) = %d\n", sizeof(long long *)); LOGE("sizeof(double *) = %d\n", sizeof(double *)); LOGE("sizeof(int (*p)[5]) = %d\n", sizeof(int (*)[5])); return 0; } extern "C" JNIEXPORT jstring JNICALL Java_com_massky_chars_1s_MainActivity_stringFromJNI( JNIEnv *env, jobject /* this */) { std::string hello = "Hello from C++"; main_s(); return env->NewStringUTF(hello.c_str()); }
jni中log可以代替printf来打印
最新推荐文章于 2022-11-08 20:12:38 发布