I am trying to access a field from a struct through a pointer of pointer
I tried:
(**vform).vpolygon->point->x = 1;
But doesn't worked.
code:
typedef struct point{
int x;
}point;
typedef struct polygon{
point *point;
}polygon;
typedef struct form{
polygon **polygon;
}form;
void main(){
polygon *vpolygon;
form **vform;
vpolygon = (polygon *)malloc(sizeof(polygon)*5);
vform = (form **)malloc(sizeof(form)*5);
vpolygon->point->x = 1;
vform->vpolygon->point->x = 1;
}
I expect the program compile, but the actual output is
18:12: error: '*vform' is a pointer; did you mean to use '->'?
vform->vpolygon->point->x = 1;
^~
->
The code below should compile in what you seem to want. There are however a lot of issues with what you're doing that may not be immediately obvious, but I'll go into that a little below.
typedef struct point {
int x;
} point;
typedef struct polygon {
// dynamically allocated array of point
point *point;
} polygon;
typedef struct form {
// dynamically allocated array of polygon
polygon *polygon;
} form;
form *vform;
vform = malloc(sizeof(form) * 5);
vform->polygon = malloc(sizeof(polygon) * 5);
vform->polygon->point = malloc(sizeof(point) * 5);
vform->polygon->point->x = 1;
First issue is that you're mixing up your variables with the members of your structures. Your form structure has no element called "vpolygon". Just because your "vpolygon" variable is a polygon structure doesn't mean you can suddenly refer to the polygon member of a form variable by calling it "vpolygon".
Secondly, you're casting the return value of malloc. Don't do this. See do I cast the result of malloc
Thirdly, your form malloc is allocationing enough memory for 5 form structures, but casting the result to a pointer to a pointer to form. The malloc should possibly be malloc(sizeof(form *) * 5) but I'm guessing you really meant (vform *)malloc... not (vform **)malloc....
Fourth, you're dynamically allocating arrays and pointing your variables at the first element in the array but you don't seem to have any mechanism to detect the size of the array. You need a method to track the size of the array so you don't accidentally cause a segmentation fault by going off the end of the array. (Three common methods: 1) track the number of elements allocated. 2) Mark the end of the array with some marker value and check for if, 3) hard-code it. All have trade-offs)
Fifth. You want to store the original value returned by malloc so you can free it at some point - if you modify what vfrom points to you'll lose that original address and cause memory leaks.
Related
I have been trying to figure out how to assign an address to an array of structures in C I have managed to have a limited amount of success. Here is my struct:
struct pdirectory {
uint16_t one;
uint8_t two;
uint8_t three;
uint16_t four;
};
Here is my first attempt at declaring my struct variable:
struct pdirectory *test = (struct pdirectory *)0x00001000;
Which worked fine and placed my struct at the memory location expected.
However I can't successfully assign an address to an array of structures.
I have tired the following code with limited success:
struct pdirectory (*test)[1] = (struct pdirectory *)0x00001000;
Which places my struct at the intended address and I can even access different arrays with the following test code:
void initialise_virtual_manager(){
test[0]->one = 0x3333;
test[0]->four = 0x3333;
test[1]->one = 0x3434;
test[1]->four = 0x3434;
test[2]->one = 0x3535;
test[2]->four = 0x3535;
}
I know my code above is not right. I tired to change the array number in the struct deceleration which offsets the array entries in memory by the struct size (which is not what I am intending to do) I want to declare an array of 1024 strut's . Also with the above code I can access an unlimited amount of array's (which is not what I want, I want the array number to limit the amount of array's I can access). If someone can see what I am doing wrong or has any other suggestion to solve this problem I am having, that would be a big help.
For one, use the proper cast, since you seem to know how to point to an array:
struct pdirectory (*test)[N] = (struct pdirectory (*)[N])0x00001000;
Secondly, test[i] will performs pointer arithmetic in units of the array size. If the array is size N, then the array access will be equivalent to:
*(struct pdirectory(*)[N])((unsigned char*)test + i * sizeof(struct pdirectory) * N)
As you can see the above doesn't index the array that test points to, but treats test as an array.
Your array access should therefore be as follows:
(*test)[0].one = 0x3333;
(*test)[0].four = 0x3333;
First test is dereferenced, and only then the array is indexed.
I have a struct defined like this:
typedef struct
{
int num;
char letter;
}* Foo;
And an array like this:
Foo* items = malloc(sizeof(Foo) * 4);
From my understanding and from the (accepted) answer to this question Dynamic array of pointers to structs, I would expect the above line to only reserve the memory for 4 Foo items, but doesn't initialize it - i.e., if I try to access items[i]->num, I should get some kind of error.
Also, in order to insert item into this array, I should do this:
items[0] = malloc(sizeof(*items[0]));
However, I did a little test, and seems like the following code prints 1 and a just fine:
Foo* items = malloc(sizeof(Foo) * 2);
items[0]->num = 4;
items[0]->letter = 'a';
printf("items[0] = {num=%d, char=%c}\n", items[0]->num, items[0]->letter);
I'm confused. Is this the expected behavior?
Your initial malloc:
Foo* items = malloc(sizeof(Foo) * 4);
Is creating an array of 4 pointers, since Foo is a pointer type. So your second malloc:
items[0] = malloc(sizeof(*items[0]));
Makes sense, since you're allocating a struct to that pointer.
However, the assignment you're doing leads to undefined behavior because you didn't do the second malloc and therefore no space has been allocated to items[0] yet. C won't prevent you from writing to a memory location you shouldn't be writing to. And once you do that, anything can happen.
One thing that's a bit confusing here is that you used typedef to define a pointer type. That can lead to a lot of confusion since it's not apparent by looking at the type that it's a pointer. And in this case, because of how you defined Foo, you had an extra layer of pointer indirection you probably don't need.
So if you define your struct like this:
typedef struct
{
int num;
char letter;
} Foo;
Then this can be done safely:
Foo* items = malloc(sizeof(Foo) * 2);
items[0].num = 4;
items[0].letter = 'a';
printf("items[0] = {num=%d, char=%c}\n", items[0].num, items[0].letter);
Now the malloc creates an array of structs instead of an array of pointers to structs, so an additional layer of mallocs is no longer necessary.
You have to allocate structs and save its pointer to items's elements if you want to use structs. Otherwise, the item's elements are junk and access to it may cause errors.
The second test may worked fine due to the optimization which removes malloc and pass the values of items[0]->num and items[0]->letter directly to printf.
This is happening because you have reserved enough space for the Foo array and probably one element but it's not the expected behavior, there is no expected behavior in this case because what you do invokes undefined behavior.
This will fail if you add more fields to the struct, because then 2 * sizeof(void *) will not be enough. To test it, try adding 2 pointers to the struct like this1
typedef struct
{
int num;
char letter;
void *pointers[2];
} *Foo;
If you want you can do it right, and this is another reason not to typedef a pointer, this would work
typedef struct
{
int num;
char letter;
void *pointers[2];
} Foo;
Foo *foo_pointer = malloc(N * sizeof(Foo));
/* ^ this would be wrong if `Foo' were a pointer */
if (foo_pointer == NULL)
please_abort_this_do_not_continue_because_malloc_has_failed();
foo_pointer[0].num = 1;
foo_pointer[0].letter = 'a';
1It really annoys me to write this, because typedefing a pointer is never a good idea
I'm working my way through the learn c the hard way book and have run into a few issues on Exercise 19. The author said that ex19 was intended for the learners to get to know the macro in c. I have no problem in understanding the concept of that, but I just don't understand everything else. I can't understand how the object prototype is created.
Especilly,what does the following sentense mean?
Since C puts the Room.proto field first, that means the el pointer is
really only pointing at enough of the block of memory to see a full
Object struct. It has no idea that it's even called proto.
the relevant code is this:
// this seems weird, but we can make a struct of one size,
// then point a different pointer at it to "cast" it
Object *el = calloc(1, size);
*el = proto;
can anyone tell me how on earth malloc/calloc exactly works? As far as i know, it just allocate the required number of memory and return the first address. If so, how can the computer know the data struct of the allocated memory? like in the code, after Room *arena = NEW(Room, "The arena, with the minotaur");,you can do this directly arena->bad_guy = NEW(Monster, "The evil minotaur"); how does the computer know there is a bad_guy??
what on earth is the content of *el after the above two statements(Object *el = calloc(1, size); and *el = proto;)?
Any help will be appreciated!!
the link to the exercise: http://c.learncodethehardway.org/book/ex19.html
calloc has the additional feature that it fills the allocated memory with zero bytes, whereas using the equivalent malloc call would require an additional step if all or some of the allocation needs to be zero initially.
In the code
arena->bad_guy = NEW(Monster, "The evil minotaur");
the compiler knows the layout of the struct because the access is through the arena variable, which is declared as a pointer to Room, which is presumably a typedef of a struct.
For the other part, the guarantee of ordering within structs allows a limited form of inheritance in composite structs, or extended structs.
struct A {
int x;
};
struct B {
int foo;
double baloney;
};
struct B (or a pointer to it) can be cast to a (pointer to a) struct A because they both begin with an int. Of course, if you cast the other way, the struct A must have been originally a struct B or access to the baloney field will be undefined. In other words, struct B essentially begins with a struct A.
This may be easier to see if I rewrite my example like this:
struct A {
int x;
};
struct B {
struct A foo;
double baloney;
};
Now you can get a struct A out of struct B in different ways.
struct A a;
struct B b;
a = b.foo; // regular member variable access
struct A *ap = &a;
struct B *bp = &b;
ap = (struct A *)bp; // cast the pointer
ap = & b.foo; // take a pointer from the member variable
ap = & bp->foo; // take a pointer from the member variable via a pointer
All it does is to alloc 1*size bytes. There's nothing magic with malloc/calloc. He is passing the sizeof(T) to the function through that NEW macro and putting it in Object_new's size parameter. So all the function knows is the size in bytes.
Suppose I have the following structs:
typedef struct plane_t Plane;
struct plane_t{
Point p1;
Point p2;
Point p3;
};
typedef struct arrangement_t* Arrangement;
struct arrangement_t{
//TODO add fields here
int maxPlanes;
int curPlanes;
Plane *planes;
};
And I have the following function:
Plane planeCreate(Point point1, Point point2, Point point3){
Plane newPlane = {{point1.x, point1.y, point1.z}, {point2.x, point2.y, point2.z}, {point3.x, point3.y, point3.z}};
return newPlane;
}
Arrangement arrangementCreate(int maxPlanes){
if (maxPlanes < 1) return NULL;
Arrangment newArrangment = malloc(sizeof struct arrangement_t);
if (newArrangment == NULL) return NULL;
newArrangment->planes = malloc(sizeof(Plane)*maxPlanes);
if (newArrangment->planes == NULL) {
free(newArrangment);
return NULL;
}
newArrangment->maxPlanes = maxPlanes;
newArrangment->curPlanes = 0;
return newArrangment;
}
Will the following line mean that every cell within the array will have a stuct of type Plane, or I still have to go over each cell in manually create them one by one?
newArrangment->planes = malloc(sizeof(Plane)*maxPlanes);
malloc will allocate enough space for an array of maxPlanes structs, but it's up to you to initialize them.
In other words, if malloc succeeds, you will be able to access the structs newArrangement->planes[0] through newArrangement->planes[maxPlanes-1]. The structs are laid out end-to-end in memory as one contiguous block.
Your code is probably (see below) fine (by "fine" I mean functional, not well-designed). Your malloc(sizeof(Plane)*maxPlanes) will allocate space for maxPlanes planes, and you do not have to allocate each cell.
The cells themselves don't "have a struct of type Plane", it is merely a block of memory large enough to hold the information stored in maxPlanes Plane structs. Since you are accessing that block via a Plane pointer, the data will be interpreted as a Plane struct.
I say probably because your Plane struct members will be uninitialized (will contain random data), and since you don't show what a Point is, there is not enough information to know if what you have is sufficient. You also do not show what you are going to end up doing with the arrangement_t, so I presume that it is acceptable to leave the values uninitialized and that you are setting the values to something meaningful later.
I'd also make a number of recommendations to clarify your code:
You typedef Arrangement as a pointer but Plane as a struct. I suggest perhaps qualifying your Arrangement type as ArrangementPtr or something instead to make the distinction and reduce confusion.
Consider using calloc instead of malloc for clarity: calloc(maxPlanes, sizeof(Plane))
If zeroing the memory is acceptable to you for initialization, you can do a quick memset(planes, 0, sizeof(Plane)*maxPlanes).
Unless you have a specific reason to use C (there are many), you may wish to consider C++ (with STL classes if they are available to you), which will greatly reduce a lot of the work and possible sources of error in your code (note this is still not ideal because public members can break invariants that you may have, but just as an example):
struct Plane {
Point p1;
Point p2;
Point p3;
Plane ();
Plane (const Point &, const Point &, const Point &);
};
struct Arrangement {
int maxPlanes;
int curPlanes;
std::vector<Plane> planes;
explicit Arrangement (int maxPlanes);
};
Plane::Plane () {
}
Plane::Plane (const Point &p1, const Point &p2, const Point &p3) :
p1(p1), p2(p2), p3(p3)
{
}
Arrangement::Arrangement (int maxPlanes) :
maxPlanes(maxPlanes),
curPlanes(0),
planes(maxPlanes)
{
}
That will handle all memory management for you; your job would be to catch std::bad_alloc to check for memory allocation errors, or add any necessary parameter validation.
Your code has a number of other problems, or potential for problems, but that's outside the scope of this question I think.
I have to use the following block of code for a school assignment, STRICTLY WITHOUT ANY MODIFICATIONS.
typedef struct
{
char* firstName;
char* lastName;
int id;
float mark;
}* pStudentRecord;
pStudentRecord* g_ppRecords;
int g_numRecords =0;
Here g_ppRecords is supposed to be an array of pointers to structs. What I am completely failing to understand is that how can the statement pStudentRecords *g_ppRecords; mean g_ppRecords to be an array because an array should be defined as
type arrayname[size];
I tried allocating memory to g_ppRecords dynamically, but that's not helping.
g_ppRecords = (pStudentRecord*) malloc(sizeof(pStudentRecord*)*(g_numRecords+1));
EDIT: updated the "BIG MISTAKE" section.
A quick lesson on C-style (different from C++!) typedefs, and why it is how it is, and how to use it.
Firstly, a basic typedef trick.
typedef int* int_pointer;
int_pointer ip1;
int *ip2;
int a; // Just a variable
ip1 = &a; // Sets the pointer to a
ip2 = &a; // Sets the pointer to a
*ip1 = 4; // Sets a to 4
*ip2 = 4; // Sets a to 4
ip1 and ip2 are the same type: a pointer-to-type-int, even though you didn't put a * in the declaration of ip1. That * was instead in the declaration.
Switching topics.
You speak of declaring arrays as
int array1[4];
To do this dynamically at runtime, you might do:
int *array2 = malloc(sizeof(int) * 4);
int a = 4;
array1[0] = a;
array2[0] = a; // The [] implicitly dereferences the pointer
Now, what if we want an array of pointers? It would look like this:
int *array1[4];
int a;
array1[0] = &a; // Sets array[0] to point to variable a
*array1[0] = 4; // Sets a to 4
Let's allocate that array dynamically.
int **array2 = malloc(sizeof(int *) * 4);
array2[0] = &a; // [] implicitly dereferences
*array2[0] = 4; // Sets a to 4
Notice the int **. That means pointer-to pointer-to-int. We can, if we choose, use a pointer typedef.
typedef int* array_of_ints;
array_of_ints *array3 = malloc(sizeof(array_of_ints) * 4);
array3[0] = &a; // [] implicitly dereferences
*array3[0] = 4; // Sets a to 4
See how there's only one * in that last declaration? That's because ONE of them is "in the typedef." With that last declaration, you now have an array of size 4 that consists of 4 pointers to ints (int *).
It's important to point out OPERATOR PRECEDENCE here. The dereference operator[] takes preference over the * one. SO to be absolutely clear, what we're doing is this:
*(array3[0]) = 4;
Now, let's change topics to structs and typedefs.
struct foo { int a; }; // Declares a struct named foo
typedef struct { int a; } bar; // Typedefs an "ANONYMOUS STRUCTURE" referred to by 'bar'
Why would you ever typedef an anonymous struct? Well, for readability!
struct foo a; // Declares a variable a of type struct foo
bar b; // Notice how you don't have to put 'struct' first
Declaring a function...
funca(struct foo* arg1, bar *arg2);
See how we didn't have to put 'struct' in front of arg2?
Now, we see that the code you have to use defines a structure IN THIS MANNER:
typedef struct { } * foo_pointers;
That is analogous to how we did an array of pointers before:
typedef int* array_of_ints;
Compare side-by-side
typedef struct { } * foo_pointers;
typedef int* array_of_ints;
The only difference is that one is to a struct {} and the other is to int.
With our foo_pointers, we can declare an array of pointers to foo as such:
foo_pointers fooptrs[4];
Now we have an array that stores 4 pointers to an anonymous structure that we can't access.
TOPIC SWITCH!
UNFORTUNATELY FOR YOU, your teacher made a mistake. If one looks at the sizeof() of the type foo_pointers above, one will find it returns the size of a pointer to that structure, NOT the size of the structure. This is 4 bytes for 32-bit platform or 8 bytes for 64-bit platform. This is because we typedef'd a POINTER TO A STRUCT, not a struct itself. sizeof(pStudentRecord) will return 4.
So you can't allocate space for the structures themselves in an obvious fashion! However, compilers allow for this stupidity. pStudentRecord is not a name/type you can use to validly allocate memory, it is a pointer to an anonymous "conceptual" structure, but we can feed the size of that to the compiler.
pStudnetRecord g_ppRecords[2];
pStudentRecord *record = malloc(sizeof(*g_ppRecords[1]));
A better practice is to do this:
typedef struct { ... } StudentRecord; // Struct
typedef StudentRecord* pStudentRecord; // Pointer-to struct
We'd now have the ability to make struct StudentRecord's, as well as pointers to them with pStudentRecord's, in a clear manner.
Although the method you're forced to use is very bad practice, it's not exactly a problem at the moment. Let's go back to our simplified example using ints.
What if I want to be make a typedef to complicate my life but explain the concept going on here? Let's go back to the old int code.
typedef int* array_of_ints;
int *array1[4];
int **array2 = malloc(sizeof(int *) * 4); // Equivalent-ish to the line above
array_of_ints *array3 = malloc(sizeof(array_of_ints) * 4);
int a, b, c, d;
*array1[0] = &a; *array1[1] = &b; *array1[2] = &c; *array1[3] = &d;
*array2[0] = &a; *array2[1] = &b; *array2[2] = &c; *array2[3] = &d;
*array3[0] = &a; *array3[1] = &b; *array3[2] = &c; *array3[3] = &d;
As you can see, we can use this with our pStudentRecord:
pStudentRecord array1[4];
pStudentRecord *array2 = malloc(sizeof(pStudentRecord) * 4);
Put everything together, and it follows logically that:
array1[0]->firstName = "Christopher";
*array2[0]->firstName = "Christopher";
Are equivalent. (Note: do not do exactly as I did above; assigning a char* pointer at runtime to a string is only OK if you know you have enough space allocated already).
This only really brings up one last bit. What do we do with all this memory we malloc'd? How do we free it?
free(array1);
free(array2);
And there is a the end of a late-night lesson on pointers, typedefs of anonymous structs, and other stuff.
Observe that pStudentRecord is typedef'd as a pointer to a structure. Pointers in C simply point to the start of a memory block, whether that block contains 1 element (a normal "scalar" pointer) or 10 elements (an "array" pointer). So, for example, the following
char c = 'x';
char *pc = &c;
makes pc point to a piece of memory that starts with the character 'x', while the following
char *s = "abcd";
makes s point to a piece of memory that starts with "abcd" (and followed by a null byte). The types are the same, but they might be used for different purposes.
Therefore, once allocated, I could access the elements of g_ppRecords by doing e.g. g_ppRecords[1]->firstName.
Now, to allocate this array: you want to use g_ppRecords = malloc(sizeof(pStudentRecord)*(g_numRecords+1)); (though note that sizeof(pStudentRecord*) and sizeof(pStudentRecord) are equal since both are pointer types). This makes an uninitialized array of structure pointers. For each structure pointer in the array, you'd need to give it a value by allocating a new structure. The crux of the problem is how you might allocate a single structure, i.e.
g_ppRecords[1] = malloc(/* what goes here? */);
Luckily, you can actually dereference pointers in sizeof:
g_ppRecords[1] = malloc(sizeof(*g_ppRecords[1]));
Note that sizeof is a compiler construct. Even if g_ppRecords[1] is not a valid pointer, the type is still valid, and so the compiler will compute the correct size.
An array is often referred to with a pointer to its first element. If you malloc enough space for 10 student records and then store a pointer to the start of that space in g_ppRecords, g_ppRecords[9] will count 9 record-pointer-lengths forward and dereference what's there. If you've managed your space correctly, what's there will be the last record in your array, because you reserved enough room for 10.
In short, you've allocated the space, and you can treat it however you want if it's the right length, including as an array.
I'm not sure why you're allocating space for g_numRecords + 1 records. Unless g_numRecords is confusingly named, that's space for one more in your array than you need.
Here g_ppRecords is supposed to be an array of pointers to structs. What I am completely failing to understand is that how can the statement *pStudentRecords g_ppRecords; mean g_ppRecords to be an array. as an array should be defined as
type arrayname[size];
umm type arrayname[size]; is one way of many ways to define an array in C.
this statically defines an array, with most of the values being stored on the stack depending the location of it definition, the size of the array must be known at compile time, though this may no longer be the case in some modern compilers.
another way would be to dynamically create an array at runtime, so we don't have to know the size at compile time, this is where pointers come in, they are variables who store the address of dynamically allocated chunks of memory.
a simple example would be something like this type *array = malloc(sizeof(type) * number_of_items); malloc returns a memory address which is stored in array, note we don't typecast the return type for safety reasons.
Going back to the problem at hand.
typedef struct
{
char* firstName;
char* lastName;
int id;
float mark;
}* pStudentRecord;
pStudentRecord* g_ppRecords;
int g_numRecords = 0;
this typedef is a bit different from most note the }* basically its a pointer to a struct so this:
pStudentRecord* g_ppRecords;
is actually:
struct
{
char* firstName;
char* lastName;
int id;
float mark;
}** pStudentRecord;
its a pointer to a pointer, as to why they would define the typedef in this way, its beyond me, and I personally don't recommend it, why?
well one problem woud be how can we get the size of the struct through its name? simple we can't! if we use sizeof(pStudentRecord) we'll get 4 or 8 depending on the underlying architecture, because thats a pointer, without knowing the size of the structure we can't really dynamically allocated it using its typedef name, so what can we do, declare a second struct such as this:
typedef struct
{
char* firstName;
char* lastName;
int id;
float mark;
} StudentRecord;
g_ppRecords = malloc(sizeof(StudentRecord) * g_numRecords);
Either way you really need to contact the person who original created this code or the people maintaining and raise your concerns.
g_ppRecords=(pStudentRecord) malloc( (sizeof(char*) +
sizeof(char*) +
sizeof(int) +
sizeof(float)) *(g_numRecords+1));
this may seem like one possible way, unfortunately, there are no guarantees about structs, so they can actually containg padding in between the members so the total size of the struct can be actually larger then its combined members, not to mention there address would probably differ.
EDIT
Apparently we can get the size of the struct by simply inferring its type
so:
pStudentRecord g_ppRecords = malloc(sizeof(*g_ppRecords) * g_numRecords);
works fine!