Created
April 17, 2011 19:30
-
-
Save asim/924393 to your computer and use it in GitHub Desktop.
nothing special
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
struct array { | |
int pos; | |
char *pointers[1024]; | |
}; | |
static struct array arr; | |
int push(char *data) { | |
if (arr.pointers[arr.pos] != NULL) { | |
free(arr.pointers[arr.pos]); | |
arr.pointers[arr.pos] = NULL; | |
} | |
char *ptr = (char *)malloc(sizeof *data); | |
if (ptr == NULL) | |
return 0; | |
else { | |
strcpy(ptr, data); | |
arr.pointers[arr.pos] = ptr; | |
arr.pos += 1; | |
return 1; | |
} | |
} | |
char* pop(int pos) { | |
if (arr.pointers[pos] != NULL) { | |
char *ptr = arr.pointers[pos]; | |
arr.pointers[pos] = NULL; | |
return ptr; | |
} else { | |
return NULL; | |
} | |
} | |
char* get(int pos) { | |
if (arr.pointers[pos] != NULL) | |
return arr.pointers[pos]; | |
} | |
int del(int pos) { | |
if (arr.pointers[pos] != NULL) { | |
printf("%p\n",arr.pointers[pos]); | |
free(arr.pointers[pos]); | |
arr.pointers[pos] = NULL; | |
return 1; | |
} | |
// no del occured | |
return 0; | |
} | |
int main(int argc, char *argv[]) { | |
arr.pos = 0; | |
int i; | |
for (i=0; i<1024; i++) | |
arr.pointers[i] = NULL; | |
char buf[] = "foo bar"; | |
printf("the string is: %s\n",buf); | |
if (push(buf) == 0) | |
printf("push failed\n"); | |
char *data = get(1); | |
printf("array pos: %d\n", arr.pos); | |
printf("the string is: %s\n",data); | |
char buf2[] = "haha this sucks"; | |
if (push(buf2) == 0) | |
printf("push failed\n"); | |
char *data2 = get(1); | |
printf("the string is: %s\n", data2); | |
int j; | |
for (j=0; j<1024; j++) { | |
if (arr.pointers[j] != NULL) | |
printf("pointer at %d is %p\n", j, &arr.pointers[j]); | |
} | |
char *data3 = get(1); | |
printf("the string is before pop: %s\n", data3); | |
char *ptr = pop(1); | |
printf("popped item and it is: %s\n", ptr); | |
if (del(0) == 0) | |
printf("delete of 0 failed\n"); | |
printf("new arr pos: %d\n", arr.pos); | |
printf("pointer at 0 is %p\n", &arr.pointers[0]); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment