ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I have some problem in assign memory for the pointer type. My example code in below:
void test(char *kk)
{
kk = malloc (sizeof(char*) + 10);
strcpy(kk,"abcdef");
printf("Value of kk inside test(): %s\n",kk);
}
int main()
{
char *kk;
test(kk);
printf("Value of kk at main: %s\n",kk);
return 0;
}
when run:
Value of kk inside test: abcdef
Memory fault (core dumped)
Why I get "Memory fault" message. I think when exit out of test() function the memory have been assigned will be release right? If true. How to serve this problem.
My requirement is: assign memory inside the child function and can get back at main when exit the child fucntion.
Please help me this problem. Thank you so much.
James
The problem is that you're only changing the value of kk inside the function. Try passing a pointer to kk to your test() function instead:
Code:
void test(char **kk)
{
*kk = malloc (sizeof(char*) + 10);
strcpy(*kk, "abcdef");
printf("Value of kk inside test(): %s\n", *kk);
}
int main()
{
char *kk;
test(&kk);
printf("Value of kk at main: %s\n",kk);
return 0;
}
What you were doing is the same thing as this:
Code:
int set_it(int num)
{
num = 5;
printf("num in set_it(): %d\n", num);
}
int main()
{
int num;
set_it(num);
printf("num in main(): %d\n", num);
return 0;
}
It's usually easier for me to see if the variable I'm playing with isn't already a pointer...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void test(char **kk)
{
*kk = malloc (sizeof(char*) + 10);
strcpy(*kk, "abcdef");
printf("Value of kk inside test(): %s\n", *kk);
}
int main()
{
char *kk;
test(&kk);
printf("Value of kk at main: %s\n",kk);
return 0;
}
itsme@itsme:~/C$ ./foof
Value of kk inside test(): abcdef
Value of kk at main: abcdef
itsme@itsme:~/C$
Oh!!!! Sorry I have a mistake before. Yes! you right!. But have you another way if only accept a char's pointer (not array of char pointer) argument at test function. Mean test function not change test(char *kk)?
Originally posted by Jamesminh Oh!!!! Sorry I have a mistake before. Yes! you right!. But have you another way if only accept a char's pointer (not array of char pointer) argument at test function. Mean test function not change test(char *kk)?
Thank you very much.
Code:
void test(char *kk)
{
char *pStr = (char*)malloc (sizeof(char*) + 10);
strcpy(pStr,"abcdef");
printf("Value of kk inside test(): %s\n",pStr);
*(int *)kk = (int)pStr;
}
int main()
{
char kk[8], *pStr;
test(kk);
pStr = (char*)0 + *(int *)kk;
printf("Value of kk at main: %s\n",pStr);
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.