C: Initialize "const" array from the "heap"


 
Thread Tools Search this Thread
Top Forums Programming C: Initialize "const" array from the "heap"
# 1  
Old 11-02-2010
C: Initialize "const" array from the "heap"

Hello,

I am working on solving an NP-Complete problem, so it is very important that operations and data with limited integer-argument ranges be computed using immutable look-up-tables contained entirely in CPU cache. Retrieval of the look-up-table data must never leave the CPU once initially read in from main memory.

Details about the program function stack are hidden, and it is just poor form to include massive look-up-table data in a ".c" source file.

Code:
#define NUMBER_OF_SQUARES 9
#define SIZE_OF_SQUARE 3

const unsigned short SetSizes[NUMBER_OF_SQUARES] = { 26, 205, 205, 205, 576, 205, 205, 205, 576 };

unsigned int X;
const unsigned short *TimesSquareSize;

// This is an array of pseudo 2-D arrays.
const unsigned char *SetRecords[NUMBER_OF_SQUARES];

TimesSquareSize = (const unsigned short *)malloc(576*sizeof(unsigned short));

for ( X = 0; X < NUMBER_OF_SQUARES; X++ ) SetRecords[X] = (const unsigned char *)malloc(SIZE_OF_SQUARE*sizeof(unsigned char)*SetSizes[X]);

// How do I now initialize the const arrays using fread()?

Here is a sample of my published work:

www.pathcom.com/~vadco/deep.html - It is important for me to solve at least 2 NP-Completes.


All the very best,

JohnPaul Adamovsky
# 2  
Old 11-02-2010
You can't write to a const array. if you want to write to it, step 1 is to remove the 'const' specifier. Smilie

How to read from file depends what exactly you're reading. Text? Binary? What?

If it's just binary integers in the same format as your array, you can

Code:
fp=fopen("file", "rb");
fread(array, sizeof(unsigned short), NUMBER_OF_SQUARES, fp);

# 3  
Old 11-02-2010
Corona688,

I never said I needed to "write to a const array" as you put it.

"How do I now initialize the const arrays using fread()?"

Initialize is the word I used. All real data must be initialized, including "const" read-only data, but after that initialization, I need the program to treat the freshly initialized data, dynamically allocated from the heap, as an immutable read only construct.

Let me rephrase my question:

How do I make sure that a dynamically allocated array is treated as an immutable structure, which is always stored in CPU cache? I am indifferent about using the word "const".

Should I simply just assume that the compiler can figure out when an array only ever gets written to once? But that doesn't sound very scientific. I am not programming computers to be a dweeb, but rather a scientist, in a tradition akin to the great John von Neumann.

I do not need your help on how to use fread(), for it is well documented. I need you to take a look at the link I provided with an example of my previous work, before you say something else that makes you look like a "daydream Johnny."

Explain to me why anybody would store 9 unsigned shorts in a file. Your example demonstrates a less than thorough investigation of my post.


All the very best,

JohnPaul Adamovsky
# 4  
Old 11-02-2010
Quote:
Explain to me why anybody would store 9 unsigned shorts in a file.
One word - persistence.


Quote:
How do I make sure that a dynamically allocated array is treated as an immutable structure, which is always stored in CPU cache? I am indifferent about using the word "const".
Control of a CPU cache(s) is processor (and usually model) specific. Read the manufacturer's programming manual(s) for your specific processor.
# 5  
Old 11-02-2010
Quote:
Originally Posted by HeavyJ
Corona688,

I never said I needed to "write to a const array" as you put it.

"How do I now initialize the const arrays using fread()?"
Not really a difference, per se. Whether you're modifying it once or modifying it many times, it still involves the memory being modified at some point.
Quote:
Initialize is the word I used. All real data must be initialized, including "const" read-only data, but after that initialization, I need the program to treat the freshly initialized data, dynamically allocated from the heap, as an immutable read only construct.
Whether you call it "initialization" or "write" it still involves the modification of read-only memory. If the memory's in stack space it's not "truly" read only, but then, it's not "truly" protected either.

How large a lookup table are we talking about here? If it's hundreds of megabytes or less, you should be able to just map it in with mmap, a handy operation for these sort of things; file into immutable memory in one step with no read() inbetween, with the added advantage that the OS only actually loads what you actually use.

Code:
int fd=open("filename", O_RDONLY);
const unsigned short *table=mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);

You could also use mprotect to temporarily change the memory protection on the memory page your array resides in.

Quote:
I do not need your help on how to use fread(), for it is well documented. I need you to take a look at the link I provided with an example of my previous work, before you say something else that makes you look like a "daydream Johnny."
Then you know perfectly well fread() cannot accomplish what you want by itself, yet that is what you asked for.
# 6  
Old 11-02-2010
Here is simple code that uses a static storage are, referenced by a const char * which persists thru the duration of the code on the stack frame for main().

I really am unsure what you are trying to do; this is the best answer I can give you.

Code:
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>

static
char *tmp=NULL;

char *loadit(FILE *in, const int len)
{
    tmp=malloc(len+1);
    fread(tmp, len, 1, in);

    return tmp;
}

size_t filesize(int fd)
{
    struct stat st;
    fstat(fd, &st);

    return st.st_size;
}

int main(int argc, char **argv)
{
    FILE *in=fopen("filename", "r");
    int filelen=filesize(fileno(in));   
    const char *constmem = loadit(in, filelen);
    printf("%s\n", constmem); // assumes readable file content
    fclose(in);
    free(tmp);
    return 0;
}

as long as you pass the constmem pointer to subsequent functions, you have static const memory that was "dynamically" loaded. Add error checking.
# 7  
Old 11-02-2010
Quote:
I really am unsure what you are trying to do; this is the best answer I can give you.
Forgive me for the confusion. I need to import a large look-up-table from a data file into a memory location designated for read-only use. There must me a way to do it, because it is a common procedure.

I want to thank jim mcnamara and Corona688 for some information that I can try out today.

fpmurphy - It is your kind of "help" that created the "Winnebago Man" and the world is doing just fine with only one of him. The world does not need two Winnebago Men. And try to avoid acting like a girl, just got felt up at the drive-in.


All the very best,

JohnPaul Adamovsky
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Solaris

The slices "usr", "opt", "tmp" disappeared!!! Help please.

The system don't boot. on the screen appears following: press enter to maintenance (or type CTRL-D to continue)...I checked with format command. ... the slices "0-root","1-swap","2-backup" exist. ...the slises "3-var","6-usr" -unassigned. :( (16 Replies)
Discussion started by: wolfgang
16 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question