新手学习nginx,模拟nginx思想编写程序

nginx 数组

#include "stdio.h"
typedef unsigned char u_char;//windows 中 是 byte

struct array_s {
    u_char*     startpos;
    size_t      maxlength;
    size_t      size;
    size_t      length; //实际上存在的最小空间大小个数  1

};

typedef struct array_s array_t;



array_t* array_create( size_t length, size_t size);
void array_destory(array_t * array);
void* array_push(array_t* a);

array_t* array_create( size_t length, size_t size)
{
    array_t* a;
    a = (array_t*)malloc(sizeof(array_t));
    if (a==NULL){return NULL;}

   // a->startpos=(u_char*)_aligned_malloc(size*length,16);
    a->startpos = (u_char*)malloc(size * length);
    if (a->startpos == NULL) {return NULL;}
    a->maxlength = length;
    a->size = size;
    a->length = 0;
    return a;
}

void array_destory(array_t * array)
{
    free(array->startpos);
    free(array);
}


void* array_push(array_t* a)
{
    u_char *startpos,*newstartpos;
    size_t size;

    if (a->length == a->maxlength) //内存使用完了的时候....
    {
        size = a->maxlength * a->size;

        newstartpos = (u_char*)malloc(size * 2);

        if (newstartpos != NULL)
        {
            memcpy(newstartpos,a->startpos,size);
            free(a->startpos);
            a->startpos = newstartpos;
            a->maxlength *= 2;
        }else{
            printf("memerror!");
            return NULL;
        }
    }

    //正常的,在范围内的array......
    newstartpos = (u_char*)a->startpos + a->length * a->size; // 往后面偏移一位 .....
    a->length++;
    return newstartpos; //返回新的,下一个元素的存放位置......
}




dump(array_t * a)
{
    int tmp=0;
    for(size_t i=0;i<a->length;++i)
    {
        memcpy(&tmp,(a->startpos + i*a->size),a->size);//只能通过这样子,将4个字节的内存转为整数
        printf("%d-->%x,size=%d,value_is:%d\n",i,a->startpos + i*a->size,a->size,tmp);
    }
}

typedef struct{
    int length;
    char* data;
} string;

dump1(array_t * a)
{
    string* tmp;

    for(size_t i=0;i<a->length;++i)
    {
        tmp = (string*)(a->startpos + i*a->size);
        printf("%d-->%x,size=%d,value_is : %s,strlen=%d\n",i,a->startpos + i*a->size,a->size,tmp->data,tmp->length);
    }
}

int main(int argc,char ** argv)
{
    array_t * myarray = array_create(10,sizeof(int));
    int* tmp=0;

    for(int i = 200 ; i< 228 ; ++i)
    {
        tmp=array_push(myarray);
        *tmp = i * i;
    }
   dump(myarray);

    array_t * myarray1;

    string* tmp1;
    char tmp2[255]="111";

    for(int i=0; i< 3;++i)
    {
        myarray = array_create(10,sizeof(string));

        for(int i=0; i< 9;++i)
        {
            tmp1 = (string*)array_push(myarray);
            sprintf(tmp2,"---%d---",i);
            tmp1->data = malloc(strlen(tmp2)+1);        //这里调试花了好长时间, 是因为, 没有给字符串指针分配内存空间, 就往字符串指针里面复制内容, 导致堆溢出。
            if (tmp1->data == NULL){printf("error!");}
            memset(tmp1->data,'\0',sizeof(tmp1->data));
            //printf("====%x,%d\n",tmp1->data,i);
            memcpy(tmp1->data,tmp2,strlen(tmp2)+1);
            tmp1->length=strlen(tmp1->data);
        }
        //char* tmp1="111111111111111111111111";
        /* string* tmp2=(string*)myarray->startpos; */
        /* printf("%s\n",tmp2->data); */
        /* printf("tmpx=%x,tmps=%s,%x,%s,max:%d,sizeof=%d\n",tmp,tmp,tmp1,tmp1,myarray->length,sizeof(char[255])); */
        //tmp1 = array_push(myarray);
        //tmp1="23432412321asfdsfasfasdf";
        //tmp1 = array_push(myarray);
        //tmp="sdafadosifjiodsajfoisdajfoiasdjf";

        dump1(myarray);
        array_destory(myarray);
    }

}

编译

cl array.c

结果

clipboard.png


paulli3
992 声望8 粉丝

源码疯狂痴迷爱好者