相关文章推荐
兴奋的口罩  ·  解决:”ssh-keygen ...·  1 年前    · 
胡子拉碴的椰子  ·  TypeError: sequence ...·  1 年前    · 
文雅的领带  ·  visual studio 2010 - ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I Use Emscripten's emcmake to compile my C project.

When I opened the generated webpage, I got the error:

text of above picture

Uncaught (in promise) RuntimeError: index out of bounds createExportWrapper http://localhost:63342/new_exp/emsdk-build/executable.js:1165 callMain http://localhost:63342/new_exp/emsdk-build/executable.js:9815 doRun http://localhost:63342/new_exp/emsdk-build/executable.js:9869 run http://localhost:63342/new_exp/emsdk-build/executable.js:9884 runCaller http://localhost:63342/new_exp/emsdk-build/executable.js:9800 removeRunDependency http://localhost:63342/new_exp/emsdk-build/executable.js:1086 receiveInstance http://localhost:63342/new_exp/emsdk-build/executable.js:1253 receiveInstantiationResult http://localhost:63342/new_exp/emsdk-build/executable.js:1271 promise callback createWasm/instantiateAsync/< http://localhost:63342/new_exp/emsdk-build/executable.js:1312 promise callback instantiateAsync http://localhost:63342/new_exp/emsdk-build/executable.js:1304 createWasm http://localhost:63342/new_exp/emsdk-build/executable.js:1341 http://localhost:63342/new_exp/emsdk-build/executable.js:9333executable.wasm:384544:1

I run this C project on Linux( not using Emscripten ), check it with sanitizers and all runs well.

I find the problem might be caused by:

uint64_t i; 
char     text[1024];    
FILE* fp = stdout;
fprintf(fp, "CAPACITY %" PRIu64 ", ELE_SIZE %" PRIu64 ", BUCKET_SIZE %" PRIu64 "\n", map->buckets_capacity, map->ele_size, map->bucket_size);
fprintf(fp, "USER_DATA 0x%p\n", map->user_data);    
fprintf(fp, "%6s\t%16s\tDATA\n", "psl", "HASH");    
for (i = 0; i < map->buckets_capacity; i++) {       
    _bucket_t *bucket;      
    /* error happened on the line below */      
    bucket = (_bucket_t *) ((char *) map->buckets_ + i * map->bucket_size); /*original: hashmap_bucket_at_(map, map->buckets_, i);*/    
    if (bucket->psl) print_data(hashmap_bucket_data_(bucket), text, 1024);      
    else text[0] = '\0';    
    fprintf(fp, "%6u\t%16lu\t%s\n", bucket->psl, bucket->hash, text);   

The memory is allocated by:

map->buckets_  = malloc(map->bucket_size * map->buckets_capacity);

where bucket_size is 144, and bucket_capacity is 16.

I have used "-s ALLOW_MEMORY_GROWTH=1" and "-fsanitize=address" when use Emscripten when this error occurs.I don't know why this happened.

You're asking us to debug code we cannot see based on a vague description of that code. contains some math calculations is an absolute waste of keystrokes. If you want help debugging your code, paste your actual code into your post. See How to Ask and minimal reproducible example, and then edit your post. If you can't provide the code in the form of a minimal reproducible example, we can't help you. – Ken White Oct 15, 2022 at 0:35 And that code is much more than some math calculations. It has a loop in it, which is where the error is happening. Use a debugger to step through the code to figure out where your logic error is - an index out of bounds error means that you're going off then end of the list or collection or array. As you've failed to provide a minimal reproducible example (which is clear because there are variables in use that are not declared in the code you posted), it's impossible to tell where the problem is located. – Ken White Oct 15, 2022 at 2:22 And as a note: If you're getting an index out of bounds error, it is not all running well. It's generating a runtime error. – Ken White Oct 15, 2022 at 2:29
fprintf(fp, "%6u\t%16lu\t%s\n", bucket->psl, bucket->hash, text);   

Where bucket->hash has a type uint64_t .

After I change %lu to %"PRIu64", no problems appear anymore.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.