<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Edwin has already pointed out the immediate error in your code.</p>
<p>However, there are a lot more problems. First some general
issues:</p>
<p>* you need to free the line buffer before returning from the
function, otherwise there will be a memory leak.</p>
<p>* AFAICT, the string buffer passed to atoi() is not
NULL-terminated. atoi() stops after encountering a non-numeric
character, so depending on the data that happens to be on stack,
this might appear to work - but it can also easily lead to wrong
results!</p>
<p>* The default line ending on Windows is CRLF (13 + 10) instead of
just LF (10), so depending on how the file was created, your code
might not work correctly.</p>
<p>Now some remarks about the actual code. IIUC, what you try to do
is read the content of the text file into a two-dimensional array.
Your approach seems to be the following: loop backwards character
by character and manually assemble the decimal numbers from
individual digits. First off, in this case there is need to call <font
face="monospace">atoi()</font> in the first place because you
already know the value of the digit. On the other hand, this is
completely unnecessary because<font face="monospace"> atoi() </font>can
parse <i>numbers </i>- not only digits! For example, <font
face="monospace">atoi("1024")</font> will output an integer
1024.</p>
<p>Unfortunately, <font face="monospace">atoi()</font> has a few
problems: it does not handle errors and it does not give you the
position after the number, so you need to manually search for
whitespace/newlines. <a moz-do-not-send="true"
href="https://cplusplus.com/reference/cstdlib/strtol/"
class="moz-txt-link-freetext">https://cplusplus.com/reference/cstdlib/strtol/</a>
solves both issues.</p>
<p>The most idiomatic way for reading a sequence of numbers from a
textfile in C would be <a moz-do-not-send="true"
href="https://cplusplus.com/reference/cstdio/sscanf/"
class="moz-txt-link-freetext">https://cplusplus.com/reference/cstdio/sscanf/</a>.
Your code could be rewritten as follows:<br>
</p>
<div>
<div style="color:rgb(0,0,0);font-family:Consolas,"Courier New",monospace;font-size:14px;line-height:19px;white-space:pre-wrap"><div><span style="color:rgb(0,0,255)">int</span> <span style="color:rgb(121,94,38)">readbarfile</span>(<span style="color:rgb(0,0,255)">int</span> <span style="color:rgb(0,16,128)">a</span><span style="color:rgb(0,0,255)">[]</span>[<span style="color:rgb(9,134,88)">8</span>], <span style="color:rgb(38,127,153)">FILE</span> *<span style="color:rgb(0,16,128)">f</span>) {</div><div> <span style="color:rgb(0,0,255)">int</span> row = 0;</div><div> <span style="color:rgb(0,0,255)">char</span> * <span style="color:rgb(0,16,128)">line</span> = <span style="color:rgb(0,0,255)">NULL</span>;</div><div> <span style="color:rgb(38,127,153)">size_t</span> <span style="color:rgb(0,16,128)">len</span> = <span style="color:rgb(9,134,88)">0</span>;</div><div>
</div><div> <span style="color:rgb(175,0,219)">while</span> (<span style="color:rgb(121,94,38)">getline</span>(&<span style="color:rgb(0,16,128)">line</span>, &<span style="color:rgb(0,16,128)">len</span>, <span style="color:rgb(0,16,128)">f</span>) != -<span style="color:rgb(9,134,88)">1</span>) {</div><div> int col, value, nread, pos = 0;
</div><div> for (col = 0; col < 4; col++) {</div><div> // We expect 1 converted element.</div><div> // (%n does not count, it only tells the current stream position.)
</div><div> if (sscanf(line + pos, "%d%n", &value, &nread) == 1) {</div><div> a[row][col] = value;</div><div> pos += nread;</div><div> } else {
</div><div> // handle conversion error</div><div> }</div><div> }</div><div> <span style="color:rgb(0,16,128)">row++;</span></div><div> }</div><div>
</div><div> free(line);
</div><div> <span style="color:rgb(175,0,219)">return</span> row; </div><div>}</div></div>
</div>
<p>Since you are dealing with a fixed column size, this could be
simplified further:</p>
<div>
<div style="color:rgb(0,0,0);font-family:Consolas,"Courier New",monospace;font-size:14px;line-height:19px;white-space:pre-wrap"><div><span style="color:rgb(0,0,255)">int</span> <span style="color:rgb(121,94,38)">readbarfile</span>(<span style="color:rgb(0,0,255)">int</span> <span style="color:rgb(0,16,128)">a</span><span style="color:rgb(0,0,255)">[]</span>[<span style="color:rgb(9,134,88)">8</span>], <span style="color:rgb(38,127,153)">FILE</span> *<span style="color:rgb(0,16,128)">f</span>) {</div><div> <span style="color:rgb(0,0,255)">int</span> row = 0, col;</div><div> <span style="color:rgb(0,0,255)">char</span> * <span style="color:rgb(0,16,128)">line</span> = <span style="color:rgb(0,0,255)">NULL</span>;</div><div> <span style="color:rgb(38,127,153)">size_t</span> <span style="color:rgb(0,16,128)">len</span> = <span style="color:rgb(9,134,88)">0</span>;</div><div>
</div><div> <span style="color:rgb(175,0,219)">while</span> (<span style="color:rgb(121,94,38)">getline</span>(&<span style="color:rgb(0,16,128)">line</span>, &<span style="color:rgb(0,16,128)">len</span>, <span style="color:rgb(0,16,128)">f</span>) != -<span style="color:rgb(9,134,88)">1</span>) {</div><div> // We expect 4 integer items. Actually, we can directly read into the output array</div><div> // because the data type (int) matches the %d specifier.
</div><div> if (sscanf(line, "%d%d%d%d", &a[row][0], &a[row][1], &a[row][2], &a[row][3]) != 4) {</div><div> // handle error</div><div> }</div><div> <span style="color:rgb(0,16,128)">row++;</span></div><div> }</div><div>
</div><div> free(line);
</div><div> <span style="color:rgb(175,0,219)">return</span> row; </div><div>}</div></div>
</div>
<p>Christof<br>
</p>
<div class="moz-cite-prefix">On 15.08.2022 08:29, Jaime Oliver
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CA+rqmj99Y0Vn-GDdeTQ4dxuKuvTAC0WWxN3NvvzO1B4eWw7+bg@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div dir="ltr">Hi Chris, Brad, All,
<div><br>
</div>
<div>I managed to trace the error to the function below. It
reads a text file and copies its contents to a matrix. The
file it's reading is always made of lines with the same number
of elements like this below:</div>
<div><br>
</div>
<div>0 4 4 8 32<br>
1 4 4 8 32<br>
2 4 4 8 32<br>
</div>
<div>...</div>
<div><br>
</div>
<div>The specific error I'm getting right now is that it is
reading that number 32 as 29. Again, this same code works fine
in all other OSs I've tried.</div>
<div><br>
</div>
<div>I'm assuming the issue is in the pow() function and all the
typecasting (int), (double) as Chris suggested?</div>
<div><br>
</div>
<div>As for compilation, I am using the latest pd_lib_builder as
the makefile. </div>
<div><br>
</div>
<div>Thanks for your help!</div>
<div><br>
</div>
<div>best,</div>
<div><br>
</div>
<div>Jaime</div>
<div><br>
</div>
<div>code:</div>
<div><br>
</div>
<div>
<div style="color:rgb(0,0,0);font-family:Consolas,"Courier New",monospace;font-size:14px;line-height:19px;white-space:pre-wrap"><div><span style="color:rgb(0,0,255)">int</span> <span style="color:rgb(121,94,38)">readbarfile</span>(<span style="color:rgb(0,0,255)">int</span> <span style="color:rgb(0,16,128)">a</span><span style="color:rgb(0,0,255)">[]</span>[<span style="color:rgb(9,134,88)">8</span>], <span style="color:rgb(38,127,153)">FILE</span> *<span style="color:rgb(0,16,128)">f</span>) {</div><div> <span style="color:rgb(0,0,255)">int</span> <span style="color:rgb(0,16,128)">i</span>, <span style="color:rgb(0,16,128)">ii</span>, <span style="color:rgb(0,16,128)">j</span>, <span style="color:rgb(0,16,128)">jj</span>, <span style="color:rgb(0,16,128)">strsize</span>, <span style="color:rgb(0,16,128)">temp</span>;</div><div> <span style="color:rgb(0,0,255)">char</span> * <span style="color:rgb(0,16,128)">line</span> = <span style="color:rgb(0,0,255)">NULL</span>;</div><div> <span style="color:rgb(38,127,153)">size_t</span> <span style="color:rgb(0,16,128)">len</span> = <span style="color:rgb(9,134,88)">0</span>;</div><div> <span style="color:rgb(38,127,153)">ssize_t</span> <span style="color:rgb(0,16,128)">read</span>;</div><div> <span style="color:rgb(0,0,255)">char</span> <span style="color:rgb(0,16,128)">ss</span>[<span style="color:rgb(9,134,88)">10</span>];</div><div> <span style="color:rgb(0,16,128)">temp</span>=<span style="color:rgb(0,16,128)">j</span>=<span style="color:rgb(9,134,88)">0</span>;</div>
<div> <span style="color:rgb(0,16,128)">ii</span>=<span style="color:rgb(9,134,88)">0</span>;</div><div> <span style="color:rgb(175,0,219)">while</span> ((<span style="color:rgb(0,16,128)">read</span> = <span style="color:rgb(121,94,38)">getline</span>(&<span style="color:rgb(0,16,128)">line</span>, &<span style="color:rgb(0,16,128)">len</span>, <span style="color:rgb(0,16,128)">f</span>)) != -<span style="color:rgb(9,134,88)">1</span>) {</div><div> <span style="color:rgb(0,16,128)">jj</span> = <span style="color:rgb(9,134,88)">4</span>;</div><div> <span style="color:rgb(0,16,128)">strsize</span> = (<span style="color:rgb(0,0,255)">int</span>) <span style="color:rgb(0,16,128)">read</span>;</div><div> <span style="color:rgb(175,0,219)">for</span> (<span style="color:rgb(0,16,128)">i</span>=<span style="color:rgb(0,16,128)">strsize</span>-<span style="color:rgb(9,134,88)">1</span>; <span style="color:rgb(0,16,128)">i</span>>=<span style="color:rgb(9,134,88)">0</span>; <span style="color:rgb(0,16,128)">i</span>--){</div><div> <span style="color:rgb(175,0,219)">if</span> ( <span style="color:rgb(0,16,128)">line</span>[<span style="color:rgb(0,16,128)">i</span>] == (<span style="color:rgb(0,0,255)">int</span>) <span style="color:rgb(9,134,88)">32</span> || <span style="color:rgb(0,16,128)">line</span>[<span style="color:rgb(0,16,128)">i</span>] == (<span style="color:rgb(0,0,255)">int</span>) <span style="color:rgb(9,134,88)">10</span>) { //space or newline</div><div> <span style="color:rgb(175,0,219)">if</span>(<span style="color:rgb(0,16,128)">i</span> != (<span style="color:rgb(0,16,128)">strsize</span>-<span style="color:rgb(9,134,88)">1</span>)) { </div><div> <span style="color:rgb(0,16,128)">a</span>[<span style="color:rgb(0,16,128)">ii</span>][<span style="color:rgb(0,16,128)">jj</span>] = <span style="color:rgb(0,16,128)">temp</span>;</div><div> <span style="color:rgb(0,16,128)">jj</span>--;</div><div> }</div><div> <span style="color:rgb(0,16,128)">j</span>=<span style="color:rgb(9,134,88)">0</span>;</div><div> <span style="color:rgb(0,16,128)">temp</span>=<span style="color:rgb(9,134,88)">0</span>;</div><div> }</div><div> <span style="color:rgb(175,0,219)">else</span> {</div><div> <span style="color:rgb(0,16,128)">ss</span>[<span style="color:rgb(9,134,88)">0</span>] = <span style="color:rgb(0,16,128)">line</span>[<span style="color:rgb(0,16,128)">i</span>];</div><div> <span style="color:rgb(0,16,128)">temp</span> += <span style="color:rgb(121,94,38)">atoi</span>(<span style="color:rgb(0,16,128)">ss</span>)*( (<span style="color:rgb(0,0,255)">int</span>) <span style="color:rgb(121,94,38)">pow</span>((<span style="color:rgb(0,0,255)">double</span>)<span style="color:rgb(9,134,88)">10</span>, (<span style="color:rgb(0,0,255)">double</span>)<span style="color:rgb(0,16,128)">j</span>) );</div><div> <span style="color:rgb(0,16,128)">j</span>++;</div><div> } </div><div> }</div><div> <span style="color:rgb(0,16,128)">ii</span>++;</div><div> }</div><div> <span style="color:rgb(175,0,219)">return</span> (<span style="color:rgb(0,16,128)">ii</span>); </div><div>}</div></div>
</div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Fri, Aug 12, 2022 at 1:57
PM Chris Clepper <<a href="mailto:cgclepper@gmail.com"
target="_blank" moz-do-not-send="true"
class="moz-txt-link-freetext">cgclepper@gmail.com</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div dir="ltr">
<div>That issue only relates to data corruption in the case
of major failures like power loss and kernel panics. In
most of those situations some data loss is not only
expected but also the least of your concerns.</div>
<div><br>
</div>
<div>As for the original topic, the first thing to check is
the usual problems moving between architectures like
endianess, definition of data structures (is long really
32 bits, double 64 bits, etc), memory alignment and
compiler settings. Does the code work with all of the
optimizations turned off?<br>
</div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Fri, Aug 12, 2022 at
5:58 AM Bastiaan van den Berg <<a
href="mailto:buzz@spacedout.nl" target="_blank"
moz-do-not-send="true" class="moz-txt-link-freetext">buzz@spacedout.nl</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left:1px solid
rgb(204,204,204);padding-left:1ex">
<div dir="ltr">Did you read that M1's storage has so much
cache + lies to the OS about cache commitments, leading
to data corruption sometimes? <a
href="https://twitter.com/marcan42/status/1494213855387734019"
target="_blank" moz-do-not-send="true"
class="moz-txt-link-freetext">https://twitter.com/marcan42/status/1494213855387734019</a><br>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Fri, Aug 12, 2022
at 6:14 AM Jaime Oliver <<a
href="mailto:jaime.oliver2@gmail.com"
target="_blank" moz-do-not-send="true"
class="moz-txt-link-freetext">jaime.oliver2@gmail.com</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px
0px 0.8ex;border-left:1px solid
rgb(204,204,204);padding-left:1ex">
<div dir="auto">Dear all,
<div dir="auto"><br>
</div>
<div dir="auto">I have a c external that compiles
and runs fine on windows, Linux, and Mac Intel
systems, but while the exact same code compiles ok
on a Mac M1 system, it runs with errors.</div>
<div dir="auto"><br>
</div>
<div dir="auto">I am trying to figure out the bug,
but wonder if anyone has come across something
like this? The external itself is not particularly
complex. It writes and reads text files, does
basic arithmetic, and uses arrays of various
sizes.</div>
<div dir="auto"><br>
</div>
<div dir="auto">Does anyone have any suggestions of
where to look first?</div>
<div dir="auto"><br>
</div>
<div dir="auto">Best,</div>
<div dir="auto"><br>
</div>
<div dir="auto">Jaime</div>
</div>
_______________________________________________<br>
<a href="mailto:Pd-list@lists.iem.at" target="_blank"
moz-do-not-send="true" class="moz-txt-link-freetext">Pd-list@lists.iem.at</a>
mailing list<br>
UNSUBSCRIBE and account-management -> <a
href="https://lists.puredata.info/listinfo/pd-list"
rel="noreferrer" target="_blank"
moz-do-not-send="true" class="moz-txt-link-freetext">https://lists.puredata.info/listinfo/pd-list</a><br>
</blockquote>
</div>
_______________________________________________<br>
<a href="mailto:Pd-list@lists.iem.at" target="_blank"
moz-do-not-send="true" class="moz-txt-link-freetext">Pd-list@lists.iem.at</a>
mailing list<br>
UNSUBSCRIBE and account-management -> <a
href="https://lists.puredata.info/listinfo/pd-list"
rel="noreferrer" target="_blank" moz-do-not-send="true"
class="moz-txt-link-freetext">https://lists.puredata.info/listinfo/pd-list</a><br>
</blockquote>
</div>
_______________________________________________<br>
<a href="mailto:Pd-list@lists.iem.at" target="_blank"
moz-do-not-send="true" class="moz-txt-link-freetext">Pd-list@lists.iem.at</a>
mailing list<br>
UNSUBSCRIBE and account-management -> <a
href="https://lists.puredata.info/listinfo/pd-list"
rel="noreferrer" target="_blank" moz-do-not-send="true"
class="moz-txt-link-freetext">https://lists.puredata.info/listinfo/pd-list</a><br>
</blockquote>
</div>
<br clear="all">
<div><br>
</div>
-- <br>
<div dir="ltr">
<div dir="ltr">**********************
<div>Jaime E Oliver LR<br>
<a href="http://www.jaimeoliver.pe" target="_blank"
moz-do-not-send="true">www.jaimeoliver.pe</a><br>
<br>
</div>
</div>
</div>
<br>
<fieldset class="moz-mime-attachment-header"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
<a class="moz-txt-link-abbreviated" href="mailto:Pd-list@lists.iem.at">Pd-list@lists.iem.at</a> mailing list
UNSUBSCRIBE and account-management -> <a class="moz-txt-link-freetext" href="https://lists.puredata.info/listinfo/pd-list">https://lists.puredata.info/listinfo/pd-list</a>
</pre>
</blockquote>
</body>
</html>