Sponsored Content
Top Forums Programming C: Initialize "const" array from the "heap" Post 302468327 by Corona688 on Tuesday 2nd of November 2010 11:27:36 AM
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.
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
pthread_rwlock_init(3T) 												   pthread_rwlock_init(3T)

NAME
pthread_rwlock_init(), pthread_rwlock_destroy() - initialize or destroy a read-write lock SYNOPSIS
PARAMETERS
rwlock Pointer to the read-write lock to be initialized or destroyed. attr Pointer to the attributes object that defines the characteristics of the read-write lock to be initialized. If the pointer is NULL, default attributes are used. DESCRIPTION
initializes the read-write lock referenced by rwlock with the attributes attr. If attr is NULL, the default read-write lock attributes are used. Upon successful initialization, the state of the read-write lock becomes initialized and unlocked. Attempting to initialize an already initialized read-write lock object results in undefined behavior. The macro can be used to initialize read-write locks which are statically allocated. The effect is equivalent to dynamic initialization by a call to with the attr parameter specified as NULL, except that no error checks are performed. The read-write lock will be initialized with default attributes. If the process-shared attribute in the read-write lock attributes object referenced by attr is defined as the read-write lock must be allo- cated such that the processes sharing the read-write lock has access to it. This may be done through the memory-mapping functions (see mmap(2)) or shared memory functions (see shmget(2)). destroys the read-write referenced by rwlock. This function may set rwlock to an invalid value. The destroyed read-write lock can be reinitialized using the function If the read-write lock is referenced after destruction in any read-write lock call, the resulting behavior is undefined. A read-write lock should be destroyed only when no threads are currently using it. Destroying a read-write lock which is currently in use results in undefined behavior. RETURN VALUE
Upon successful completion, and returns zero. Otherwise, an error number is returned to indicate the error (the variable is not set). ERRORS
If any of the following occur, the function returns the corresponding error number: The necessary resources (other than memory) to initialize rwlock are not available. rwlock is an already initialized read-write lock. However, getting for reinitializing the rwlock cannot be expected all the time since the behavior of reinitializing the rwlock is undefined. The value specified by rwlock or attr is invalid. There is insufficient memory available in which to initialize the read-write lock rwlock. The caller does not have the privilege to perform the operation. If any of the following occur, the function returns the corresponding error number: rwlock is currently locked or being used by other threads. The value specified by rwlock is invalid. WARNINGS
The space for the read-write lock must to be allocated before calling Undefined behavior may result if the process-shared attribute of attr is and the space allocated for the read-write lock is not accessible to cooperating threads. AUTHOR
and were developed by X/Open. SEE ALSO
pthread_rwlock_rdlock(3T), pthread_rwlock_tryrdlock(3T), pthread_rwlock_trywrlock(3T), pthread_rwlock_unlock(3T), pthread_rwlock_wrlock(3T). STANDARDS CONFORMANCE
Pthread Library pthread_rwlock_init(3T)
All times are GMT -4. The time now is 05:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy