Dear Guys,

I want somebody to explain this behaviour. I am using devC++ IDE and I had problem with the following code snippet. I rectified it, but i need to know the correct answer why its solved?.

I declared a temporary character pointer (tmp) in main and used it directly in strcpy function to copy contents of a string, str, that is

int main(){
char str[] = "I Want To Know";
char *tmp;
strcpy(tmp,str);
return 0;
}

The above code works fine. But when I tried to do the same in a function, I started getting run time error. The function is defined as follows:

void someStrFunc(const char *str){
 
char *tmp;
strcpy(tmp,str);
 
return;
}

However, this is solved by using the following code,

void someStrFunc(const char *str){
 
char *tmp;
tmp = (char*)malloc(sizeof(strlen(str)));
strcpy(tmp,str);
 
return;
}

I want to know why is it not required to allocate the memory for tmp in main() or other way, why is it required to allocate the memory for tmp in the function someStrFunc.

Regards,
V.Amar

It's because in the code

void someStrFunc(const char *str){
 
    char *tmp;
    strcpy(tmp,str);
 
    return;
}

tmp is just a pointer and has no space assigned to it. Therefore you are copying the string into unknown memory outside of your program space.


The same thing is happening with

int main(){
    char str[] = "I Want To Know";
    char *tmp;
    strcpy(tmp,str);
    return 0;
}

but the unknown memory is part of your program space. You're still overwriting memory.

> I declared a temporary character pointer (tmp) in main and used it directly in strcpy function
> But when I tried to do the same in a function, I started getting run time error.
This is your first lesson in getting to grips with the adage "working is not the same a bug-free".

C gives you plenty of rope to hang yourself with, though it seldom snaps tight in such a small program. As you add more code, the probability of failure increases, but there is never any guarantee that any given mistake will immediately cause a failure. Some bugs like this can remain hidden for months or years.

> tmp = (char*)malloc(sizeof(strlen(str))); This too was wrong, despite your "it works" statement.
1. sizeof(strlen(str)) is essentially sizeof(size_t) (the type strlen returns). So the amount you allocate has NOTHING to do with the length of the input string.
2. Even if you omit that, then strlen(str) by itself isn't enough, since that doesn't count the \0 at the end of the string (which gets copied as well).
3. Do not cast malloc in C programs.
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1047673478&id=1043284351
At best, it does nothing useful in a correct C program, and potentially hides some serious problems in bad C programs. tmp = malloc ( strlen(str) + 1 );

Thanks for you reply.

But still I am not clear. Can you provide me any information regarding "Program Space". Are you saying about stack/heap ?
Guide me.

Regards,
V.Amar

Where the memory comes from doesn't matter.
That your tmp pointer points to some memory does.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.