LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-13-2005, 12:18 PM   #1
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Rep: Reputation: 15
basic pointer prob.


2 questions. Im fumbling in the dark with these pointer errors.

Code:
void func3(char *src, char *dst)
{
 printf("\nfunc3->src: %s", src);
 free(src);
 src = malloc(10);
 strcpy(src, (char *)"fromfunc3");
 printf("\nfunc3->src: %s", src);
 (dst) = malloc(10);
 strcpy(dst, (char *)"newdata..");
 printf("\nfunc3->dst: %s", dst);
}

void func2(char *src)
{
 printf("\nfunc2->src: %s", src);
 free(src);
 src = malloc(10);
 strcpy(src, (char *)"fromfunc2");
 char *dst;
 func3(src, dst);
 printf("\nfunc2->src: %s", src);                    
 printf("\nfunc3->dst: %s", dst);                    //problem #1
 free(src);
 src = &dst;                                  //problem #2
}

int main(int argc, char *argv[])
{
 char *ptr = malloc(10);
 strcpy(ptr, (char*)"frommain.");
 func2(ptr);
 printf("\nmain->ptr: %s", ptr);
return 0;
}
1. problem. I get a seg. fault at first comment in func2 - how come? Im not doing anything different with dst, than I'm doing with src.
2. problem. I need to make src point at the memory pool dst is pointing at - func2 at 2. comment. Can someone please explain what I'm doing wrong here?
Thank
j.

Last edited by jnusa; 01-13-2005 at 12:20 PM.
 
Old 01-13-2005, 12:24 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
You need to pass a pointer to the pointer if you want to update that pointer with the newly allocated memory. You already know that you can't do this:
Code:
void func(int a)
{
  a = 4;
}

int main(void)
{
  int num = 5;

  func(num);
  printf("%d\n", num);
  return 0;
}
That program will still print 5 because func() has its own local copy of the number and that's the only one that it's modifying. When you pass src to your function, it's modifying its own local copy so the calling function's src is still pointing to the free()'d memory. BAM! Seg. fault. Instead, you have to do something like this:
Code:
void func(char **str)
{
  free(*str);
  *str = malloc(10);
  strcpy(*str, "infunc");
}

int main(void)
{
  char *str = malloc(10);

  strcpy(str, "inmain");
  printf("%s\n", str);
  func(&str);
  printf("%s\n", str);
  return 0;
}
That way the value of str gets updated by func(). Make sense?
 
Old 01-13-2005, 12:40 PM   #3
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
Yes, very easy to understand. Just must remember, that when ever you have e prototype with "char *", it makes a local copy. Thanks itsme86

for reference:

Code:
void func3(char **src, char **dst)
{
 printf("\nfunc3->src: %s", *src);
 free(*src);
 *src = malloc(10);
 strcpy(*src, (char *)"fromfunc3");
 printf("\nfunc3->src: %s", *src);
 *dst = malloc(10);
 strcpy(*dst, (char *)"newdata..");
 printf("\nfunc3->dst: %s", *dst);
 
 
}

terminal printout:




void func2(char **src)
{
	printf("\nfunc2->src: %s", *src);
	free(*src);
 *src = malloc(10);
 strcpy(*src, (char *)"fromfunc2");
 char *dst;
 func3(src, &dst);
 printf("\nfunc2->src: %s", *src); //problem #1
 printf("\nfunc3->dst: %s", dst);
 free(*src);
 (*src) = dst;       //problem #2
 printf("\nfunc2->src: %s", *src);
 
}


int main(int argc, char *argv[])
{
 char *ptr = malloc(10);
 strcpy(ptr, (char*)"frommain.");
 func2(&ptr);
 printf("\nmain->ptr: %s", ptr);
 return 0;
}
Quote:
func2->src: frommain.
func3->src: fromfunc2
func3->src: fromfunc3
func3->dst: newdata..
func2->src: fromfunc3
func3->dst: newdata..
func2->src: newdata..
main->ptr: newdata..

Last edited by jnusa; 01-13-2005 at 12:42 PM.
 
Old 01-13-2005, 01:25 PM   #4
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
Well oka, i rewrote my code to this syntax. Let's say that I wanted to call func4 - prototype
Code:
 void func4(char *dst, char *src)
within func3. The function takes 2 ordinary pointers, alters data in src and saves in dst (and allocation is OK). Would this call look like this?
Code:
...
func4(*dst, *src);
...

Last edited by jnusa; 01-13-2005 at 01:26 PM.
 
Old 01-13-2005, 01:48 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
If you want to keep the same pointer you can just pass it directly through. Eample:
Code:
void func3(char **str)
{
  // Stuff
}

void func2(char **str)
{
  func3(str);
}

void func1(void)
{
  char *str;

  ...
  func2(&str);
  ...
}
 
Old 01-13-2005, 02:11 PM   #6
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
hmm well I'm asking because I'm using compress2() from zlib.h and it takes 2 ByteF pointers
prototype:
Code:
int compress2(ByteF *dest, uLong destLen, const Bytef *source, uLong sourceLen, int level)
This function (as you probably know) compresses source and saves in dest. If a function always make a local copy, when ever a pointer is passed (and the prototype of the function is char *), how can compress2 save in dest?

say func4(char *dst, char *src)
You shouldn't be able to e.g. strcpy somethink over in dst, and access it in parent scope right? But this is what compress2() does!
 
Old 01-13-2005, 02:42 PM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
When you pass a char * to a function, you're not passing the allocated memory that the char * points to, you're passing the address of the memory. The reason your original example was failing is because you're modifying the pointer in the called function. strcpy() is a good example of how this works. You pass it a pointer to allocated memory in which it can store the second string.

Code:
mystrcpy(char *dst, char *src)
{
  while(*src)
    *dst++ = *src++;
  *dst = '\0';
}

int main(void)
{
  char buf[50];

  mystrcpy(buf, "hello");
  printf("%s\n", buf);
  return 0;
}
You can see, mystrcpy() actually modifies dst by incrementing it a bunch of times, but when you return to main(), buf still points to its original location. But in your original code, you were free()'ing the memory that dst pointed to and then allocating new memory for it. So when it got back to the calling function, dst still pointed to the free()'d memory instead of the newly allocated memory.
 
  


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
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 01:36 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
Basic pointer scope Q jnusa Programming 8 11-22-2004 09:14 AM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
XFree keyboard problem, Quake prob and half-life prob cradlebin Linux - Software 0 09-21-2003 06:57 AM


All times are GMT -5. The time now is 06:57 AM.

Main Menu
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