469,603 Members | 2,069 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,603 developers. It's quick & easy.

beginner qn..malloc

hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.

eg: in
char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

how can i allocate the memory for this array? I know malloc() and
free() need to be used but not sure since i am only a beginner
thanx in adv
gordon
Jan 23 '08 #1
12 1234
nodrogbrown wrote:
hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.

eg: in
char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

how can i allocate the memory for this array? I know malloc() and
free() need to be used but not sure since i am only a beginner
thanx in adv
gordon
You can use a loop:

for (ctr = 0; ctr < ARR_SIZE; ctr++) {
mynames[ctr] = malloc(elements);
if (!mynames[ctr]) {
handle_error();
}
else {
elements = get_next_array_size();
}
}

I hope the object and routine names are self descriptive.

Jan 23 '08 #2
nodrogbrown wrote:
hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.

eg: in
char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

how can i allocate the memory for this array? I know malloc() and
free() need to be used but not sure since i am only a beginner
Something like:

#include <stdlib.h>

enum { NumberOfNames = 42 };

int main(void)
{
char* mynames[NumberOfNames] = {NULL};

size_t stringSize = someValue;

mynames[0] = malloc( stringSize );

stringSize = someOtherValue;

mynames[1] = malloc( stringSize );

/* do stuff */

for( unsigned n = 0; n < NumberOfNames; free( mynames[n++] ) );
}

--
Ian Collins.
Jan 23 '08 #3
nodrogbrown wrote:
hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.

eg: in
char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

how can i allocate the memory for this array? I know malloc() and
free() need to be used but not sure since i am only a beginner
thanx in adv
gordon
Other people have given you one kind of solution. However, if all of
your strings come from string literals, there's a much simpler solution:

char * mynames[] =
{"mathew", "gordon_brown", "F:\gordon\docs\mycv.txt"};

You probably need the more complicated solution, but since you're a
beginner I didn't want to miss out on the possibility that the simpler
solution would meet your needs.
Jan 23 '08 #4
James Kuyper wrote:
nodrogbrown wrote:
>hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.

eg: in
char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

how can i allocate the memory for this array? I know malloc() and
free() need to be used but not sure since i am only a beginner
thanx in adv
gordon

Other people have given you one kind of solution. However, if all of
your strings come from string literals, there's a much simpler
solution:

char * mynames[] =
{"mathew", "gordon_brown", "F:\gordon\docs\mycv.txt"};

You probably need the more complicated solution, but since you're a
beginner I didn't want to miss out on the possibility that the simpler
solution would meet your needs.
He specifically says above that the sizes of the strings will not be
known at compile time.

Jan 23 '08 #5
In article <fn**********@registered.motzarella.org>,
santosh <sa*********@gmail.comwrote:
>>i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.
[...]
>>mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"
>He specifically says above that the sizes of the strings will not be
known at compile time.
He also gives an example in which they are, so he might be confused.

-- Richard
--
:wq
Jan 23 '08 #6
James Kuyper <ja*********@verizon.netwrites:
nodrogbrown wrote:
>i want to create an array of strings
[...]
>char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"
[...]
>
Other people have given you one kind of solution. However, if all of
your strings come from string literals, there's a much simpler
solution:

char * mynames[] =
{"mathew", "gordon_brown", "F:\gordon\docs\mycv.txt"};
The \g, \d, and \m in the string literal are going to cause problems.
If you want a backslash character in a string literal, you need to
double it:

"F:\\gordon\\docs\\mycv.txt"

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 23 '08 #7
On Jan 23, 12:26 pm, Keith Thompson <ks...@mib.orgwrote:
James Kuyper <jameskuy...@verizon.netwrites:
nodrogbrown wrote:
i want to create an array of strings
[...]
char * mynames[]
mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"
[...]
Other people have given you one kind of solution. However, if all of
your strings come from string literals, there's a much simpler
solution:
char * mynames[] =
{"mathew", "gordon_brown", "F:\gordon\docs\mycv.txt"};

The \g, \d, and \m in the string literal are going to cause problems.
If you want a backslash character in a string literal, you need to
double it:

"F:\\gordon\\docs\\mycv.txt"
That's actually a problem inherited from the original question, I just
cut-and-pasted it. However, I should have mentioned the problem. All
my programming for the last dozen years has been for Unix-like
systems, where filenames seldom present the same kind of problem, so I
didn't even think about it.
Jan 23 '08 #8
>I'm not sure what you mean when you say that
>"F:\\gordon\\docs\\mycv.txt" "has a chance". Is that meant to imply
that there's a chance it could fail? If so, how?
How many systems do you know of that DON'T have that file? (or,
for that matter, don't even have a F: drive?) If so, trying to
open it would fail. And there's always the chance a file open could
fail due to bad sectors, insufficient permissions, Sony rootkits, etc.

There are no defined characters for the escapes \g, \d, or \m. An
implementation might take \g to be the Google logo and \m to be the
Microsoft Windows logo, which normally aren't allowed in file names.
\d might be considered an error unless the content rating allows
material for 35+.

There are defined characters for escapes such as \r, \n, \b, and
\f, but it can get awkward putting carriage returns, newlines,
backspaces, and form feeds in file names (UNIX will let you do it,
but it's still awkward).

Jan 27 '08 #9
go***********@burditt.org (Gordon Burditt) writes:
>>I'm not sure what you mean when you say that
"F:\\gordon\\docs\\mycv.txt" "has a chance". Is that meant to imply
that there's a chance it could fail? If so, how?
I wrote the above. Stop snipping attributions.
How many systems do you know of that DON'T have that file? (or,
for that matter, don't even have a F: drive?) If so, trying to
open it would fail. And there's always the chance a file open could
fail due to bad sectors, insufficient permissions, Sony rootkits, etc.
Who said anything about opening files? The original question was
about storing strings (one of which happens to look very much like a
file name).

"F:\\gordon\\docs\\mycv.txt" is a perfectly valid string literal; I
see no way it can fail *as a string literal*.

In any case, I was asking what Joe Wright meant.
There are no defined characters for the escapes \g, \d, or \m. An
implementation might take \g to be the Google logo and \m to be the
Microsoft Windows logo, which normally aren't allowed in file names.
\d might be considered an error unless the content rating allows
material for 35+.

There are defined characters for escapes such as \r, \n, \b, and
\f, but it can get awkward putting carriage returns, newlines,
backspaces, and form feeds in file names (UNIX will let you do it,
but it's still awkward).
Of course, that was the point of changing \ to \\ several messages
upthread.

This article was written by Keith Thompson <ks***@mib.org>.
Permission to quote without attribution is explicitly denied for this
message and for every message I post here.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jan 27 '08 #10
Keith Thompson wrote:
>
.... snip ...
>
This article was written by Keith Thompson <ks***@mib.org>.
Permission to quote without attribution is explicitly denied for
this message and for every message I post here.
I like this.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Jan 28 '08 #11
On Wed, 23 Jan 2008 15:15:11 +0000, Richard Tobin wrote:
In article <fn**********@registered.motzarella.org>, santosh
<sa*********@gmail.comwrote:
>>>i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.
[...]
>>>mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

He specifically says above that the sizes of the strings will not be
known at compile time.

He also gives an example in which they are, so he might be confused.
He also uses some odd characters in his example. What, pray tell, does
\g do? Or \d? \m?

Jan 28 '08 #12
In article <no************@spanky.localhost.net>,
Kelsey Bjarnason <kb********@gmail.comwrote:
>>>>mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"
>>>He specifically says above that the sizes of the strings will not be
known at compile time.
>He also gives an example in which they are, so he might be confused.
>He also uses some odd characters in his example. What, pray tell, does
\g do? Or \d? \m?
I think that problem was addressed several days ago.

-- Richard

--
:wq
Jan 29 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Atip Asvanund | last post: by
6 posts views Thread by sathyashrayan | last post: by
11 posts views Thread by rory | last post: by
5 posts views Thread by Yourko | last post: by
9 posts views Thread by raam | last post: by
13 posts views Thread by fool | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.