LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Assign memory in child function (https://www.linuxquestions.org/questions/programming-9/assign-memory-in-child-function-236926/)

Jamesminh 09-29-2004 11:01 PM

Assign memory in child function
 
Hi All,

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

itsme86 09-29-2004 11:04 PM

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...

Jamesminh 09-29-2004 11:09 PM

You wrong! I have done brfore you reply (like you say) but the result is still the same. Have you another way to serve this.

Thank you for reply.

itsme86 09-29-2004 11:11 PM

Umm...no, I'm not wrong:
Code:

#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$


Jamesminh 09-29-2004 11:18 PM

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.

aiza 09-29-2004 11:54 PM

Quote:

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);
}


kees-jan 09-30-2004 02:21 AM

Uh... I hope you don't seriously want him to do that :-)

While at it, I recommend changing the memory allocation line
Code:

char *pStr =  (char*)malloc (sizeof(char*) + 10);
to read
Code:

char *pStr =  (char*)malloc (10*sizeof(char));
because that's probably closer to what he intents to do.

Groetjes,

Kees-Jan

aiza 09-30-2004 02:47 AM

Quote:

Originally posted by kees-jan
Uh... I hope you don't seriously want him to do that :-)

While at it, I recommend changing the memory allocation line
Code:

char *pStr =  (char*)malloc (sizeof(char*) + 10);
to read
Code:

char *pStr =  (char*)malloc (10*sizeof(char));
because that's probably closer to what he intents to do.

Groetjes,

Kees-Jan

I only mention the way how to retrieve buffer allocate in functions, thanks for your comment :)
Aiza


All times are GMT -5. The time now is 12:01 PM.