【北邮OJ】277. 内存分配-网研14-计算机二14

本文介绍了一个使用C++实现的内存块管理程序,通过自定义比较函数`cmp`来对结构体数组进行排序,以便有效地为请求分配合适的内存块。程序还展示了如何跟踪已分配内存的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

总结:
1。自定义cmp函数
适用于:
①结构体多重排序
②改变排序规则(如倒序)
2。注意可用位的设置

#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;

struct memblock{
    int mem;
    int avail;
};

bool cmp(memblock a,memblock b){
    if(a.mem != b.mem) return a.mem<b.mem;
}

int main(){
    int t,T;
    cin>>T;
    for(t=1;t<=T;t++){
        int n,N;
        cin>>N;
        memblock str[100];
        int j;
        for(j=0;j<=99;j++)
            str[j].avail=0;
        for(n=0;n<=N-1;n++)
            cin>>str[n].mem;
        sort(str,str+N,cmp);
        int m,M;
        cin>>M;  
        for(m=1;m<=M;m++){
            int request;
            cin>>request;
            int i;
            for(i=0;i<=N-1;i++){
                if((request <= str[i].mem) && (str[i].avail==0)){
                    cout<<str[i].mem;
                    str[i].avail=1;
                    if(m != M)
                        cout<<" ";
                    break;
                }
            }
            if(i==N){
                cout<<"NULL";
                if(m != M)
                    cout<<" ";
            }              
        }
        cout<<endl;
    }
    return 0;
}
FAILED: out/target/product/p12_k37mv1_bsp/dex_bootjars/system/framework/arm/boot.art /bin/bash -c "(mkdir -p out/target/product/p12_k37mv1_bsp/symbols/system/framework/arm/ ) && (rm -f out/target/product/p12_k37mv1_bsp/dex_bootjars/system/framework/arm//*.art out/target/product/p12_k37mv1_bsp/dex_bootjars/system/framework/arm//*.oat out/target/product/p12_k37mv1_bsp/dex_bootjars/system/framework/arm//*.art.rel ) && (rm -f out/target/product/p12_k37mv1_bsp/symbols/system/framework/arm//*.art ) && (rm -f out/target/product/p12_k37mv1_bsp/symbols/system/framework/arm//*.oat ) && (rm -f out/target/product/p12_k37mv1_bsp/symbols/system/framework/arm//*.art.rel ) && (ANDROID_LOG_TAGS=\"*:e\" out/host/linux-x86/bin/dex2oatd --runtime-arg -Xms64m --runtime-arg -Xmx64m --compiler-filter=speed-profile --profile-file=out/target/product/p12_k37mv1_bsp/dex_bootjars/system/framework/boot.prof --dex-file=out/target/common/obj/JAVA_LIBRARIES/mediatek-common_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/mediatek-framework_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/mediatek-telephony-common_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/mediatek-telephony-base_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/core-oj_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/core-libart_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/conscrypt_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/okhttp_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/bouncycastle_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/apache-xml_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/ext_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/telephony-common_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/voip-common_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/ims-common_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/android.hidl.base-V1.0-java_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/android.hidl.manager-V1.0-java_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/framework-oahl-backward-compatibility_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/android.test.base_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/mediatek-ims-common_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/mediatek-telecom-common_intermediates/javalib.jar --dex-file=out/target/common/obj/JAVA_LIBRARIES/mediatek-cta_intermediates/javalib.jar --dex-location=/system/framework/mediatek-common.jar --dex-location=/system/framework/mediatek-framework.jar --dex-location=/system/framework/mediatek-telephony-common.jar --dex-location=/system/framework/mediatek-telephony-base.jar --dex-location=/system/framework/core-oj.jar --dex-location=/system/framework/core-libart.jar --dex-location=/system/framework/conscrypt.jar --dex-location=/system/framework/okhttp.jar --dex-location=/system/framework/bouncycastle.jar --dex-location=/system/framework/apache-xml.jar --dex-location=/system/framework/ext.jar --dex-location=/system/framework/framework.jar --dex-location=/system/framework/telephony-common.jar --dex-location=/system/framework/voip-common.jar --dex-location=/system/framework/ims-common.jar --dex-location=/system/framework/android.hidl.base-V1.0-java.jar --dex-location=/system/framework/android.hidl.manager-V1.0-java.jar --dex-location=/system/framework/framework-oahl-backward-compatibility.jar --dex-location=/system/framework/android.test.base.jar --dex-location=/system/framework/mediatek-ims-common.jar --dex-location=/system/framework/mediatek-telecom-common.jar --dex-location=/system/framework/mediatek-cta.jar --oat-symbols=out/target/product/p12_k37mv1_bsp/symbols/system/framework/arm/boot.oat --oat-file=out/target/product/p12_k37mv1_bsp/dex_bootjars/system/framework/arm/boot.oat --oat-location=/system/framework/arm/boot.oat --image=out/target/product/p12_k37mv1_bsp/dex_bootjars/system/framework/arm/boot.art --base=0x70000000 --instruction-set=arm --instruction-set-variant=cortex-a53 --instruction-set-features=default --android-root=out/target/product/p12_k37mv1_bsp/system --runtime-arg -Xnorelocate --compile-pic --no-generate-debug-info --generate-build-id --multi-image --no-inline-from=core-oj.jar --abort-on-hard-verifier-error --abort-on-soft-verifier-error --no-abort-on-soft-verifier-error --generate-mini-debug-info || ( echo \"ERROR: Dex2oat failed to compile a boot image. It is likely that the boot classpath is inconsistent. Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS=\"--runtime-arg -verbose:verifier\" to see verification errors.\" ; false ) && ANDROID_LOG_TAGS=\"*:e\" ANDROID_ROOT=out/target/product/p12_k37mv1_bsp/system ANDROID_DATA=out/target/product/p12_k37mv1_bsp/dex_bootjars/system/framework/arm/ out/host/linux-x86/bin/patchoatd --input-image-location=out/target/product/p12_k37mv1_bsp/dex_bootjars/system/framework/boot.art --output-image-relocation-directory=out/target/product/p12_k37mv1_bsp/dex_bootjars/system/framework/arm/ --instruction-set=arm --base-offset-delta=0x10000000 )" patchoatd E 08-01 17:32:01 1225778 1225778 image_space.cc:1761] Could not create image space with image file 'out/target/product/p12_k37mv1_bsp/dex_bootjars/system/framework/boot.art'. Attempting to fall back to imageless running. Error was: Failed to mmap at expected address, mapped at 0x7ead86400000 instead of 0x7057a000 patchoatd E 08-01 17:32:01 1225778 1225778 image_space.cc:1761] Attempted image: out/target/product/p12_k37mv1_bsp/dex_bootjars/system/framework/boot-framework.art patchoatd E 08-01 17:32:01 1225778 1225778 runtime.cc:1290] Dex file fallback disabled, cannot continue without image. patchoatd E 08-01 17:32:01 1225778 1225778 patchoat.cc:485] Unable to initialize runtime ninja: build stopped: subcommand failed. 17:32:01 ninja failed with: exit status 1 怎么关闭掉
最新发布
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值