String array

A

alex323

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?
 
V

Vladimir S. Oka

(e-mail address removed) 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.
 
S

stathis gotsis

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=malloc(STRING_LENGTH);
if (temp==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.
 
C

CBFalconer

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/>
 
P

pinkfog

stathis said:
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=malloc(STRING_LENGTH);
if (temp==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.
 
S

stathis gotsis

pinkfog said:
stathis said:
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=malloc(STRING_LENGTH);
if (temp==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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top