Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (NULL == (p = calloc(sizeof(struct mystruct) * n,1))) {
- /* handle error */
- }
- int length = askMallocLibraryHowMuchMemoryWasAlloced(p) / sizeof(mystruct)
- void *p = calloc(sizeof(struct mystruct) * n + sizeof(unsigned long int),1));
- *((unsigned long int*)p) = n;
- void *arr = p+sizeof(unsigned long int);
- typedef struct {
- unsined int n;
- void *arr;
- } arrInfo;
- typedef void* arrayInfo;
- (arrayInfo)newArrayInfo(unsignd int n, unsigned int itemSize);
- (void)deleteArrayInfo(arrayInfo);
- (unsigned int)arrayLength(arrayInfo);
- (void*)arrayPtr(arrayInfo);
- ...
- void *blockOfMem = malloc(sizeof(mystruct)*n + sizeof(int));
- ((int *)blockofMem)[0] = n;
- mystruct *structs = (mystruct *)(((int *)blockOfMem) + 1);
- typedef struct MyStructTag
- { /* etc. */ } MyStruct ;
- void doSomething(MyStruct * p)
- {
- /* well... extract the memory allocated? */
- size_t i = get_size(p) ;
- initializeMyStructArray(p, i) ;
- }
- void doSomethingElse()
- {
- MyStruct * s = malloc(sizeof(MyStruct) * 10) ; /* Allocate 10 items */
- doSomething(s) ;
- }
- void doSomethingElse()
- {
- MyStruct * s = malloc(sizeof(MyStruct) * 10) ; /* Allocate 10 items */
- MyStruct * s2 = s + 5 ; /* s2 points to the 5th item */
- doSomething(s2) ; /* Oops */
- }
- int size;
- struct mystruct *cur;
- for (cur = myarray; cur->name != NULL; cur++)
- ;
- size = cur - myarray;
- void* my_malloc(size_t s); /* Calls malloc(s), and if successful stores
- (p,s) in a list of handled blocks */
- void my_free(void* p); /* Removes list entry and calls free(p) */
- size_t my_block_size(void* p); /* Looks up p, and returns the stored size */
- ...
- #include <stdio.h>
- float a, b, c, d, e, f, g;
- float myMax(float *apa,int soa){
- int i;
- float max = apa[0];
- for(i=0; i< soa; i++){
- if (apa[i]>max){max=apa[i];}
- printf("on i=%d val is %0.2f max is %0.2f, soa=%dn",i,apa[i],max,soa);
- }
- return max;
- }
- int main(void)
- {
- a = 2.0;
- b = 1.0;
- c = 4.0;
- d = 3.0;
- e = 7.0;
- f = 9.0;
- g = 5.0;
- float arr[] = {a,b,c,d,e,f,g};
- float mmax = myMax((float *)&arr,(int) sizeof(arr)/sizeof(arr[0]));
- printf("mmax = %0.2fn",mmax);
- return 0;
- }
Add Comment
Please, Sign In to add comment