C container

I'm making a C side container...

Question:

Storing addresses in size_t would be wrong if I use bigger sized pointers?



I already wrote one with addressing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef ADDRESSLIST_H
#define ADDRESSLIST_H

#include<stddef.h>
#include<stdlib.h>

typedef struct{
    size_t*list;//addresses
    size_t len; //list length
    size_t cap; //capacity...
}addressl;

addressl*addresslnew(size_t cap){
    addressl*res=(addressl*)malloc(sizeof(addressl));
        res->cap=cap;
        res->len=0;
        res->list=(size_t*)malloc(cap*sizeof(size_t));
    return res;
};

size_t addresslfind(addressl*cont,const void*item){
    size_t pos=0;
    size_t address=(size_t)item;

    for(;pos<cont->len;++pos)if(cont->list[pos]==address)return pos;

    return cont->len;
};

void addresslgoto(addressl*cont,size_t from,size_t to){
    size_t address=cont->list[from];

    if(to>from){
        for(;from<to;++from){
            cont->list[from]=cont->list[from+1];
        };
    }else{
        for(;from>to;--from){
            cont->list[from]=cont->list[from-1];
        };
    };

    cont->list[to]=address;
};

void addresslmove(addressl*cont,size_t begin,size_t end,size_t to){
    size_t len=end-begin;
    size_t border=to+len;
    if(begin>to){
        for(;to<border;++to,++begin){
            addresslgoto(cont,begin,to);
        };
    }else{
        for(;to<border;++to){
            addresslgoto(cont,begin,border-1);
        };
    };
};

void addresslpush(addressl*cont,void*item){
    ++cont->len;

    if(cont->len>cont->cap){
        cont->cap+=cont->cap;
        cont->list=(size_t*)realloc(cont->list,sizeof(size_t)*cont->cap);
    };

    cont->list[cont->len-1]=(size_t)item;
};

void addresslpop(addressl*cont){
    --cont->len;
};

void addresslinsert(addressl*cont,void*item,size_t postarget){
    size_t pos=cont->len;

    addresslpush(cont,item);

    for(;pos>postarget;--pos){
        cont->list[pos]=cont->list[pos-1];
    };

    cont->list[postarget]=(size_t)item;
};

void addresslerase(addressl*cont,size_t posbegin,size_t posend){
    size_t ptr;
    size_t pos=posbegin;
    size_t len=cont->len;
    size_t*list=cont->list;

    for(;pos<len;++pos){
        ptr=(pos-posbegin)+posend;
        if(ptr<=len){
            list[pos]=list[ptr];
        };
    };

    cont->len-=(posend-posbegin);
};

void addresslfree(addressl*cont,size_t posbegin,size_t posend){
    size_t ptr;
    size_t pos=posbegin;
    size_t len=cont->len;
    size_t*list=cont->list;

    for(;pos<len;++pos){
        ptr=(pos-posbegin)+posend;
        if(ptr<=len){
            list[pos]=list[ptr];
        }else{
            free((void*)list[pos]);
        };
    };

    cont->len-=(posend-posbegin);
};

void addresslreserve(addressl*cont,size_t len){
    cont->len=len;
    cont->list=(size_t*)realloc(cont->list,sizeof(size_t)*len);
};

#endif//ADDRESSLIST_H 
Storing addresses in size_t would be wrong if I use bigger sized pointers?
That depends on how size_t is defined. In other words: don't do that.

If you want to store different types in an array use union
thanks for the answer but not what I expected lol...

I got the answer from myself when I allocated 2GB data and there wasn't crash l0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>

#include<alastd.h>
#include<addresslist.h>

/**************************************************************************************************/

typedef struct{float x,y,z;}test;

int main(){
    addressl*list=addresslnew(4);

    int i=0;
    for(;i<7;++i){
        {float pos=0.f;
            for(;pos<10000000.f;pos+=1.f){
                test hue;
                    hue.x=(pos+1);
                    hue.y=(pos+1);
                    hue.z=(pos+1);
                addresslpush(list,copydata(&hue,12));
            };
        };
    };


    printf("%010u% f %f %f\n"
           ,list->list[list->len-1]
           ,((test*)list->list[list->len-1])->x
           ,((test*)list->list[list->len-1])->y
           ,((test*)list->list[list->len-1])->z
           );

    char hue[256];
    gets(hue);

    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef ALASTD_H
#define ALASTD_H

#include<stdlib.h>
#include<string.h>

void*copydata(const void*item,size_t size){
    void*res=malloc(size);
        memcpy(res,item,size);
    return res;
};

#endif//ALASTD_H 
Topic archived. No new replies allowed.