Generic Stack implementation in C-style C++ I implemented generic stack in C. Seems to work fine. It doesn't work for strings, though.

It was compiled using Visual C++.

Implementation:

#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;

struct node{
    void* value;

    node* next;
};

struct stack{
    node* first;
    int elemSize;
    int length;
};

stack* StackNew(int elemSize)
{
    stack* s = (stack*)malloc(sizeof(stack));
    s-&gt;elemSize = elemSize;
    s-&gt;length = 0;
    return s;
}

void Push(stack* s, void* value)
{
    node* oldFirst = s-&gt;first;
    s-&gt;first = (node*)malloc(sizeof(node));
    s-&gt;first-&gt;value = malloc(s-&gt;elemSize);
    memcpy(s-&gt;first-&gt;value, value, s-&gt;elemSize);
    s-&gt;first-&gt;next = oldFirst;
    s-&gt;length++;
}

void* Pop(stack* s)
{
    void* value = malloc(s-&gt;elemSize);
    memcpy(value, s-&gt;first-&gt;value, s-&gt;elemSize);
    node* oldFirst = s-&gt;first;
    s-&gt;first = s-&gt;first-&gt;next;
    free(oldFirst);
    s-&gt;length--;
    return value;
}

void Destory(stack* s)
{
    while (s-&gt;length &gt; 0) Pop(s);
    free(s);
}


Test:

struct point{
    int x, y;
};

int main()
{
    stack* s = StackNew(sizeof(point));
    for (int i = 0; i &lt; 100; i++){
        point pt;
        pt.x = i;
        pt.y = i * i;
        Push(s, &amp;pt);
    }

    while (s-&gt;length &gt; 0) {
        point pt = *(point*)Pop(s);
        printf("(%d,%d)\n", pt.x, pt.y);
    }
    Destory(s);
    return 0;
}


Output:


(99,9801)
(98,9604)
(97,9409)
(96,9216)
(95,9025)
(94,8836)
(93,8649)
(92,8464)
(91,8281)
(90,8100)
(89,7921)
(88,7744)
(87,7569)
(86,7396)
...



as expected.

Please tell me what I did wrong and how I can improve things.
Naming seems inconsistent.

Consider prefixing all exported names with Stack, e.g. StackPush, StackPop, StackDestroy (Destory is definitely a typo).

The entity named first in a stack context is traditionally referred to as top.
Memory management is overengineered.

Every time a value is pushed memory is allocated. That's justifiable for ownership reasons (make sure to document it). However every time a value is popped memory is allocated again. This is very strange. A simple

return s-&gt;first-&gt;value;


would suffice.

Along the same line Destroy doesn't free memory (re)allocated for individual stack entries. Consider

while(s-&gt;length &gt; 0)
    free(Pop(s))

You've mention the problem with strings; I believe it is stemmed from elemSize being a property of the stack itself. Making it a property of an element would solve the problem.
As mentioned in the comments, the C code shall not include C++ headers.