Re: string in an array
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Tue, 31 Mar 2009 16:40:16 -0500 (CDT)
clinisbut <clinisbut@xxxxxxxxx> writes:
I have a doutb about allocating memory and storing a string inside it.
It may sound a very stupid question, but I need to clear it...
My doubt comes when declaring a array bigger than the string it is
going to be stored inside it:
char* array = malloc( sizeof(char)*1024 );
strcpy( array, "hello" );
You haven't declared an array here. You've declared a pointer
(misleadingly named "array") and initialized it to point to an
anonymous region of storage of 1024 bytes.
Note that sizeof(char) is 1 by definition, so this could be written
as:
char *array = malloc(1024);
Note also that you should always allow for the possibility that malloc
could fail.
When comes to free it, I always supposed that the entire array is
freed.
Yes, the entire region of storage allocated by malloc is freed when
you pass the pointer to free.
But today, using the (non compliant) mode to check the size of an
array:
sizeof(array) / sizeof(array[0])
I noticed that the size I get is the size of "hello" and not 1024
bytes (I repeat, I know this is not the best way to check an array's
size). I'm not sure if free() is going to free strlen("hello") bytes
or the full 1024.
No, the size of "hello" is 6 bytes ({ 'h', 'e', 'l', 'l', 'o', '\n'}).
I'd expect ``sizeof(array) / sizeof(array[0])'' to yield 4 or 8 on
most platforms, though 6 is theoretically possible.
sizeof(array) gives you the size of the pointer object, not of the
allocated array. In fact, there is no way to use sizeof to obtain the
size of the allocated array. (Well, you could use sizeof(char[1024]),
but that's only loosely connected to the allocated object.)
If you want to remember how many bytes you've allocated with malloc(),
remember the argument you passed to it.
This doubts are related to deleting (freeing) the source string I pass
to a function like strtok(), which is going to modify it.
Keep in mind that a "string" is not a data type; it's a data format.
By definition, a string is "a contiguous sequence of characters
terminated by and including the first null character". An array can
*contain* a string, but it can't *be* a string. A pointer can point
to a string (more precisely, to the first character of a string, but
the standard specificallyi defines a "pointer to a string" as "a
pointer to its initial (lowest addressed) character").
Could someone clear my mind?
I hope I have.
Since comp.lang.c.moderated tends to have a very long moderation
latency, I'm sending this by e-mail as well as posting to the
newsgroup. If you want faster responses, try comp.lang.c (but watch
out for the trolls).
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.
- Prev by Date: Re: real newbie question on pointers
- Next by Date: Re: real newbie question on pointers
- Previous by thread: Re: real newbie question on pointers
- Next by thread: Re: string in an array
- Index(es):
Relevant Pages
|