I am using the CvPoint2D32f struct of opencv, which is defined as
typedef struct CvPoint2D32f
{
float x; /* x-coordinate, usually zero-based */
float y; /* y-coordinate, usually zero-based */
}
CvPoint2D32f;
I am trying to allocate an array of pointers of these objects as follows, and it is not working. How is this to be accomplished?
CvPoint2D32f *cvpoints[8];
for (i = 0; i < 16; i++)
{
if (i % 2 == 0)
{
(*cvpoints)[i/2].x = float(points[i]); // points is an array of ints.
}
else
{
(*cvpoints)[i/2].y = float(points[i]);
}
}
You have an array of pointers to CvPoint2D32f, not an array of the structs themselves. You have no memory allocated for the actual structures. You have three options:
Change your declaration to CvPoint2D32f cvpoints[8]; so you have all of the structures in an array on the stack.
After you allocate the array for the pointers, add a malloc statement to allocate memory. Example:
CvPoint2D32f *cvpoints[8];
for (i = 0; i < 8; i++)
{
cvpoints[i] = (CvPoint2D32f *)malloc(sizeof(CvPoint2D32f));
}
Use malloc to allocate size for all the structures. Example:
CvPoint2D32f *cvpoints = (CvPoint2D32f *)malloc( 8 * sizeof(CvPoint2D32f));
CvPoint2D32f *cvpoints = (CvPoint2D32f*) malloc(sizeof(struct CvPoint2D32f) * 8);
memset(cvpoints, \0, sizeof(struct CvPoint2D32f) * 8);
for (i = 0; i < 16; i++)
{
if (i % 2 == 0)
{
cvpoints[i/2].x = float(points[i]); // points is an array of ints.
}
else
{
cvpoints[i/2].y = float(points[i]);
}
}
And when you're done with them, you free the memory:
free(cvpoints);
Where do these pointers point to? I guess what you want to do is allocate dynamic memory for the structs, but I don't see any memory allocation in your code. Currently all pointers point to undefined memory, so of course this will fail.
You are just creating the pointers, but you need them to point to something in memory!
Example:
struct CvPoint2D32f *cvpoint = (struct CvPoint2D32f *)malloc(sizeof(struct CvPoint2D32f));
if (cvpoint == NULL) {
//malloc error
}
Related
I have a struct let say
struct packets {
int tcp_source;
int tcp_dest;
char *ip_source;
char *ip_dest;
int seq;
...
} *pkts;
I am allocating space using malloc in while loop and inside while loop when I done using specific pkts (in pointed to by (ptks+index)) I need to free it like this:
while (i++ < n - 1) {
(pkts+i)=(struct packets *) malloc(sizeof (struct packets))
//shared `i` and signal sending thread to send packet
...
}
// Now I need to free `(pkts+i)` like
while (i < 10)
free((pkts + i)); //Not working
//OR
free(pkts[i]); / Not working either -- in both cases error zsh: abort
Question is if I choose to free specific element at time then what is the right way plus what I need if I choose to free all elements at once
You need to use pkts as an array of pointers to struct packets.
struct packets **pkts = malloc(N * sizeof *pkts);
...
for (int i = 0; i < N; ++i) {
// setting i-th element
pkts[i]=malloc(sizeof pkts[i]);
// accessing i-th element
pkts[i]-> ...
}
for (int i = 0; i < N; ++i) {
// freeing individual element
free(pkts[i]);
pkts[i] = NULL; // avoid accidental accessing or re-freeing
}
// freeing whole array of pointers
free(pkts);
As mentioned in the comments, you probably do not need to allocate each packets individually. Just allocate an array of packets.
struct packets *pkts = malloc(N * sizeof *pkts);
...
free(pkts);
If you use C99-compliant compiler and you have a reasonable bound on the number of packets use VLA.
struct packets pkts[N]; // that's all
I would like some help accessing the arrays in my struct. My program compiles without any errors, but stalls when I try to change some values in my struct. I'm stumped as to what the problem is, and would greatly appreciate some assistance.
Global struct declaration:
typedef struct Data {
float timestamp[array_length];
float azimuth[array_length];
float distance[array_length];
float signal_strength[array_length];
} Datain;
Datain *dataptr;
This is were I try to initialize the arrays to what I would like them to be:
for (i = 0; i < array_length; i++)
{
dataptr->timestamp[i]=-100;
dataptr->distance[i]=-100;
dataptr->azimuth[i]=-100;
dataptr->signal_strength[i]=-100;
}
Let me know what you think
dataptr is an uninitialized pointer, it's pointing to nowhere. You have to
allocate memory for it:
dataptr = malloc(sizeof *dataptr); // or malloc(size * sizeof *dataptr)
// for allocating size Datain objects
if(dataptr == NULL)
{
// error handling
// do not continue
}
for(i=0;i<array_length;i++)
{
dataptr->timestamp[i]=-100;
dataptr->distance[i]=-100;
dataptr->azimuth[i]=-100;
dataptr->signal_strength[i]=-100;
}
/*
or if you do the malloc(size * sizeof *dataptr)
for(size_t i = 0; i < size; ++i)
{
for(size_t j = 0; j < array_length; ++j)
{
dataptr[i].timestamp[j]=-100;
dataptr[i].distance[j]=-100;
dataptr[i].azimuth[j]=-100;
dataptr[i].signal_strength[j]=-100;
}
}
*/
and later you don't have to forget to free the memory with
free(dataptr);
And if dataptr is meant to be a single object, then you don't need to declare
it as a pointer:
Datain dataptr; // no * here
then later in a function
for(i=0;i<array_length;i++)
{
dataptr.timestamp[i]=-100;
dataptr.distance[i]=-100;
dataptr.azimuth[i]=-100;
dataptr.signal_strength[i]=-100;
}
and here you don't need to free the memory.
I'm doing a school assignment, I've I've run into 2 problems. I have to simulate stacks, with arrays.
My current code is as follows:
#include <stdlib.h>
#include <stdio.h>
typedef struct {
int capacity;
int * array;
int size;
} stack_tt;
int pop(stack_tt * stack_p);
void push(stack_tt * stack_p, int value);
int top(stack_tt * stack_p);
stack_tt * newStack(void);
int empty(stack_tt * stack_p);
int main() {
stack_tt * myStack = newStack();
push(myStack, 123);
push(myStack, 99);
push(myStack, 4444);
while (!empty(myStack)) {
int value;
value = pop(myStack);
printf("popped: %d\n", value);
}
return 0; }
stack_tt * newStack(){
stack_tt * newS = malloc(sizeof(stack_tt) * 20);
(*newS).capacity = 1;
(*newS).size = 0;
return newS;
}
void push(stack_tt * stack_p, int value){
if ((*stack_p).size >= (*stack_p).capacity) {
(*stack_p).capacity*=2;
//realloc(stack_p, stack_p->capacity * sizeof(stack_tt));
}
(*stack_p).array = &value;
(*stack_p).size++;
}
int pop(stack_tt * stack_p){
(*stack_p).size--;
int fap = *(*stack_p).array;
return fap;
}
int empty(stack_tt * stack_p){
if ((*stack_p).size >= 1)
return 0;
return 1;
}
Fist of, when I call the line
while(!empty(myStack))
It changes the value in my array to 1.
secondly I'm not able to change individual values in my array, whenever I try things like:
(*stack_p).array[0] = value;
It doesn't know where in the memory to look.
I hope someone is able to help me out :)
There are a couple of problems with the code as I see it.
Lets take the push function where you do
(*stack_p).array = &value;
That will make the array structure member point to the local variable value, and once the function returns the variable cease to exist leaving you with a stray pointer and using that pointer will lead to undefined behavior.
The second problem with that code is that your stack will only be pointing (illegally) to the last element added.
You must allocate memory explicitly for array and use capacity to keep track of how much memory is allocated. The use size as an index into the allocated array for the pushing and popping. Something like
stack_tt * newStack(){
stack_tt * newS = malloc(sizeof(stack_tt)); // Only allocate *one* structure
newS->capacity = 0; // Start with zero capacity
newS->size = 0;
newS->array = NULL;
return newS;
}
void push(stack_tt * stack_p, int value){
if (stack_p->size + 1 > stack_p->capacity){
// Increase capacity by ten elements
int new_capacity = stack_p->capacity + 10;
int * temp_array = realloc(stack_p->array, new_capacity * sizeof(int));
if (temp_srray == NULL)
return;
stack_p->capacity = new_capacity;
stack_p->array = temp_array;
}
stack_p->array[stack_p->size++] = value;
}
int pop(stack_tt * stack_p){
if (stack_p->size > 0)
return stack_p->array[--stack_p->size];
return 0;
}
int empty(stack_tt * stack_p){
return stack_p->size == 0;
}
There is no need to allocate space for 20 structs of type stack_tt, you only need to allocate space for one:
stack_tt * newS = malloc(sizeof(stack_tt));
however you need to allocate space for elements of the struct member array:
newS->array = malloc( sizeof(int)*20);
newS->size = 0;
newS->capacity = 20;
now you can use the array member.
When you push a value to the 'stack', you shouldn't overwrite the array member with the address of the local variable, that doesn't make sense and will cause undefined behavior in addition of loosing the previously allocated memory. Instead simply assign the value to the member array, in the function push:
stack_p->array[stack_p->size] = value;
stack_p->size++;
Similarly when you pop an element, take the current element from the member array:
stack_p->size--;
int fap = stack_p->array[stack_p->size];
The rest of the functions and code should be fixed in the same manner.
You're code is good, but probably you didn't understand the usage of realloc:
//realloc(stack_p, stack_p->capacity * sizeof(stack_tt));
This function returns a pointer to the newly allocated memory, or NULL if the request fails.
The realloc (as the function suggests) takes the memory pointed by the pointer you pass, and copies that memory block in a new and resized block. So the right code should be.
stack_p->array = realloc(stack_p->array, stack_p->capacity * sizeof(stack_tt));
This other line is wrong:
(*stack_p).array = &value;
Change it with:
stack_p->array[stack_p->size] = value;
Another little suggestion, every (*stack_p). can be replaced by stack_p->, which is more elegant.
In the newStack() you're mallocing 20 structs which is kinda useless. You just need one.
Then you should malloc the array for the first time:
newS->array = malloc(sizeof(int));
newS->capacity = 1;
I've been doing research for a while, and I'm not finding anything that helps me.
I have the following struct declarations:
typedef struct position_struct{
int x;
int y;
} pos;
typedef struct item_struct{
char member1;
pos member2;
} item;
typedef struct room_stuct{
item * member3;
pos * member4;
pos member5;
} roomLayout;
And the code to try to malloc it is (I removed the error checking for brevity):
roomLayout *genFloor () {
// Allocate mem for Arrays of:
roomLayout * room = malloc(sizeof(roomLayout) * 6 ); // 6 rooms
room->member3 = malloc(sizeof(item) * 10); // 10 member3's
room->member4 = malloc(sizeof(pos) * 10); // 10 member4's
/* TESTING */
room[0].member3[0].member1 = 'd';
printf("Room[0] is good\n");
room[1].member3[0].member1 = 'd';
printf("Room[1] is good\n"); // Never prints/makes it to this line
return room;
}
When I try this, assigning to room[1] causes a crash, but not room[0]. My guess is that I havent actually allocated enough space for the whole array and only one spot. But I don't understand why as I believe that I'm following what I see everywhere else.
If someone could please explain to me the procedure for allocating memory for this kind of setup, that would be very helpful! Thank you.
You are only allocating member3 and member4 for the first room; not for all of them. In the line room->member3, room is at the beginning of the memory segment, or at room[0], which allocates space for 10 member3s. room[1], however, has not allocated any space for any member3s (similarly for doors). Work around this by allocating in a loop:
for(i = 0; i < 6; ++i)
{
room[i].member3 = malloc(sizeof(room[i].member3) * 10);
room[i].member4 = malloc(sizeof(room[i].member4) * 10);
}
When free-ing the memory, remember to use a similar loop, i.e.
for(i = 0; i < 6; ++i)
{
free(room[i].member3);
free(room[i].member4);
}
free(room);
room->groundItems = malloc(sizeof(item) * 10);
The code above allocate a 10 items for only for room[0].
room[1 to 5] are not allocated and access them is UB.
You must allocate all your pointers before to access them, like showed below
roomLayout * room = malloc(sizeof(roomLayout) * 6 )
for (int i=0; i<6; i++)
{
room[i].groundItems = malloc(sizeof(item) * 10); // 10 ground items
room[i].doors = malloc(sizeof(pos) * 10); // 10 doors
}
You allocated groundItems and doors only for the first room, But you have to allocate it for all 6 rooms. Each room needs its own list of groundItems and doors. Adapt your code like this:
roomLayout * room = malloc(sizeof(roomLayout) * 6 ); // 6 rooms
for ( int i = 0; i < 6; i ++ )
{
room[i].groundItems = malloc(sizeof(item) * 10); // 10 ground items
room[i].doors = malloc(sizeof(pos) * 10); // 10 doors
}
Note room->groundItems is similar to room[0].groundItems. So in your case room[0].groundItems[0].objectID worked, but room[1].groundItems[0].objectID failed, because room[1].groundItems was never initialized.
You are missing allocation to room[1], room[2] and so on.
by
room->groundItems = malloc... you implcitly say
room[0].groundItems = malloc ....
Maybe the allocation of six places of roomLayout confused you. It allocated the pointers to groundItems and doors arrays, but not the space they required themselves.
I'm trying to create an array of pointers in C. Each value of the array should be a pointer to a struct (let's call it struct Type*).
Should i do
struct Type* myVariable= malloc(sizeof(struct Type*)*MY_SIZE);
or
struct Type** myVariable= malloc(sizeof(struct Type*)*MY_SIZE);
The second one looks like what i should do when i want to create a two dimensional array, which are an array of pointer, and those pointers are used to create arrays of the wanted type.
EDIT : But in my case the second dimension size would be only one
The first one looks like a regular array with int* as the contained values type.
How can i pass the good solution to a function (by pointer, not by value because the array may be fairly large) and use it in the fonction ?
The second one the right solution. However, you'll need to allocate memory for the objects too. Also, make sure to check the value returned by malloc.
// Allocate memory for the array of pointers.
struct Type** myVariable = malloc(sizeof(struct Type*)*MY_SIZE);
if ( myVariable == NULL )
{
// Deal with error
exit(1);
}
for (int i = 0; i < MY_SIZE; ++i )
{
// Allocate memory for the array of objects.
myVariable[i] = malloc(sizeof(struct Type)*THE_SIZE_IN_THE_OTHER_DIMENSION);
if ( myVariable[i] == NULL )
{
// Free everything that was allocated so far
for (int j = 0; j < i-1; ++j )
{
free(myVariable[j]);
}
free(myVariable);
// Exit the program.
exit(1);
}
}
However, if THE_SIZE_IN_THE_OTHER_DIMENSION is going to be 1, you are better off using your first approach.
struct Type* myVariable = malloc(sizeof(struct Type)*MY_SIZE);
// ^^^^^^^^^^^ Drop the *
if ( myVariable == NULL )
{
// Deal with error
exit(1);
}
Neither!
Use an idiom that reduces work and errors
pointer = malloc(sizeof *pointer * Number_of_elements);
Or in OP's case "to create an array of pointers in C"
#define ARRAY_N 100
struct Type **myVariable = malloc(sizeof *myVariable * N);
if (myVariable == NULL) Handle_OutOfMemmory();
Now set those pointers to some value
#define M 50
size_t i;
for (i=0; i<N; i++) {
myVariable[i] = malloc(sizeof *(myVariable[i]) * M);
if (myVariable[i] == NULL) Handle_OutOfMemmory();
for (size_t m = 0; m<M; m++) {
// Initialize the fields of
myVariable[i][m].start = 0;
myVariable[i][m].value = 0.0;
myVariable[i][m].set = NULL;
}
}