473,413 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

String array

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
6 2615
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
<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
haha, I got it to work, thanks. It seems that I was trying too hard. :/

Apr 11 '06 #4
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

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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
22
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
8
by: Jeff Johnson | last post by:
Hi, I've begun converting an ASP site over to .NET and I'm a novice at both the new platform as well as C#. I have a COM+ object that returns a string array when it is called. The size of...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.