LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-30-2004, 12:01 AM   #1
Jamesminh
LQ Newbie
 
Registered: May 2002
Location: Vietnam
Posts: 13

Rep: Reputation: 0
Question 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
 
Old 09-30-2004, 12:04 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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...

Last edited by itsme86; 09-30-2004 at 12:06 AM.
 
Old 09-30-2004, 12:09 AM   #3
Jamesminh
LQ Newbie
 
Registered: May 2002
Location: Vietnam
Posts: 13

Original Poster
Rep: Reputation: 0
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.
 
Old 09-30-2004, 12:11 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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$
 
Old 09-30-2004, 12:18 AM   #5
Jamesminh
LQ Newbie
 
Registered: May 2002
Location: Vietnam
Posts: 13

Original Poster
Rep: Reputation: 0
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.
 
Old 09-30-2004, 12:54 AM   #6
aiza
LQ Newbie
 
Registered: Jul 2004
Posts: 18

Rep: Reputation: 0
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);
}
 
Old 09-30-2004, 03:21 AM   #7
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
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
 
Old 09-30-2004, 03:47 AM   #8
aiza
LQ Newbie
 
Registered: Jul 2004
Posts: 18

Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 01:40 AM
Assign function to extra mouse buttons sbrault Linux - Hardware 3 10-20-2005 03:55 PM
Killing a child process from another child marri Programming 6 10-01-2004 08:08 PM
call_usermodehelper function memory leak lys8934 Linux - Software 0 08-31-2004 05:46 PM
Memory usage when spawning child processes brianvdc Programming 4 10-24-2003 07:03 AM


All times are GMT -5. The time now is 08:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration