<div dir="ltr">You printed &j and &solutions - did you mean to print 'solutions' instead of '&solutions' Because 'solutions' and 'j' are both local variables in distinct function calls ('ok' is called, then 'nqueens' is called - so they can both use/reuse the same stack space for their local variables).</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Nov 20, 2019 at 1:16 PM Mohammad Norouzi via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</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>Hi,</div><div><br></div><div>Could anyone please help me understand why Clang reallocates the same memory address for different variables while their lifetime intersect?</div><div><br></div><div>Here is an example code:</div><div><br></div><div><br>#include <stdlib.h><br>#include <stdio.h><br>#include <memory.h><br>#include <alloca.h><br><br><br>/* Checking information */<br><br>static int solutions[] = {<br> 1,<br> 0,<br> 0,<br> 2,<br> 10, /* 5 */<br> 4,<br> 40,<br> 92,<br> 352,<br> 724, /* 10 */<br> 2680,<br> 14200,<br> 73712,<br> 365596,<br>};<br>#define MAX_SOLUTIONS sizeof(solutions)/sizeof(int)<br><br>int total_count;<br>int sharedVar = 0;<br><br>int ok(int n, char *a)<br>{<br> int i, j;<br> char p, q;<br> printf("jjjjjjjjj: %d, %p\n", n,&j);<br> for (i = 0; i < n; i++) {<br> p = a[i];<br> <br><br> for (j = i + 1; j < n; j++) {<br> q = a[j];<br> if (q == p || q == p - (j - i) || q == p + (j - i))<br> return 0;<br> }<br> }<br> return 1;<br>}<br><br>void nqueens (int n, int j, char *a, int *solutions)<br>{<br> int i,res;<br> sharedVar = sharedVar * j - n;<br> if (n == j) {<br> /* good solution, count it */<br> *solutions = 1;<br> return;<br> }<br>printf("solutions: %d, %p\n", j, &solutions);<br> *solutions = 0;<br><br> /* try each possible position for queen <j> */<br> for (i = 0; i < n; i++) {<br> a[j] = (char) i;<br> if (ok(j + 1, a)) {<br> nqueens(n, j + 1, a,&res);<br> *solutions += res;<br> }<br> }<br>}<br><br>int main()<br>{<br> int size = 3;<br> char *a;<br>// printf("total_count: %p\n", &total_count);<br> total_count=0;<br> a = (char *)alloca(size * sizeof(char));<br> printf("Computing N-Queens algorithm (n=%d) ", size);<br> sharedVar = -5;<br> nqueens(size, 0, a, &total_count);<br> printf("completed!\n");<br> printf("sharedVar: %d\n", sharedVar);<br>}<br></div><div><br></div><div><br></div><div><br></div><div>When I compile the program with clang -O0 variable j in function ok has the same memory address as th variable solutions in function nqueens.</div><div><br></div><div>However, compiling it with gcc, they have different memory addresses.</div><div><br></div><div>Any help is appreciated!</div><div><br></div><div>Best Regards,</div><div>Mohammad<br></div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>