Where a big array can be stored?

  • Thread starter idkfaidkfaidkfa
  • Start date
I

idkfaidkfaidkfa

I've found an example where a structure with a pointer to a very big
array is present...const array and structure values are in the same .c
file; struct declaration is in a .h file. In the same .h file, an
"extern type_struct name_struct" is also present. Is this correct? Why
array and structure values are non stored in a .h file?
Thanks
 
C

chutsu

I've found an example where a structure with a pointer to a very big
array is present...const array and structure values are in the same .c
file; struct declaration is in a .h file. In the same .h file, an
"extern type_struct name_struct" is also present. Is this correct? Why
array and structure values are non stored in a .h file?
Thanks

Can you post some code? Its quite hard to visualize without looking at
the code.
 
A

Anand Hariharan

I've found an example where a structure with a pointer to a very big
array is present...const array and structure values are in the same .c
file; struct declaration is in a .h file. In the same .h file, an
"extern type_struct name_struct" is also present. Is this correct? Why
array and structure values are non stored in a .h file?
Thanks

To fully understand the issue, suggest you look up what 'definition'
and 'declaration' mean and how they are different.

Basically, a definition can occur only once whereas declarations can
occur several times.

Say you have a header file "Foo.h" that contains a definition of a
variable. Every C file that includes Foo.h lands up defining that
variable. This is not allowed.

On the other hand if you have a declaration (your 'extern' is a
declaration) in a header file, exactly one C file has its definition
and other C files that include your header file won't be duplicating
that definition.

I hope this made some sense. There is a lot more to all of this and
it can all get quite murky.

- Anand
 
I

idkfaidkfaidkfa

This is an example of the code

DATA.C FILE
#include "DATA.h"
const int datavalues[]={213,434,657,/*very big array*/};
measure first_measure={5,5,2009,&datavalues}

DATA.H FILE
//definition of struct measure
typedef struct measure={int month,int day, int year, int *pData};
//extern of struct first_measure
extern measure first_measure;

MAIN.C FILE
#include "DATA.h"
//do something with first_measure

I want to avoid to put all const data in a .c file, because these data
can never be changed...A better solution is present or not?
 
F

Flash Gordon

This is an example of the code

DATA.C FILE
#include "DATA.h"
const int datavalues[]={213,434,657,/*very big array*/};
measure first_measure={5,5,2009,&datavalues}

DATA.H FILE
//definition of struct measure
typedef struct measure={int month,int day, int year, int *pData};
//extern of struct first_measure
extern measure first_measure;

All fine and standard.
MAIN.C FILE
#include "DATA.h"
//do something with first_measure

I want to avoid to put all const data in a .c file, because these data
can never be changed...A better solution is present or not?

Code can't be changed at run time either, and you put that in a .c file!
Anyway, you could in your .c file do:
const int datavalues[] = {
#include "values.dat"
};
 
I

idkfaidkfaidkfa

Thanks for infos...
Code can't be changed at run time either, and you put that in a .c file!
Anyway, you could in your .c file do:
const int datavalues[] = {
#include "values.dat"};

Anoter question: is it possible to do the same thing with a .row file?
Is it possible to include a .row file without conversion?
 
K

Keith Thompson

Code can't be changed at run time either, and you put that in a .c file!
Anyway, you could in your .c file do:
const int datavalues[] = {

Anoter question: is it possible to do the same thing with a .row file?
Is it possible to include a .row file without conversion?

I don't know what a .row file is (and neither does wotsit.org).

If it contains valid C source, there shouldn't be a problem.
#include "filename.whatever"
is essentially equivalent to inserting the contents of
"filename.whatever" at that point in your code.
 
I

idkfaidkfaidkfa

Code can't be changed at run time either, and you put that in a .c file!
Anyway, you could in your .c file do:
const int datavalues[] = {
Anoter question: is it possible to do the same thing with a .row file?
Is it possible to include a .row file without conversion?

I don't know what a .row file is (and neither does wotsit.org).

I've done a big mistake: .raw (and not .row) file....pardon....
If it contains valid C source, there shouldn't be a problem.

It contains only bytes of data not formatted....My question is: is it
possible to include any file and fetch bytes of this file (regardless
its meaning and without any conversion) in the program?
 
F

Flash Gordon

Code can't be changed at run time either, and you put that in a .c file!
Anyway, you could in your .c file do:
const int datavalues[] = {
Anoter question: is it possible to do the same thing with a .row file?
Is it possible to include a .row file without conversion?
I don't know what a .row file is (and neither does wotsit.org).

I've done a big mistake: .raw (and not .row) file....pardon....

That does not narrow it down.
It contains only bytes of data not formatted....My question is: is it
possible to include any file and fetch bytes of this file (regardless
its meaning and without any conversion) in the program?

Sounds to me like that is binary file, does it really sound like valid C
source?

The basic rule is that if you could not type it in to a source file and
have it do what you want, you can't directly include it in your source!

Either write a program to read your .raw file and output it as a valid C
source file, or make it a file read at run time by your program. For
either of these *you* need to understand the format (I know of .raw
files which *do* contain structured data) and then use some combination
of fopen/getc/fgetc/fread/fclose and whatever else is required to read
and interpret the file.

If you make a reasonable attempt and port it here together with a
definition of the formatting of the .raw file people can help you with it.
 
K

Keith Thompson

Code can't be changed at run time either, and you put that in a .c file!
Anyway, you could in your .c file do:
const int datavalues[] = {
Anoter question: is it possible to do the same thing with a .row file?
Is it possible to include a .row file without conversion?

I don't know what a .row file is (and neither does wotsit.org).

I've done a big mistake: .raw (and not .row) file....pardon....

Ok, but a file whose name ends in ".raw" could still be just about
anything.
It contains only bytes of data not formatted....My question is: is it
possible to include any file and fetch bytes of this file (regardless
its meaning and without any conversion) in the program?

No. You can read the file during program execution, or you can
translate it to C source and include it. (Some file formats, such as
..xbm and .xpm, are designed to be compatible with C syntax, but most
are not.)
 
J

JosephKK

Code can't be changed at run time either, and you put that in a .c file!
Anyway, you could in your .c file do:
const int datavalues[] = {
Anoter question: is it possible to do the same thing with a .row file?
Is it possible to include a .row file without conversion?

I don't know what a .row file is (and neither does wotsit.org).

I've done a big mistake: .raw (and not .row) file....pardon....
If it contains valid C source, there shouldn't be a problem.

It contains only bytes of data not formatted....My question is: is it
possible to include any file and fetch bytes of this file (regardless
its meaning and without any conversion) in the program?

That sounds similar to a memory mapped file. But it would be
difficult to protect the data (structure) reliably. And it certainly
would not be portable.
 
K

Keith Thompson

JosephKK said:
(e-mail address removed) writes:
Code can't be changed at run time either, and you put that in a .c file!
Anyway, you could in your .c file do:
const int datavalues[] = {

Anoter question: is it possible to do the same thing with a .row file?
Is it possible to include a .row file without conversion?

I don't know what a .row file is (and neither does wotsit.org).

I've done a big mistake: .raw (and not .row) file....pardon....
If it contains valid C source, there shouldn't be a problem.

It contains only bytes of data not formatted....My question is: is it
possible to include any file and fetch bytes of this file (regardless
its meaning and without any conversion) in the program?

That sounds similar to a memory mapped file.

It does? I don't see the resemblance.

I think the OP has an existing data file whose contents he wants to
incorporate into his program at compile time. I don't think there's
any concern about the contents of the file changing unexpectedly.
This requires the contents of the file to be syntactically valid C,
which is possible for some formats (.xbm, .xpm), but it's very
unlikely to work with a .raw file, whatever that may be.
But it would be
difficult to protect the data (structure) reliably. And it certainly
would not be portable.

I'm not sure what you mean by "protect the data".
 
J

JosephKK

JosephKK said:
(e-mail address removed) writes:
Code can't be changed at run time either, and you put that in a ..c file!
Anyway, you could in your .c file do:
const int datavalues[] = {

Anoter question: is it possible to do the same thing with a .row file?
Is it possible to include a .row file without conversion?

I don't know what a .row file is (and neither does wotsit.org).

I've done a big mistake: .raw (and not .row) file....pardon....

If it contains valid C source, there shouldn't be a problem.

It contains only bytes of data not formatted....My question is: is it
possible to include any file and fetch bytes of this file (regardless
its meaning and without any conversion) in the program?

That sounds similar to a memory mapped file.

It does? I don't see the resemblance.

Let's see, read into memory without any translation, mapped bit for
bit to a file. I suppose a big buffer that goes around part of the
file system could do it. It has minor significance if it is at
compile time or run time.
I think the OP has an existing data file whose contents he wants to
incorporate into his program at compile time.

That does seem to be true.
I don't think there's
any concern about the contents of the file changing unexpectedly.
This requires the contents of the file to be syntactically valid C,
which is possible for some formats (.xbm, .xpm), but it's very
unlikely to work with a .raw file, whatever that may be.


I'm not sure what you mean by "protect the data".

A memory mapped file is not all that easy to reliably protect from
accidental writes. These would then be transferred back to the file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,813
Messages
2,569,697
Members
45,488
Latest member
MohammedHa

Latest Threads

Top