User forums > Help
Codeblocks showing wrong output with memmove().
Gobtotem:
So i ran the below code on codeblocks and it showed 11234 with both memmove() while the second one should be 12344.I ran the programme on gcc on linux terminal and it showed 12344 for the second memmove() which is correct. The compiler i am using for codeblocks is gcc which i ran on the linux terminal and it gave the correct output.So don't know what is the problem here.
OUTPUT:- https://imgur.com/a/vy8EdL7
CODE:-
--- Code: ---#include<stdio.h>
#include<string.h>
int main()
{
char array[5]={1,2,3,4,5};
memmove(&array[1],&array[0],4);
for(int i=0;i<5;i++)
{
printf("%d",array[i]);
}
printf("\n");
memmove(&array[0],&array[1],4);
for(int i=0;i<5;i++)
{
printf("%d",array[i]);
}
printf("\n");
}
--- End code ---
BUILD LOG
--- Code: ---
-------------- Clean: Release in test (compiler: GNU GCC Compiler)---------------
Cleaned "test - Release"
-------------- Build: Release in test (compiler: GNU GCC Compiler)---------------
gcc -Wall -O2 -Wfatal-errors -Wextra -Wall -std=c11 -c /home/titan/Desktop/C_source/test/main.c -o obj/Release/main.o
g++ -o bin/Release/test obj/Release/main.o -s
Output file is bin/Release/test with size 14.03 KB
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))
--- End code ---
VERSION INFO:- CODEBLOCKS:- Release 17.12 rev 11256 Sep 3 2018, 07:29:05 - wx3.0.4 (Linux, unicode) - 64 bit
GCC:-gcc (GCC) 8.2.1 20180831
LINUX:-4.18.9-arch1-1-ARCH
BlueHazzard:
--- Quote ---So i ran the below code on codeblocks
--- End quote ---
No, you run it on your operating system. Codeblocks is only a editor, it can not run, or compile code...
Codeblocks does not influence the output of your program.
So my suggestion is that you have simply not build the code and run a older version of your application? Remember, the green arrow only runs the program and does not check if it has to be build first!
Have you tried Build->Rebuild?
Gobtotem:
--- Quote from: BlueHazzard on November 27, 2018, 09:10:05 am ---So my suggestion is that you have simply not build the code and run a older version of your application? Remember, the green arrow only runs the program and does not check if it has to be build first!
Have you tried Build->Rebuild?
--- End quote ---
I guess it is cause of the O2 optimistion as i asked someone else too and they are able to reproduce it because of the O2 flag in gcc 8.x and 9 . So dont know much i guess it is cause of gcc and not codeblocks i guess. I tried running it in on my linux terminal with gcc -Wall -O2 -Wfatal-errors -Wextra -Wall -std=c11 test. c and even it shows 11234 now and without O2 flag it showed the correct output.
BlueHazzard:
Thank you for reporting back!
Krice:
There is no need to write code like that, because if it sounds like undefined behavior, it's going to be it at least with compiler optimizations. I don't get what is the point trying funny stuff like that. If you want to find UB features in compiler you will get what you want. But if you want to write working programs you don't use memmove or anything like that (C memory "management" functions) at all and then continue with programming as usual.
Navigation
[0] Message Index
[#] Next page
Go to full version