beginner qn..malloc

N

nodrogbrown

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
 
S

santosh

nodrogbrown said:
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.
 
I

Ian Collins

nodrogbrown said:
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++] ) );
}
 
J

James Kuyper

nodrogbrown said:
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.
 
S

santosh

James said:
nodrogbrown said:
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.
 
R

Richard Tobin

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"
[/QUOTE]
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
 
K

Keith Thompson

James Kuyper said:
nodrogbrown said:
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"
 
J

jameskuyper

James Kuyper said:
nodrogbrown said:
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.
 
J

Joe Wright

nodrogbrown said:
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

Your declaration of mynames[] and assignments to it won't work. Assuming
your three strings, try..

char **names;
names = malloc(3 * sizeof *names);
names[0] = malloc(strlen("mathew")+1);
strcpy(names[0], "mathew");
names[1] = malloc(strlen("gordon brown")+1);
strcpy(names[1], "gordon brown");

Not pretty, I admit.
Your use of "F:\gordon\docs\mycv.txt" won't work either. the '\' is an
escape character in C strings. "F:\\gordon\\docs\\mycv.txt" has a
chance. Also "F:/gordon/docs/mycv.txt" has a chance.

names[2] = malloc(strlen("F:/gordon/docs/mycv.txt")+1);
strcpy(names[1], "F:/gordon/docs/mycv.txt");

To free all this up..

for (i = 0; i < 3; ++i)
free(names);
free(names);
 
K

Keith Thompson

Joe Wright said:
Your use of "F:\gordon\docs\mycv.txt" won't work either. the '\' is an
escape character in C strings. "F:\\gordon\\docs\\mycv.txt" has a
chance. Also "F:/gordon/docs/mycv.txt" has a chance.
[...]

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?

"F:/gordon/docs/mycv.txt" is a different string than
"F:\\gordon\\docs\\mycv.txt". They *might* be functionally equivalent
in some contexts (but not all; the details are OS-specific), but
they'll certainly [*] be displayed differently, and that might be what
matters.

[*] I'm assuming a display device on which the forward slash and
backslash characters are visually distinct.
 
G

Gordon Burditt

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).
 
K

Keith Thompson

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 <[email protected]>.
Permission to quote without attribution is explicitly denied for this
message and for every message I post here.
 
K

Kelsey Bjarnason

santosh said:
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?
 
R

Richard Tobin

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.
[/QUOTE]
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
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top