Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneric stack in plain C does not copies structures correct
    text
    copied!<p>So I have implemented a generic stack in Plain C. It should copy different type of data, inclusive structures. And by structures I have the problem.</p> <p>So here's the structure of the stack:</p> <pre><code>/* * Definite genStack as a structure. * Pointer elems points to the objects lying on the stack * The variable elemSize spiecifies the size of an element * The variable logLength specifies the number of actually * lying on the stack objects * The variable allocLenght specifies the allocated size */ typedef struct{ void* elems; int elemSize; int logLength; int allocLength; }genStack; </code></pre> <p>Push and pop functions:</p> <pre><code>void GenStackPush(genStack *s, const void *elemAddr) { /* if stack is full - allocates more memory */ if (GenStackFull(s)) { GenStackAlloc(s, s-&gt;elemSize); } memcpy((char*) (s-&gt;elems)+(s-&gt;logLength), elemAddr, sizeof(*elemAddr)); s-&gt;logLength++; } void GenStackPop(genStack *s, void *elemAddr) { if(GenStackEmpty(s)) { fprintf(stderr, "Can't pop element from stack: stack is empty.\n"); } else { s-&gt;logLength--; memcpy((void*) elemAddr, (s-&gt;elems)+(s-&gt;logLength), sizeof(s-&gt;elems[s-&gt;logLength])); } } </code></pre> <p>Simple structures test:</p> <p>gentest.h:</p> <pre><code>#ifndef GENTEST1_H #define GENTEST1_H typedef struct { char* name; int age; char gender; }person; #endif </code></pre> <p>gentest.c:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include "gentest1.h" #include "genstacklib.h" int main(int argc, char* argv[]) { genStack StructStack; person testPerson[5]; person* newPerson; person* test; int i; newPerson = (void*) malloc (sizeof(person)); testPerson[0].name = "Alex"; testPerson[0].age = 21; testPerson[0].gender = 'm'; testPerson[1].name = "Vanja"; testPerson[1].age = 20; testPerson[1].gender = 'm'; testPerson[2].name = "sjrgsde"; testPerson[2].age = 11; testPerson[2].gender = 'w'; testPerson[3].name = "wergsggsd"; testPerson[3].age = 99; testPerson[3].gender = 'y'; testPerson[4].name = "adaasxx"; testPerson[4].age = 13; testPerson[4].gender = 'g'; GenStackNew(&amp;StructStack, sizeof(person)); printf("sizeof(person) = %lu\n", sizeof(person)); for (i = 0; i &lt; 5; i++) { newPerson = &amp;testPerson[i]; GenStackPush(&amp;StructStack, newPerson); printf("Pushed: %s, %d, %c\n", newPerson-&gt;name, newPerson-&gt;age, newPerson-&gt;gender); } test = (void*) malloc (sizeof(person)); test-&gt;name = "test"; test-&gt;age = 0; test-&gt;gender = 't'; while(!GenStackEmpty(&amp;StructStack)) { GenStackPop(&amp;StructStack, test); printf("Popped: %s, %d, %c\n", test-&gt;name, test-&gt;age, test-&gt;gender); } GenStackDispose(&amp;StructStack); return 0; } </code></pre> <p>And here's the output I get:</p> <pre><code>./gentest1 elemSize = 16 GenStackInitialAlocationSize = 4 sizeof(person) = 16 Pushed: Alex, 21, m Pushed: Vanja, 20, m Pushed: sjrgsde, 11, w Pushed: wergsggsd, 99, y New size of alloc = 8 Pushed: adaasxx, 13, g Popped: adaasxx, 0, t Popped: wergsggsd, 0, t Popped: sjrgsde, 0, t Popped: Vanja, 0, t Popped: Alex, 0, t </code></pre> <p>As you can see, I can receive names, but no age or gender. I've tried a lot of options, but still getting Segmentation Fault or the output from above. For moment, the output above is the finest output I get, but still not what I want. </p> <p>The question is - how can I get the output I need? Thanks in advance.</p> <p>To avoid some questions: sizeof(person) = s->elemSize</p> <p>It is defined by creating the stack:</p> <p>genstacklib.c:</p> <pre><code>void GenStackNew(genStack *s, int elemSize) { void* newElems; /* Allocate a new array to hold the contents. */ newElems = (void*) malloc(elemSize * GenStackInitialAlocationSize); printf("elemSize = %d\tGenStackInitialAlocationSize = %d\n", elemSize, GenStackInitialAlocationSize); if (newElems == NULL) { fprintf(stderr, "Error with allocating the stack.\n"); exit(1); /* Exit, returning error code. */ } s-&gt;elems = newElems; s-&gt;elemSize = elemSize; s-&gt;allocLength = GenStackInitialAlocationSize; s-&gt;logLength = 0; /*is empty*/ } </code></pre> <p>gentest.c:</p> <pre><code>GenStackNew(&amp;StructStack, sizeof(person)); printf("sizeof(person) = %lu\n", sizeof(person)); </code></pre>
 

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