Note that there are some explanatory texts on larger screens.

plurals
  1. POC pointer address changes without assignment
    primarykey
    data
    text
    <p>I am working on a Uni assignment here, and I've run into a problem. I am attempting to store a string input at a point inside a struct using a for-loop. Later on I intend to use the pointer to the place where the data was stored to fetch the string. Now the problem is, as I move on inside my for-loop, the address of the point changes as well. This code:</p> <pre><code>printf("B: %p\n", txt-&gt;point); for(i = 0; i &lt; input_sz; i++) { txt-&gt;point[i] = input[i]; } printf("A: %p\n", txt-&gt;point); </code></pre> <p>gives the output:</p> <pre><code>B: 0x7fc111803200 A: 0x7fc111803265 </code></pre> <p>where B is before-value and A is after-copying value.</p> <p>Any help debugging this would be very appreciated!</p> <hr> <p>EDIT: Here's some more code:</p> <p>The struct:</p> <pre><code>struct text_storage { char* start; char* point; char* end; } typedef text_t; </code></pre> <p>Initialization function:</p> <pre><code>text_t* text_init(void *memory, size_t size) { text_t* to_return; if(size &lt; sizeof(text_t)) { return NULL; } to_return = (text_t*) memory; to_return-&gt;start = to_return; to_return-&gt;end = to_return-&gt;start + size; to_return-&gt;point = to_return-&gt;start; printf("Start: %p, point: %p, end: %p, end-start: %d\n", to_return-&gt;start, to_return-&gt;point, to_return-&gt;end, (to_return-&gt;end - to_return-&gt;start)); return to_return; } </code></pre> <p>The text-store method in which the error occurs:</p> <pre><code>int text_store_entry(text_t *txt, const char *input, size_t input_sz) { int to_return; char* begin = txt-&gt;point; int i; if(input_sz &gt; (txt-&gt;end - txt-&gt;point)) { return -1; } printf("Start: %p, point: %p, end: %p, end-start: %d\n", txt-&gt;start, txt-&gt;point, txt-&gt;end, (txt-&gt;end - txt-&gt;start)); printf("B: %p\n", txt-&gt;point); for(i = 0; i &lt; input_sz; i++) { txt-&gt;point[i] = input[i]; } printf("A: %p\n", txt-&gt;point); } </code></pre> <p>Main-function (testing purposes only):</p> <pre><code>int main(int argc, char* argv[]) { void* memory = malloc(10000); char* a = "hei pa deg din trekkbasun"; text_t* txt; int memoverwritten; txt = text_init(memory, 10000); memoverwritten = text_store_entry(txt, a, (size_t)26); printf("got through\n"); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload