Re: string in an array



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



Relevant Pages

  • Re: "Mastering C Pointers"....
    ... A pointer is a kind of variable that can "point to" some object. ... has a type (pointer to int), and a value of some kind. ... You may know that you can access these integers by using array notation ... The function will take one argument, a string, and will return the length ...
    (comp.lang.c)
  • Re: copy a string into a 2d array of chars
    ... This split function should allocate a 2D array of chars ... >focus the program the string is not actually split. ... later) is an array of char containing the original contents of the ... The i-th pointer will contain the starting address of the ...
    (comp.lang.c)
  • Re: new IL: C (sort of...).
    ... C doesn't need a string type... ... variant of PL/1 which was very Pascal-ish. ... - C does implement an array declaration. ... effectively converted into a pointer that can be used with the offset ...
    (comp.lang.misc)
  • Re: Arrays of Strings, malloc
    ... Each string is at most 50 characters long, ... How do I actually start the array? ... an "array of pointers" and a "pointer ... Allocate space for some number of pointers to char ...
    (comp.lang.c)
  • Re: HEXADECIMAL to STRING
    ... suppose that I have a string that is an hexadecimal number, ... void print_hex(unsigned char *bs, unsigned int n){ ... The name of the function is misleading, since the input is not a hexadecimal array, but a binary array. ... Without any comment to guide me, I assume that it is a pointer to an existing array to be used for the output. ...
    (comp.lang.c)