By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

String array

P: n/a
Hey everybody. I am coding a function that takes a string as an
argument (char *msg) and breaks it up into pieces. The result will be
stored in (char ***fragments) which is passed as an argument. The
caller calls myfunction(mymsg, &my_fragment_array) where
`my_fragment_array' is a char **. My problem is that the array is
perfectly constructed inside the function, but when I switch frames in
GDB to see the caller, the second element of the array is either null
or memory garbage. How can I dynamically allocate an array of strings
from inside a function for use outside a function?

Apr 11 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
al*****@gmail.com opined:
Hey everybody. I am coding a function that takes a string as an
argument (char *msg) and breaks it up into pieces. The result will
be stored in (char ***fragments) which is passed as an argument. The
caller calls myfunction(mymsg, &my_fragment_array) where
`my_fragment_array' is a char **. My problem is that the array is
perfectly constructed inside the function, but when I switch frames
in GDB to see the caller, the second element of the array is either
null or memory garbage. How can I dynamically allocate an array of
strings from inside a function for use outside a function?


Well, you just need to pass the pointer to the allocated memory out of
the function. You can either return it as the return value of the
function, or you can pass a pointer to the variable you want it stored
in, and update its value inside the function.
--
Linux! Guerrilla UNIX Development Venimus, Vidimus, Dolavimus.
(By ma*@ka4ybr.com, Mark A. Horton KA4YBR)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Apr 11 '06 #2

P: n/a
<al*****@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Hey everybody. I am coding a function that takes a string as an
argument (char *msg) and breaks it up into pieces. The result will be
stored in (char ***fragments) which is passed as an argument. The
caller calls myfunction(mymsg, &my_fragment_array) where
`my_fragment_array' is a char **. My problem is that the array is
perfectly constructed inside the function, but when I switch frames in
GDB to see the caller, the second element of the array is either null
or memory garbage. How can I dynamically allocate an array of strings
from inside a function for use outside a function?


You could do something like this:

#include <stdlib.h>

#define STRING_NUMBER 10
#define STRING_LENGTH 20

char **foo(void)
{
int i;
char **temp=malloc(STRING_NUMBER *sizeof *temp);
if (temp==NULL)
/*Do something*/;
for (i=0;i<STRING_NUMBER;i++)
{
temp[i]=malloc(STRING_LENGTH);
if (temp[i]==NULL)
/*Do something*/;
}
return temp;
}

,adjusting it to your needs for variable number of strings, variable string
length and so on. In the former case you will need a mechanism to pass that
variable number of strings back to the caller. Do not forget to free all
allocated memory back in your main() program.
Apr 11 '06 #3

P: n/a
haha, I got it to work, thanks. It seems that I was trying too hard. :/

Apr 11 '06 #4

P: n/a
al*****@gmail.com wrote:

haha, I got it to work, thanks. It seems that I was trying too hard. :/


Well, that's nice. Whatever 'it' is, and whatever 'work' means.
Why don't you try hard to follow normal usenet conventions and
include context, so your articles have a possibility of being
meaningful. For how, see my sig. below (even on the broken foul
google interface to usenet).

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Apr 12 '06 #5

P: n/a

stathis gotsis wrote:
<al*****@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Hey everybody. I am coding a function that takes a string as an
argument (char *msg) and breaks it up into pieces. The result will be
stored in (char ***fragments) which is passed as an argument. The
caller calls myfunction(mymsg, &my_fragment_array) where
`my_fragment_array' is a char **. My problem is that the array is
perfectly constructed inside the function, but when I switch frames in
GDB to see the caller, the second element of the array is either null
or memory garbage. How can I dynamically allocate an array of strings
from inside a function for use outside a function?


You could do something like this:

#include <stdlib.h>

#define STRING_NUMBER 10
#define STRING_LENGTH 20

char **foo(void)
{
int i;
char **temp=malloc(STRING_NUMBER *sizeof *temp);
if (temp==NULL)
/*Do something*/;
for (i=0;i<STRING_NUMBER;i++)
{
temp[i]=malloc(STRING_LENGTH);
if (temp[i]==NULL)
/*Do something*/;
}
return temp;
}

,adjusting it to your needs for variable number of strings, variable string
length and so on. In the former case you will need a mechanism to pass that
variable number of strings back to the caller. Do not forget to free all
allocated memory back in your main() program.

I have a question:
in the for-statement,you use malloc for STRING_NUMBER times,does it
mean that you must use free for STRING_NUMBER times or not?I am
confused.Hope you can help.thx.

Apr 12 '06 #6

P: n/a
"pinkfog" <ch*******@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...

stathis gotsis wrote:
<al*****@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Hey everybody. I am coding a function that takes a string as an
argument (char *msg) and breaks it up into pieces. The result will be
stored in (char ***fragments) which is passed as an argument. The
caller calls myfunction(mymsg, &my_fragment_array) where
`my_fragment_array' is a char **. My problem is that the array is
perfectly constructed inside the function, but when I switch frames in
GDB to see the caller, the second element of the array is either null
or memory garbage. How can I dynamically allocate an array of strings
from inside a function for use outside a function?


You could do something like this:

#include <stdlib.h>

#define STRING_NUMBER 10
#define STRING_LENGTH 20

char **foo(void)
{
int i;
char **temp=malloc(STRING_NUMBER *sizeof *temp);
if (temp==NULL)
/*Do something*/;
for (i=0;i<STRING_NUMBER;i++)
{
temp[i]=malloc(STRING_LENGTH);
if (temp[i]==NULL)
/*Do something*/;
}
return temp;
}

,adjusting it to your needs for variable number of strings, variable string length and so on. In the former case you will need a mechanism to pass that variable number of strings back to the caller. Do not forget to free all
allocated memory back in your main() program.

I have a question:
in the for-statement,you use malloc for STRING_NUMBER times,does it
mean that you must use free for STRING_NUMBER times or not?I am
confused.Hope you can help.thx.


Yes, you should free() STRING_NUMBER times too when you free the space
allocated for the strings. In addition you should free() the space allocated
for the pointers to these strings. That means you will free()
STRING_NUMBER+1 times.
Apr 12 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.