What's wrong with this C code?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *nome="mario";
nome=realloc(nome,10*sizeof(char));
strcpy(nome,"valentino");
printf("%s",nome);
return(0);
}
>>51791279
>mario
MAMMA MIA! SCENDI CHE LA PASTA SI FREDDA!!
>>51791291
Expected! :) Can you help me?
"valentino" > "mario"
>>51791324
But I did the realloc before.
>>51791356
void* realloc (void* ptr, size_t size);
ptr - Pointer to a memory block previously allocated with malloc, calloc or realloc.char* nome = malloc(5);
strcpy(nome, "mario");
nome = realloc(nome, 12);
strcpy(nome, "valentino");
As >>51791390 hinted at, you can only realloc() pointers that you got from alloc() in the first place.
>>51791390
Doesn't this code:
char *nome="mario";
equals this one:
char *nome;
nome=malloc(5*sizeof(char));
strcpy(nome,"mario");
>>51791390
>>51791422
So, what does, exactly, this code do?
char *nome="mario";
It creates a var named 'nome' and whose type is char*. And after that?
>>51791390
dont do this
if realloc fails you have a memory leak
>>51791436
It points to a piece of memory that is part of the loaded program. Which is readonly, and definitely not malloc()ed.
>>51791424
>>51791436
When you type char *nome it allocates memory for a char pointer and you assign it something likechar *nome = "mario"
it creates a read-only string constant in the memory and nome becomes a pointer to that string, so it's different than malloc-ing for nome and doing strcpy
>>51791438
obviously, but I that's not the point here
>>51791436
here's the assembly output
>>51791458
>>51791453
So if I do, for example:
char *nome="mario";
nome=malloc(10*sizeof(int));
strcpy(nome,"valentino");
The part of memory allocated in the 1st line (read-only), can't be free-ed in any way (neither by the user nor by the system, until the program ends), right? So, it's a memory-leakage here too?
>>51791474
And the assembly output for >>51791390
Notice there's no declaration for strings in LC0
>>51791438
The memory leakage can be avoided like this:
char *nome, *tmp;
nome=strdup("mario");
tmp=nome;
nome=realloc(10*sizeof(char));
if (nome!=NULL && nome!=tmp) free(tmp);
Right?
nome = "mario"
nome = "valentino"
print nome
Use a better programming language.
>>51791525
I'm used to doing it like thistemp = realloc(nome, 12);
if(temp) {
nome = temp;
}
>>51791504
>The part of memory allocated in the 1st line (read-only), can't be free-ed in any way (neither by the user nor by the system, until the program ends), right?
Right.
>So, it's a memory-leakage here too?
Well, that's an interesting philosophical question, but it's not usually considered one. The string "mario" is part of the program code, along with all the machine language making up the actual compiled instructions; it gets unloaded together with said machine code. Practically speaking, it doesn't "leak" any more than the machine code does.
The same holds for the string "valentino", by the way. The first one, that is; the statementstrcpy(nome,"valentino");takes the chunk of read-only program memory containing the string "valentino", and copies it into the newly allocated 10-byte chunk of memory. Of those, the former never goes away until the program ends.
>>51791559
Thank you!
>>51791595
But the former can be overwritten? I mean that part of memory can be used by some other variable/process?
>>51791728
>But the former can be overwritten? I mean that part of memory can be used by some other variable/process?
No. It stays there in readonly form indefinitely, until the process ends.
>>51791755
Alright! Then....thank you, everybody! ^__^
Greetings from Italy! :=D
>>51791474
>>51791510
holy shit x86 asm is fucking awful
LET ME FINISH MAH PEPPERONATA
>>51792121
ahahahah :D
Gotta go eat some pizza :)