How can use __attribute__ (section) for each element of an array? - c

I have a multidimensional array and I want to define each element of this array in a different section. Something like:
int array [2][200];
array[0] __attribute__((section (".section1")));
array[1] __attribute__((section (".section2")));
I know this piece of code is not correct because __atribute__ ((section "section name")) should be used in the array definition. Do you have any suggestions on how I can do it?
Thanks in advance

No. The array is a contigous chunk of memory and elements cannot be in different sections.
C standard (6.2.5.20):
An array type describes a contiguously allocated nonempty set of
objects with a particular member object type, called the element type.
Do you have any suggestions on how I can do it?
You need to have two separate arrays in different sections and array of two pointers referencing those arrays.

Related

Overwriting an existing 2D Array in C

I'm currently writing a project in C, and I need to be able to fill a 2D array with information already stored in another 2D array. In a separate C file, I have this array:
int levelOne[][4] =
{{5,88,128,0},
{153,65,0,0},
{0,144,160,20}}; //First Array
int levelTwo[][4] =
{{5,88,128,0},
{153,65,0,0},
{0,144,160,20}}; //Second Array
And in my main file, I have this variable which I'd like to fill with the information from both of these arrays at different points in my code. (This isn't exactly what I'm doing, but it's the general gist):
#include "arrayFile.c"
void main()
{
int arrayContainer[][4] = levelOne;
while (true)
{
func(arrayContainer);
if(foo)
{
arrayContainer = levelTwo;//Switches to the other array if the conditional is met.
}
}
}
I know this method doesn't work - you can't overwrite items in arrays after they're instantiated. But is there any way to do something like this? I know I'll most likely need to use pointers to do this instead of completely overwriting the array, however there's not a lot of information on the internet about pointers with multidimensional arrays. In this situation, what's best practice?
Also, I don't know exactly how many arrays of 4 there will be, so I wouldn't be able to use a standard 3D array and just switch between indexes, unless there's a way to make a 3D jagged array that I don't know about.
Given the definitions you show, such as they are, all you need is memcpy(arrayContainer, levelTwo, sizeof LevelTwo);.
You should ensure that arrayContainer has sufficient memory to contain the copied data and that LevelTwo, since it is used as the operand of sizeof, is a designator for the actual array, not a pointer. If it is not, replace sizeof LevelTwo with the size of the array.
If you do not need the actual memory filled with data but simply need a way to refer to the contents of the different arrays, make arrayContainer a pointer instead of an array, as with int (*arrayContainer)[4];. Then you can use arrayContainer = levelOne; or arrayContainer = levelTwo; to change which data it points to.
Also, I don't know exactly how many arrays of 4 there will be, so I wouldn't be able to use a standard 3D array and just switch between indexes, unless there's a way to make a 3D jagged array that I don't know about.
It is entirely possible to have a pointer to dynamically allocated memory which is filled with pointers to arrays of four int, and those pointers can be changed at will.

Is the stride of a char[] guaranteed to be 1 if the char[] is inside a struct __attribute__((aligned))?

So let's say I have a struct that looks like this
(pretty common in the real world, it turns out):
struct foo {
char[24] bar;
uint32_t fnord;
uint32_t quux;
}__attribute__((aligned(4));
What is the stride of bar, that is, what is &bar[1] - &bar[0],
given that it's in struct foo?
This has implications for sizeof(foo), which I'm pretty sure I wanted
to be 32, and I also wanted nice fast aligned operations on foo.fnord and foo.quux, or it wouldn't be aligned in the first place.
Per paragraph 6.2.5/20 of the standard,
An array type describes a contiguously allocated nonempty set of
objects with a particular member object type
(Emphasis added.) Thus, the elements of an array are always contiguous in memory. That is among the defining characteristics of an array. Linkage, storage class, membership in another data structure, alignment requirement of the array itself or of any data structure containing it -- none of these affect array elements' contiguity.
The alignment requirement of an array is normally a multiple of that of its element type, so that aligning the array itself also aligns all its elements. In no case are array elements subject to individual alignment.

How to define an array having 2 columns of strings

Is it possible to define and initialize 2 columns of strings with a single array? I mean I want to initialize an array with following values:
{"Cp", "Mu", "H", "Si"} -> Column-1
{"Specific Heat", "Viscosity", "Enthalpy", "Surface Tension") -> Column-2
How can I do it? Will it be easier by using pointers?
You could perhaps use an array of a structure, as so,
struct property{
char col1[size_of_row];
char col2[size_of_second_row];
};
struct property list[size_of_list];
Or, rather, if the number of elements in the list is not known, you could use an array of pointers, with each pointer pointing to a node with a property under column 1 and column 2. You can refer to dynamic array of dynamically allocated structs

Julia - fixed size array in C struct

I need to make a Julia type corresponding to a C struct that has a fixed size array:
struct cstruct {
...
int arr[N] //N known at compile time
...
};
I have defined Julia types corresponding to other C structs with arrays like this:
type jstruct
...
arr::Ptr{Cint}
...
end
But as I understand it, this only works when arr is a pointer, not an array of a specific size. How can I ensure that the offsets of elements coming after arr remain the same in both languages?
When you define a C struct with a fixed size array (or with the array hack), the data are stored directly inline within that struct. It's not a pointer to another region. The equivalent Julia structure is:
type JStruct{N}
arr::NTuple{N,Int}
end
That will store the integers directly inline within the struct.
Note that if you want array-type operations on this object in Julia, the StaticArrays package might be useful. It uses tuples to store the elements of arrays, while also giving them an AbstractArray interface.

Access two-dimensional array via pointer to first element

I got strucure
typedef struct
{char *cells;}
Map;
and cells suppose to be pointer to array of rows(in rows are integers on every position).
I don't know how to access for example to number on 3. position in 2. row.
I have stared with some array[3][3], but I don't know how to connect them with this struct.
I tried
Map nextmap;
nextmap.cells[0] = array[0][0];
But I got only first number, which is clear. How can I get to other positions?
Thanks in advance.
EDIT: renaming the structure ..
.
When you did Map nextmap;, you created an uninitialized Map struct. When you did nextmap.cells[0] = array[0][0]; you dereferenced (i.e. followed) the uninitialized pointer, and stored a value at the random memory it points at.
If you want to initialize the cells structure, you can do something as simple as nextmap.cells = array[0]; That will cause nextmap.cells to point at array. Note that it's not copying the contents; just pointing at them. That means that if you change the values through cells, you'll be modifying the values in arrays.
(Also, using 'new' as a variable name is perfectly acceptable in C, but you're likely to confuse any C++ programmers reading your code, since 'new' is an operator in that language.)
new now changed to nextmap in question
Edited to correct the type mismatch in nextmap.cells assignment.
Given an array char array[][NumberOfColumns] (the first dimension is irrelevant and is omitted here; it would be needed when the array is defined), you can set a pointer to the first element of the array with:
nextmap.cells = &array[0][0];
Then you can access an element in the array, array[i][j], by calculating its position within the array, with either of these two expressions:
*(nextmap.cells + i*NumberOfColumns + j)
nextmap.cells[i*NumberOfColumns + j]
Two-dimensional arrays generally ought to be addressed as two-dimensional arrays. Calculating the position manually is poor practice if done without good reason. If this school assignment did not have a good reason for this, then it is a bad assignment.
First of all new is not a good name for a variable.
new now changed to nextmap in question
Second of all in your case cells should be a double pointer, like this
char ** cells;
Or a pointer to a 2D array, like
char (*cells)[N][N];
where N is a constant you want to use.

Resources