I'm trying to implement a simple stack. It works fine on MacOSX but crashes on linux(Ubuntu). Can someone help me to know why?
MYStack.h
#ifndef MYSTACK_H_
#define MYSTACK_H_
typedef struct Element *element;
typedef struct Stack *stack;
struct Element {
struct Element *next;
void *data;
};
struct Stack {
struct Element *head;
unsigned int count;
void (*dump) (void *);
};
/* Define boolean type */
typedef signed char bool;
#define YES (bool)1
#define NO (bool)0
/* utility macro */
#define ELEMENT_NEXT(E) ((E) = (E)->next)
#define ELEMENT_DATA(E) ((E)->data)
#define STACK_HEAD(S) ((S)->head)
#define STACK_SIZE(S) ((S)->count)
/* Functions prototypes */
bool push( stack , void * );
bool pop( stack , void ** );
bool create_stack( stack , void (*) (void*) );
bool delete_stack( stack );
void dump_stack( stack );
#endif /* MYSTACK_H_ */
MYStack.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "MYStack.h"
/* Creating the stack */
bool create_stack( stack new_stack, void (*dump_function) (void*))
{
new_stack->head = NULL;
new_stack->dump = dump_function;
new_stack->count = 0;
return YES;
}
/* Deleting the stack */
bool delete_stack( stack this_stack )
{
element next_element;
while (this_stack->head) {
next_element = this_stack->head->next;
free(this_stack->head->next);
this_stack->head = next_element;
}
return YES;
}
/* Dump the stack */
void dump_stack(stack this_stack)
{
element e;
int i;
e = STACK_HEAD(this_stack);
if (this_stack->dump ) {
for (i = 0; i < this_stack->count; i++) {
(this_stack->dump) (e->data);
if (e->next != NULL)
e = e->next;
}
}
}
/* Adding element to the stack */
bool push( stack this_stack, void *data )
{
element new_element;
void (*temp_dump) (void *);
this_stack->dump = temp_dump;
new_element = (element ) malloc(sizeof(element *));
if (new_element == NULL) {
fprintf(stdout, "malloc() new element failed : %d\n", errno);
return NO;
}
new_element->data = data;
new_element->next = (this_stack)->head;
(this_stack)->head = new_element;
this_stack->dump = temp_dump;
(this_stack)->count++;
#ifdef DEBUG
fprintf(stdout, "Inserting at the stack in the address %p\n", new_element->data);
#endif
return YES;
}
/* Remving element from the stack */
bool pop( stack this_stack, void **data )
{
element delete_me;
void (*temp_dump) (void *);
this_stack->dump = temp_dump;
delete_me = this_stack->head;
if (delete_me == NULL) {
fprintf(stdout, "stack is empty\n");
return NO;
}
*data = delete_me->data;
this_stack->head = delete_me->next;
this_stack->dump = temp_dump;
this_stack->count--;
free(delete_me);
#ifdef DEBUG
fprintf(stdout, "Removing from the stack in the address %p\n", delete_me->data);
#endif
return YES;
}
/* Dump function test */
void dump_ints(void* data)
{
int* int_data_ptr = (int*)data;
printf("We are dumping an int data : %d\n", *int_data_ptr);
}
/* To test our stack */
int main (int argc, char const **argv)
{
stack new_stack;
void (*dump_func_ptr) (void*);
int i;
int stack_ints[] = {1, 2, 3, 4};
void *deleted_item;
#ifdef DEBUG
fprintf(stdout, "We are in debug mode...\n");
#endif
new_stack = (stack) malloc(sizeof(stack *));
dump_func_ptr = dump_ints;
create_stack( new_stack, dump_func_ptr);
/* Insert to the stack */
for (i = 0; i < 4; i++)
push( new_stack, &stack_ints[i]);
/* Print the number of elements */
printf("Our stack contain %d elements\n", new_stack->count);
/* Dump the stack */
dump_stack(new_stack);
/* Removing some data */
pop(new_stack, &deleted_item);
/* Dump the stack */
dump_stack(new_stack);
/* Deleting the stack */
//delete_stack(new_stack);
free(new_stack);
return 0;
}
There are a couple of incorrect allocations. They are only allocating memory the size of a pointer (4 bytes on a 32-bit system) and subsequent initialization of those pieces of memory will overwrite past the end of the allocated memory. The ones I noticed are these:
new_element = (element ) malloc(sizeof(element *));
and
new_stack = (stack) malloc(sizeof(stack *));
They should be something like this:
new_element = (element ) malloc(sizeof(struct Element));
new_stack = (stack) malloc(sizeof(struct Stack));
next_element = this_stack->head->next;
free(this_stack->head->next);
this_stack->head = next_element;
On the next iteration of the loop, you access this_stack->head, which has just been freed.
Related
I'm trying to implement a stack in C. I have only implemented the struct that will contain an array and that currently only contains the size of the array and the position of the last item added to the stack
This is a partial implementation that is giving me some trouble.
in stack.h
#include <stdlib.h>
#include <stdbool.h>
typedef struct Stack
{
int max_size;
int top;
// int *contents;
} Stack;
Stack *stack_create(int n);
bool stack_is_empty(Stack *stack);
bool stack_is_full(Stack *stack);
void stack_push(Stack *stack, int value);
in stack.c:
#include <stdio.h>
#ifndef STACK_H
#include "stack.h"
#endif
Stack *stack_create(int n)
{
Stack stack;
Stack *s = &stack;
s->max_size = n;
s->top = 0;
// s->contents = (int *)malloc(sizeof(int) * n);
return s;
}
bool stack_is_empty(Stack *stack)
{
if (stack->top == 0)
{
return true;
}
return false;
}
bool stack_is_full(Stack *stack)
{
if (stack->top == stack->max_size)
{
return true;
}
return false;
}
void stack_push(Stack *stack, int value)
{
if (!stack_is_full(stack))
{
printf("max_size: %d\n", stack->max_size);
printf("top: %d (%p)\n", stack->top++, &stack->top);
printf("value: %d (%p)\n", value, &value);
}
else
{
printf("Can't push. max_size==%d reached.\n", stack- >max_size);
exit(EXIT_FAILURE);
}
}
and in main.c:
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#define SIZE 3
int main()
{
Stack *s = stack_create(SIZE);
printf("stack_is_empty: %d\n", stack_is_empty(s));
stack_push(s, 100);
printf("stack_is_empty: %d\n", stack_is_empty(s));
stack_push(s, 30);
printf("stack_is_empty: %d\n", stack_is_empty(s));
stack_push(s, 20);
printf("stack_is_empty: %d\n", stack_is_empty(s));
return 0;
}
main produces the following output:
stack_is_empty: 1
max_size: 3
top: 100 (0x7ffd5430dfb4)
value: 101 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 30 (0x7ffd5430dfb4)
value: 31 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 20 (0x7ffd5430dfb4)
value: 21 (0x7ffd5430dfb4)
stack_is_empty: 0
Why is value's address the same of stack->top?
Problem 1 : You are allocating memory for the stack locally in stack_create function. As soon as the function goes out of scope memory will be freed. Thus you will have a dangling pointer.
Problem 2 : You are allocating memory only for one instance regardless of value of 'n'
typedef struct Stack
{
int max_size;
int *contents;
int top;
// int *contents;
} Stack;
Stack *stack_create(int n) {
Stack *s;
s = (Stack *)malloc(sizeof(Stack));
s->contents = (int *)malloc(sizeof(int) * n);
s->max_size = n;
s->top = 0;
return s;
}
Below is the code,
/****** list.h **********/
#include<stddef.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
/***************** Usage-start ************/
typedef enum{false, true}bool;
typedef enum {CREATE_NEW_LIST, DOUBLE_THE_LIST, HALF_THE_LIST}Op;
#if defined(ARRAY)
/* To ensure Encapsulation(i.e., maintain invariants of array) */
typedef struct List List;
#elif defined(LINKED_LIST)
/* To ensure Encapsulation(i.e., maintain invariants of linked list) */
/* User will not get access to node*/
typedef struct List List;
#else
#error "Wrong list implementation macro name !!!"
#endif
void insertItem(List *, void *newItem);
void *deleteItem(List *, int listIndex);
List* createList(List *, Op opType);
/************* arrayImpl.c ***********/
#include"list.h"
/************ Representation - start ************/
typedef struct List{
void **array;
/* Following members for Housekeeping - Array enhancement*/
int lastItemPosition;
int size;
}List;
#define INITIAL_LIST_SIZE 50
List *createList(List *list, Op opType){
if(opType == CREATE_NEW_LIST){
list = malloc(sizeof(List));
list->array = malloc(INITIAL_LIST_SIZE*sizeof(void*));
/* Is it safe to initialise zero to array of pointers? */
list->array = memset(list->array, 0, INITIAL_LIST_SIZE*sizeof(void *));
list->lastItemPosition = -1;
list->size = INITIAL_LIST_SIZE;
}else if(opType == DOUBLE_THE_LIST){
list->array = realloc(list->array, 2*(list->size)*sizeof(void *));
list->lastItemPosition = list->lastItemPosition;;
list->size = 2*(list->size);
}else if(opType == HALF_THE_LIST){
list->array = realloc(list->array, ((list->size)/2)*sizeof(void *));
list->lastItemPosition = list->lastItemPosition;
list->size = (list->size)/2;
}
return list;
}
/********** arrayImpl.c*********/
void *deleteItem(List *arrayList, int listIndex){
void *returnElement; //Deep copy before freeing the object
free(arrayList->array[listIndex]);
/* Delete operation - O(n) operation */
for(int accumulator = listIndex; accumulator <= arrayList->lastItemPosition; accumulator++){
arrayList->array[accumulator] = arrayList->array[++accumulator];
}
arrayList->lastItemPosition--;
/* House keeping - Half the list */
if(arrayList->size > INITIAL_LIST_SIZE){ /* Minimum size maintained */
if((arrayList->lastItemPosition + 1) == ((arrayList->size)/2)){
arrayList = createList(arrayList, HALF_THE_LIST);
}
}
return returnElement;
}
/***************arrayImpl.c***************/
void insertItem(List *arrayList, void *newItem){
/* House keeping - Enhance the array */
if(arrayList->lastItemPosition + 1 == arrayList->size){
arrayList = createList(arrayList, DOUBLE_THE_LIST);
}
/* Insert new element - O(1) operation */
arrayList->array[++(arrayList->lastItemPosition)] = newItem;
return;
}
User code,
#include"list.h"
int main(void){
List *arrayList = createList((List *)NULL, CREATE_NEW_LIST);
if (arrayList == (List *)0){
fprintf(stderr, "Unable to create list \n");
exit(1); //Nothing else to do without arrayList
}
// Objects should only be on heap
int *object1 = malloc(sizeof(int));
*object1 = 777;
insertItem(arrayList, object1);
int *object2 = malloc(sizeof(int));
*object2 = 888;
insertItem(arrayList, object2);
object1 = deleteItem(arrayList, 0);
}
I want to re-use List abstraction for writing Stack abstraction, as shown below with push()/pop()
#include"../list/list.h"
typedef struct Stack{
List *stack;
}Stack;
Question:
In deleteItem() function, how to deep copy arrayList->array[listIndex] and return returnElement from deleteItem() function?
pop() would be calling deleteItem()
Note: Compilation >gcc -DARRAY main.c arrayImpl.c
insertItem didn't allocate an item - so deleteItem shouldn't free it.
void *deleteItem(List *arrayList, int listIndex){
void *returnElement = arrayList->array[listIndex];
/* Delete operation - O(n) operation */
....
/* House keeping - Half the list */
....
return returnElement;
}
I get a segfault while runnig this code to implement a stack in C. Please note that the code is kind of incomplete. I just wanted to check and see if I could push a few elements on to the stack and print them out. But it throws back a segfault. Any help would be much appreciated!!
#include<stdlib.h>
#include<stdio.h>
struct stack
{
int *elems;
int ll;
int al;
};
void stack_new(struct stack *s)
{
s->ll=0;
s->al=4;
s->elems=malloc(4*sizeof(int));
}
void stack_del(struct stack *s)
{
free(s->elems);
}
void stack_push(struct stack *s,int value)
{
if(s->ll==s->al)
{
printf("overflow");
/*s->al*=2;
s->elems=realloc(s->elems, s->al*sizeof(int));*/
}
s->elems[s->ll]=value;
s->ll++;
}
void stack_pop(struct stack *s)
{
s->ll--;
return (s->elems[s->ll]);
}
void main()
{
struct stack *s;
stack_new(s);
stack_push(s,3);
stack_push(s,4);
stack_push(s,8);
printf("%d", s->elems[0]);
//stack_pop(s);
//stack_del(s);
}
Declaring
struct stack *s;
doesn’t allocate any memory for a struct stack. Do that:
struct stack *s = malloc(sizeof *s);
Or just put your stack on the stack:
struct stack s;
stack_new(&s);
…
Using more descriptive field names is also a good idea.
You have several errors
You never initialize the pointer s in your main function, so in your stack_new function dereferencing s causes a segmentation fault.
You should allocate space for the stack first, wherever you want but you must.
Another thing is if you want to initialize your al field with a constant number and then allocate an array of constant size, you don't need the field al, and you can declare elems as int elems[CONSTANT_NUMBER] but if you want it to be dynamic, which is what I think you want from your check if(s->ll == s->al) in the stack_push function, then you can simply pass the value you want al to have to the stack_new function.
This is some of your code, fixed so you can see what I actually mean.
#include<stdlib.h>
#include<stdio.h>
struct stack
{
int *elems;
int ll;
int al;
};
struct stack *stack_new(int al) /* you can pass the maximum number of elements allowed */
{
struct stack *s;
s = malloc(sizeof(struct stack));
if (s == NULL)
return NULL;
s->ll = 0;
s->al = al;
s->elems = malloc(al * sizeof(int)); /* and you dynamically allocate space for them here */
return s;
}
void stack_del(struct stack *s)
{
if (s != NULL) /* always check the pointers to prevent `SEGMENTATION FAULT` */
{
if (s->elems != NULL)
free(s->elems);
free(s);
}
}
void stack_push(struct stack *s, int value)
{
if (s == NULL)
return;
if(s->ll == s->al)
{
printf("overflow");
/*s->al*=2;
s->elems=realloc(s->elems, s->al*sizeof(int));*/
}
if (s->elems != NULL)
s->elems[s->ll] = value;
s->ll++;
}
int stack_pop(struct stack *s)
{
if ((s == NULL) || (s->elems == NULL))
return 0;
s->ll--;
return (s->elems[s->ll]);
}
int main()
{
struct stack *s;
s = stack_new(4);
stack_push(s, 3);
stack_push(s, 4);
stack_push(s, 8);
printf("%d", s->elems[0]);
stack_pop(s);
stack_del(s);
return 0;
}
```
I have been assigned to program a generic stack in ANSI C. It is meant to be for primitive datatypes. Until here there was no big problem whatsoever.
Afterwards I was asked to reprogram my application so that even complex data types can be used on my stack. I have searched and researched for the last week and I found nothing that could be helpful enough.
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stddef.h>
#include "genstacklib.h"
void (*freefn) (void*);
/*
* ToDo
*/
void GenStackNew(genStack *s, int elemSize, void (*freefunk) (void*))
{
s->elems = malloc (elemSize * GenStackInitialAllocationSize);
freefn = freefunk;
assert (s->elems != NULL);
s->elemSize = elemSize;
s->logLength = 0;
s->allocLength = GenStackInitialAllocationSize;
}
/*
* ULStackPush adds an element to the stack and allocates new memory if
* needed. If there is not enough memory, ULStackPush does nothing.
*/
void GenStackPush (genStack *s, const void *elemAddr)
{
/*assert (sizeof(*elemAddr) == s->elemSize);*/
assert (s->elems != NULL);
if (s->logLength == s->allocLength)
{
void *temp = NULL;
temp = realloc (s->elems, 2 * s->allocLength * s->elemSize);
assert (temp != NULL);
s->allocLength = 2 * s->allocLength;
s->elems = temp;
}
memcpy(currentval(s), elemAddr, s->elemSize);
s->logLength = s->logLength + 1;
}
void GenStackPop (genStack *s, const void *elemAddr)
{
assert (s->elems != NULL);
assert (s->logLength != 0);
(s->logLength)--;
memcpy((void *)elemAddr, currentval(s), s->elemSize);
}
void *currentval(genStack *s)
{
assert (s->elems != NULL);
return ((size_t*)s->elems + s->logLength * s->elemSize);
}
bool GenStackEmpty (const genStack *s)
{
assert (s->elems != NULL);
return s->logLength == 0;
}
void GenStackDispose (genStack *s)
{
assert (s->elems != NULL);
s->logLength = 0;
free (s->elems);
freefn();
}
/*
* ToDO
*/
void *freefn (void *) {
free
And my header data is:
#ifndef GENSTACKLIB_H
#define GENSTACKLIB_H
#include <stdbool.h>
#define GenStackInitialAllocationSize 4
typedef struct
{
void *elems;
int elemSize;
int logLength;
int allocLength;
} genStack;
void GenStackNew (genStack * s, int elemSize);
bool GenStackEmpty (const genStack * s);
void GenStackPush (genStack * s, const void *elemAddr);
void GenStackPop (genStack * s, const void *elemAddr);
void GenStackDispose (genStack * s);
void *currentval(genStack *s);
#endif
In the first block of code, I believe that what has to be done is in the ToDo markings.
How can I make it to use my stack for complex data types?
Thanks in advance
I dont see any problem with "complex" types like strings... there is no real difference bewteen pointer to string and pointer to int. So just store pointers (or pointers to pointers) and that should work.
So instead of element to be "int".. element is pointer to pointer.
Basic idea in form of very "pseudo" C code
typedef struct Wrapper
{
void * primitiveData;
} Wrapper;
void PrimitivePush(void * data)
{
Wrapper * w = malloc();
w->primitiveData = malloc();
memcpy(w->primitiveData, data);
ClassicComplexTypePush(&w)
}
ClassicComplexTypePush(void ** data)
{
push data to stack
}
Consider using a singularly linked list for implementation, since when
using a stack, we don't know how many items may be needed.
Use a byte* or (char*) to store the contents of memory, instead of a void* (which would also work, but we may need to pad the allocation, to include structs)
Copy memory into a new allocation, which is pushed onto the stack,
then delete that used upon pop.
each node has to be of the same type, or at-least the same size,
errors using wrong type though may be undesired
pop can be either used to check if the stack is empty by passing (NULL)
or to actually pop the stack, by referencing the memory you want to set.
typedef unsigned char byte;
Create the structures which will be used to keep track of the stack
struct gStackNode {
byte *data;
struct gStackNode *next;
};
struct gStack {
unsigned size;
struct gStackNode *head;
};
Initialize the stack, including the size of the type we will be using
void stack_initalize(struct gStack *stk, unsigned size) {
if (!stk)
return;
stk->size = size;
stk->head = (void*)0;
}
Always, we need to manually free the stack, in-case not all were popped
void stack_free(struct gStack *stk) {
if (!stk)
return;
struct gStackNode *temp;
/* step through the remaining stack, deleting each item */
while(stk->head) {
temp = stk->head->next;
free((byte*)stk->head->data);
free((struct gStackNode *)stk->head);
stk->head = temp;
}
}
push an item onto the stack
void stack_push(struct gStack *stk, void *data) {
struct gStackNode *node = (struct gStackNode*)malloc(sizeof(struct gStackNode));
struct gStackNode *temp = stk->head;
node->next = temp;
node->data = (byte*)malloc(sizeof(byte)*(stk->size));
byte * src = (char*)(data);
byte * dest = (char*)(node->data);
unsigned n = stk->size;
/* fill the new allocation with source data */
for(;n;n--)
*(dest++) = *(src++);
/* the node becomes the new head */
stk->head = node;
}
Sometimes we don't want to use a local variable ie: stack_pop_(stack, &type) we can use stack_push_arg_no_ref(stack, 10).
void stack_push_arg_no_ref(struct gStack *stk, void *data) {
stack_push(stk, &data);
}
Now we can pop, and use the same to peek, passing (NULL) to data will result in a peek,
returning (1) if there is an item in the stack, and a (0) if its empty
int stack_pop(struct gStack *stk, void * data) {
if (!stk)
return 0;
if (!stk->head)
return 0;
if (data == (void*)0) {
/*
simply check to see if the stack is empty or not
don't actually pop the stack
*/
return ((!stk->head == (void*)0));
} else {
struct gStackNode *next = stk->head->next;
struct gStackNode *node = stk->head;
unsigned i;
byte *c_temp = (byte*)data;
for(i=0;i<stk->size;i++)
*c_temp++ = node->data[i];
free((byte*)node->data);
free((struct gStackNode*)node);
stk->head = next;
}
}
Finally we can implement the stack
using any ANSI C data types
the size of a character string needs to be fixed
structs can also be used
Using a character string
CAUTION, for this example, the strings need to be NULL terminated, though
it is possible to use non-NULL terminated strings
char ta[32] = "ta: text 1";
char tb[32] = "tb: text 2";
char tc[32];
struct gStack stack_char; stack_initalize(&stack_char, sizeof(ta));
stack_push(&stack_char, ta);
stack_push(&stack_char, tb);
while (stack_pop(&stack_char, &tc))
printf("%s\n", tc);
be sure to free the stack
stack_free(&stack_char);
Using integers
int a = 120, b = -32, c;
struct gStack stack_int; stack_initalize(&stack_int, sizeof(int));
stack_push(&stack_int, &a);
stack_push(&stack_int, &b);
/* or we can use */
stack_push_arg_no_ref(&stack_int, 1776);
/* we can now see the contents of the stack */
while (stack_pop(&stack_int, &c))
printf("%d\n", c);
stack_free(&stack_int);
#include <stdio.h>
#include <stdlib.h>
/* Report an error and abort */
#define FATAL_ERROR(message) \
{ \
fprintf(stderr,"In %s(%d) [function %s]: %s\n", \
__FILE__, __LINE__, __FUNCTION__ , (message) ); \
abort(); \
} \
/* Report a posix error (similar to perror) and abort */
#define FATAL_PERROR(errcode) FATAL_ERROR(strerror(errcode))
void* Malloc(size_t n)
{
void* new = malloc(n);
if(new==NULL) FATAL_ERROR("Out of memory.");
return new;
}
typedef struct twit{
char data[141]; //contains the actual data
//struct twit *prev; //pointer to previous node (Closer to front)
struct twit *next; //pointer to next node (Closer to back)
}twit;
typedef struct twitbuffer{
twit *first;
twit *last;
int size;
}twit_buffer;
/*
function for create a new buffer
*/
void new_twitbuffer(twit_buffer *a)
{
a=Malloc(sizeof(twit)*12000);
a->first = a->last = NULL;
a->size = 0;
return;
}
int twitbuffer_empty(twit_buffer *a) {
if(a->first == NULL)
return 1;
else
return 0;
}
/*
function to insert a new twit in the buffer
*/
void insertTwit(twit_buffer *a, char *data)
{
twit new;
if (strlen(&data)<=140){
strcpy(&new.data,data);
}
else{
printf("Twit > 140 characters...");
}
if (new.data == NULL) {
//errno = ENOMEM;
printf("error!");
return;
}
if(a->first==NULL){
a->first = a->last = &new;
}else{
a->last->next=&new;
a->last=&new;
}
new.next= NULL;
a->size++;
return;
}
char* popTwit(twit_buffer *a) {
if (twitbuffer_empty(a)) {
return NULL;
}
char *data;
//strcpy(&data,a->first->data);
data=a->first->data;
if (a->first == a->last)
a->first = a->last = NULL;
else
a->first = a->first->next;
a->size--;
return data;
}
twit_buffer mytwitbuffer;
int main()
{
new_twitbuffer(&mytwitbuffer);
//printf("a=%d",mytwitbuffer);
char *a = "first twit\n";
char *b = "second twit\n";
char *c = "third twit\n";
insertTwit(&mytwitbuffer, a);
insertTwit(&mytwitbuffer, b);
insertTwit(&mytwitbuffer, c);
char *poppp;
poppp = popTwit(&mytwitbuffer);
printf("%s", poppp);
poppp = popTwit(&mytwitbuffer);
printf("%s", poppp);
poppp = popTwit(&mytwitbuffer);
printf("%s", poppp);
}
This is my code for an implementation of a queue. When i execute this i take this result:
thir�it
(null)(null)
This means that the two first insertions are not being done correctly and the third goes in stdout in a "paranormal" way! Do you have any ideas?
You are storing a reference to the local variable 'new' in your twit buffer. You should Malloc it, instead Of declaring It local