Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

char x[256] vs. char* = malloc(256*sizeof(char));

Tags:

arrays

c

malloc

Someone here recently pointed out to me in a piece of code of mine I am using

char* name = malloc(256*sizeof(char));
// more code
free(name);

I was under the impression that this way of setting up an array was identical to using

char name[256];

and that both ways would require the use of free(). Am I wrong and if so could someone please explain in low level terms what the difference is?

like image 588
faceless1_14 Avatar asked Jan 07 '09 02:01

faceless1_14


People also ask

Does char * need malloc?

As was indicated by others, you don't need to use malloc just to do: const char *foo = "bar"; The reason for that is exactly that *foo is a pointer — when you initialize foo you're not creating a copy of the string, just a pointer to where "bar" lives in the data section of your executable.

What is the size of malloc?

The malloc() function reserves a block of storage of size bytes. Unlike the calloc() function, malloc() does not initialize all elements to 0. The maximum size for a non-teraspace malloc() is 16711568 bytes.

How much memory is allocated for a char?

Dynamic arrays is a popular name given to a series of bytes allocated on the heap. char *ptr = (char*) malloc(10); allocates a memory of 10 bytes on heap and we have taken the starting address of this series of bytes in a character pointer ptr.


2 Answers

There is a third possibility here, which is that the array can be declared external to a function, but statically, eg,

// file foo.c
char name[256];

int foo() {
    // do something here.
}

I was rather surprised in answers to another question on SO that someone felt this was inappropriate in C; here's it's not even mentioned, and I'm a little confused and surprised (like "what are they teaching kids in school these days?") about this.

If you use this definition, the memory is allocated statically, neither on the heap nor the stack, but in data space in the image. Thus is neither must be managed as with malloc/free, nor do you have to worry about the address being reused as you would with an auto definition.

It's useful to recall the whole "declared" vs "defined" thing here. Here's an example

/* example.c */

char buf1[256] ;           /* declared extern, defined in data space */
static char buf2[256] ;    /* declared static, defined in data space */
char * buf3 ;              /* declared extern, defined one ptr in data space */
int example(int c) {       /* c declared here, defined on stack */
    char buf4[256] ;       /* declared here, defined on stack   */
    char * buf5 = malloc(256)]   /* pointer declared here, defined on stack */
                           /* and buf4 is address of 256 bytes alloc'd on heap */
    buf3 = malloc(256);    /* now buf3 contains address of 256 more bytes on heap */

    return 0;              /* stack unwound; buf4 and buf5 lost.      */
                           /* NOTICE buf4 memory on heap still allocated */
                           /* so this leaks 256 bytes of memory */
}

Now in a whole different file

/* example2.c */

extern char buf1[];             /* gets the SAME chunk of memory as from example.c */
static char buf2[256];          /* DIFFERENT 256 char buffer than example.c */
extern char * buf3 ;            /* Same pointer as from example.c */
void undoes() {
     free(buf3);                /* this will work as long as example() called first */
     return ;
}
like image 55
Charlie Martin Avatar answered Sep 17 '22 13:09

Charlie Martin


In the first code, the memory is dynamically allocated on the heap. That memory needs to be freed with free(). Its lifetime is arbitrary: it can cross function boundaries, etc.

In the second code, the 256 bytes are allocated on the stack, and are automatically reclaimed when the function returns (or at program termination if it is outside all functions). So you don't have to (and cannot) call free() on it. It can't leak, but it also won't live beyond the end of the function.

Choose between the two based on the requirements for the memory.

Addendum (Pax):

If I may add to this, Ned, most implementations will typically provide more heap than stack (at least by default). This won't typically matter for 256 bytes unless you're already running out of stack or doing heavily recursive stuff.

Also, sizeof(char) is always 1 according to the standard so you don't need that superfluous multiply. Even though the compiler will probably optimize it away, it makes the code ugly IMNSHO.

End addendum (Pax).

like image 34
Ned Batchelder Avatar answered Sep 17 '22 13:09

Ned Batchelder