[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
simple code not working
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 25
Thread images: 3
File: err.png (47 KB, 1569x887) Image search: [Google]
err.png
47 KB, 1569x887
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 like
char *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
>>
File: ass.png (15 KB, 548x777) Image search: [Google]
ass.png
15 KB, 548x777
>>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?
>>
File: Untitled.png (34 KB, 553x956) Image search: [Google]
Untitled.png
34 KB, 553x956
>>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 this
temp = 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 statement
strcpy(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 :)
Thread replies: 25
Thread images: 3

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.