Question

I have been trying to understand what is malloc() and why is it used. I understand that malloc is for dynamic allocation of memory, it is needed if you don't know how much memory you wan't to create. I have been doing some hands-on on it.

The following code declares an array of character pointers, and 1st character pointer is initialized with "hello". This works fine.

int main()
{


char *strarray[5];
strarray[0]="hello";
printf("%s\n",strarray[0]);
return 0;
}

But if i try to use strcpy() function to copy "hello" string into strarray[0] (without malloc()) it gives a problem. And it goes into some loop and does not copy the string. And it works fine if i use malloc to allocate memory.

int main()
{

char *strarray[5];
//strarray[0]=(char *)malloc(sizeof(char)*10);
strcpy(strarray[0],"hello");
printf("%s\n",strarray[0]);
return 0;
}

I want to know what makes the difference? If i can initialize "hello" to a char pointer for which malloc is not used, why can't i do the same with strcpy().

Was it helpful?

Solution

char *strarray[5];

will allocate array of five pointers to the characters (which can be interpreted as strings).

What this will not allocate is the memory space to place these five strings to. So, if you want to make all pointers to point to the different strings, you need to allocate them yourself.

However, as these are pointers, you don't really need to allocate anything, if you don't want to. That is why your first example is working. strarray[0]="hello"; will make the first element of the array to point to the location where "hello" string literal is stored (the compiler will put this literal somewhere in the memory for you, so your pointer will point to the valid memory location).

To sum up, in the first example, your pointer is pointing to the memory already allocated and initialized with "hello" (compiler did this for you) - you're not copying the string, you're just assigning value to the pointer. In the second example your pointer is pointing to some undefined location, which you didn't explicitly reserved, but you're trying to write into it.

OTHER TIPS

This is wrong: char *strarray[5]; This is stack-allocating 5 char* pointers. I think you wanted to write char strarray[6], which creates 6 chars on the stack. That can accommodate "hello" and its implicit NULL-terminator (the NULL terminator tells C functions that a string has ended).

Having written char strarray[6];, strcpy(strarray, "hello"); is safe. Then you can call:

printf("%s\n",strarray);

where I have dropped the [0]: the %s expects a char* not a char: strarray[0] is a char type.

You use malloc to allocate memory on the heap. Might I suggest you read Kernighan and Ritchie (C Programming Language) for more details on that?

malloc(): Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.

void* malloc (size_t size);  

size_t is an unsigned int type.

it is needed if you don't know how much memory you wan't to create,this is not correct.
you should know how much minimum memory required.

with the help of malloc() you can allocating memory at run time not at compile time.

strarray[0]="hello";   
//here you need not to allocate memory as this is an assignment 

You need to allocate some space before using a pointer as destination string in strcpy()

strcpy() Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).

the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.

for this you need to allocate Memory first.

strcpy itself doesn't allocate memory for the destination string

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top