Reputation: 2226 Grandmaster
- Active Posts:
- 3,442 (2.4 per day)
- 21-June 11
- Profile Views:
- Last Active:
- Today, 02:35 AM
- OS Preference:
- Favorite Browser:
- Who Cares
- Favorite Processor:
- Who Cares
- Favorite Gaming Platform:
- Who Cares
- Your Car:
- Who Cares
- Dream Kudos:
- Expert In:
- C/C++, Functional Programming, Java, Python, Computer Science
Posts I've Made
Posted 25 May 2015Happenstance. Accessing memory out of bounds invokes undefined behavior, so anything might happen. What seems to have happened in your case is that the call to system access memory on the heap and overwrites the out-of-bounds contents of out, so what should be "25\0" ends up "2garbage". The call to printf seems to somehow cause system to allocate its memory elsewhere.
Perhaps printf allocates some memory itself, but does not write to it, causing the contents to stay unchanged, but causing system to get a different piece of memory. Or perhaps printf's memory needs to start at a later address for alignment or similar reasons and then system's memory also needs to start later because there's not enough consecutive memory left at the earlier place.
Either way printf likely does something related to memory allocation, which causes system to get memory at a different address than it otherwise would causing it to not overwrite out. At least that's the most likely explanation I can think of. Either way it's all undefined behavior.
PS: You might consider sprintf to generate your string. That seems simpler that your use of your own conversion function and strcat.
Posted 25 May 2015You're not allocating enough memory for the string. 15+strlen(cols)+strlen(lin) is enough to store cols, lin, 14 characters and the null byte. "mode CON:cols=" is 14 characters and " lines=" is another 7.
Posted 20 May 2015Sure, just add a print statement after you set thisMid.
Posted 20 May 2015done it and this is what i get.
You mean that's what you get when you do it by hand/in your head, i.e. what you should get, right? I think Jon meant that you should look into what you actually get when you run your code (by printing the values of the variables). Because that's definitely not what you get in your actual code.
As another hint: On which variable(s) does the value that you calculate for thisMid depend? So under which circumstances would the value change? Does that ever happen?
Quotebut i think is something about the variables. i remember reading something like, if a function changes a variable its a global change
No, variables you set in functions are local unless you explicitly use one of the global or nonlocal keywords.
Posted 19 May 2015i create a file fct.hs and put it in bin ( C:\Program Files\Haskell Platform\2014.2.0.0\bin)
You shouldn't put your source files in the Haskell installation directory. It shouldn't hurt anything, but it just doesn't make any sense (especially since I imagine you need admin rights to write in that directory). Usually you'd put your source code somewhere in your home folder.
Quoteto call the function should i just write the commande :l fct ?
To call that function from ghci, you need to make sure that you're in the right directory (either by starting ghci from that directory or by using :cd inside ghci), then use :l fct and then actually call the function you want to call (by writing fact 5 for example, depending on what the name of the function is and with which argument you want to call it).
Quotei found this commande to build the file : ghc --make name.hs ,but in unix, and if in windows name.exe ; i dont get it; should i write ghc --make name.exe or directly name.exe, what im i doing wrong?
ghc --make name.hs should work fine (though obviously you should replace "name.hs" by "fct.hs" if that's the name of your Haskell file). If that does not work, what error message do you get?