#include <stdio.h>
#include <stdlib.h>
struct Point {
double x;
};
void test(struct Point **a, int len)
{
int i;
printf("a = %p\n", a);
for (i = 0; i < len; ++i)
printf("%f\n", a[i]->x);
}
int main()
{
int i;
int len = 4;
struct Point *P;
P = malloc(len*sizeof(struct Point));
for (i = 0; i < len; ++i) {
P[i].x = i;
printf("%f\n", P[i].x);
}
printf("&P = %p\n", &P);
test(&P, len);
return 0;
}
I am trying to pass an array of structs to a function (I want to pass a pointer to the array, not make a copy). When I try to use the array inside the function, I get an access violation. What is the correct way to do this? What am I doing wrong? a == &P, so it should work, right?
Why's you want a struct Point **? You can rewrite the same as
void test(struct Point *a, int len)
{
//some stuff
printf("%f\n", a[i].x);
}
and call it like
test(P, len);
This way, IMHO, the requirement
I want to pass a pointer to the array
is also met#.
(#) NOTE: To be strict, here we pass the pointer to the first element of the array, however, the behaviour compares equal. Thanks to Mr. #alk for the comment.
Passing &p to function test means that you are passing a pointer to the first element of an array of one element of type struct Point *. Therefore, only a[0] is valid (and hence a[0]->x) and all other a[i] are out of bound access. This will invoke undefined behavior.
Change a[i]->x to a[0][i].x or (*a)[i].x in test function.
Using pointer to pointer in this case is not worthy. This would be of worth using if passed pointer is to be modified in the function and that modification is expected to seen in the caller.
The array should be passed using the parameter struct Point *a. When you increment a the pointer will move by sizeof(struct Point).
void test(const struct Point *a, int len)
{
...
}
Other answers offer you better alternatives.But I'll put this here to help anyone (myself) understand why it is wrong.
I want to pass a pointer to the array,
Taking your requirement literally you do it like this:
void test(size_t len, struct Point (*a)[len])
{
size_t i;
printf("a = %p\n", (void *) a);
for (i = 0; i < len; ++i)
printf("%f\n", (*a)[i].x);
}
And call it like this:
size_t len = 4;
struct Point * p = malloc(len * sizeof *p);
for (i = 0; i < len; ++i) {
p[i].x = i;
printf("%f\n", p[i].x);
}
printf("p = %p\n", (void *) p);
printf("&p = %p\n", (void *) &p);
test(len, &p);
You could also implement the same functionality (looping over the array's elements) by going the way proposed by Sourav Ghosh's answer. You then would pass a pointer to the array's 1st element then, but a pointer to the array itself.
Related
I have a function to update an unsigned char* and cannot find where my bug is. I'm not sure if I need to allocate memory, or if I am pointing to the wrong memory space somewhere. I tried to follow a similar structure as posted here, but have not had success with an unsigned char.
My code so far:
#include <stdio.h>
void changeArray(unsigned char **arr)
{
unsigned char ptr[3] = {100, 101, 102};
*arr = ptr;
printf("%d\n", **(arr+0)); // This prints out the correct value of 100
}
int main(int argc, const char* argv[])
{
int i = 0;
unsigned char *blah;
unsigned char ptr2[3] = {103, 104, 105};
blah = ptr2;
printf("Blah is: \n");
for (i = 0; i < 3; i++) {
printf("%d,",*(blah+i)); //This prints out 103,104,105
}
changeArray(&blah);
printf("Blah is now: \n");
for (i = 0; i < 3; i++) {
printf("%d,", *(blah +i)); //This prints out 0,0,0
}
return 0;
}
Any help in determining how to properly access the values set in the changeArray() function would be greatly appreciated.
With this *arr = ptr; you are storing a pointer to a variable with automatic storage duration. The behaviour undefined.
You can dynamically allocate and return a pointer that way:
void changeArray(unsigned char **arr)
{
unsigned char ptr[3] = {100, 101, 102};
unsigned char *p = malloc(sizeof ptr);
memcpy(p, ptr, sizeof ptr);
*arr = p;
printf("%d\n", **(arr+0)); // This prints out the correct value of 100
}
You should also do error checking if malloc failed and remember to free the allocated memory after use in main.
The problem here is, ptr is local to changeArray() function. So once the function finishes execution, there is no existance of ptr. Hence, once you assign ptr to *arr
*arr = ptr;
and changeArray() execution gets over, accessing blah in main() now will invoke undefined behaviour.
FWIW, you don't need to pass the address of blah, you don't need a pointer-to-pointer at all. blah is already a pointer, which you can pass to changeArray() to alter the contents of the memory area it points to. You can do something like
void changeArray(unsigned char *arr)
{
for (int i = 0; i < 3; i ++)
arr[i] = 100+i;
}
and call it like
changeArray(blah);
I have a struct that contains an int pointer
struct mystruct {
int *myarray;
};
I want to make a function that mallocates for mystruct and also initializes myarray. But, when I try to access an element of myarray, I get a seg. fault
void myfunction(struct mystruct *s, int len) {
s = malloc(sizeof(mystruct));
s->myarray = malloc(sizeof(int) * len);
int i;
for (i=0; i<len; i++) {
s->myarray[i] = 1;
}
}
main() {
struct mystruct *m;
myfunction(m, 10);
printf("%d", m->myarray[2]); ////produces a segfault
}
However, mallocating m in main seemed to solve my problem.
Revised Code:
void myfunction(struct mystruct *s, int len) {
int i;
s->myarray = malloc(sizeof(int) * len);
for (i=0; i<len; i++) {
s->myarray[i] = 1;
}
}
main() {
struct mystruct *m = malloc(sizeof(mystruct)); //this was in myfunction
myfunction(m,10);
printf("%d", m->myarray[2]); ///Prints out 1 like I wanted
}
Why did the 2nd attempt work and why did the first attempt not work?
The problem is that the first version assigns the result of malloc to a parameter, which effectively a local variable; the assigned value vanishes when the function returns
So, an alternative is to pass to the function a pointer to the location where you want to store the result of malloc. This is named pps in the code below. At the beginning of the function we do the malloc and assign to a local variable s. Then we do things with s. Then, just before the function exits, we assign the local variable s to the location pointed to by the parameter pps. *pps = s;
void myfunction(struct mystruct **pps, int len) { // note double "**"
struct mystruct *s = malloc(sizeof(mystruct);
s->myarray = malloc(sizeof(int) * len);
int i;
for (i=0; i<len; i++) {
s->myarray[i] = 1;
}
*pps = s; // now pass the alloc'ed struct back to main through parameter pps
}
Now, back in main we pass &m to the function. This passes a pointer to m to the function. When the function returns, the local variable m holds the value returned by malloc and passed through the parameter pps.
main() {
struct mystruct *m;
myfunction(&m, 10); // PASS THE ADDRESS OF m, not m itself
printf("%d", m->myarray[2]); // this will work now
}
I am working on a Uni assignment here, and I've run into a problem. I am attempting to store a string input at a point inside a struct using a for-loop. Later on I intend to use the pointer to the place where the data was stored to fetch the string. Now the problem is, as I move on inside my for-loop, the address of the point changes as well. This code:
printf("B: %p\n", txt->point);
for(i = 0; i < input_sz; i++)
{
txt->point[i] = input[i];
}
printf("A: %p\n", txt->point);
gives the output:
B: 0x7fc111803200
A: 0x7fc111803265
where B is before-value and A is after-copying value.
Any help debugging this would be very appreciated!
EDIT: Here's some more code:
The struct:
struct text_storage {
char* start;
char* point;
char* end;
} typedef text_t;
Initialization function:
text_t* text_init(void *memory, size_t size)
{
text_t* to_return;
if(size < sizeof(text_t))
{
return NULL;
}
to_return = (text_t*) memory;
to_return->start = to_return;
to_return->end = to_return->start + size;
to_return->point = to_return->start;
printf("Start: %p, point: %p, end: %p, end-start: %d\n", to_return->start, to_return->point, to_return->end, (to_return->end - to_return->start));
return to_return;
}
The text-store method in which the error occurs:
int text_store_entry(text_t *txt, const char *input, size_t input_sz)
{
int to_return;
char* begin = txt->point;
int i;
if(input_sz > (txt->end - txt->point))
{
return -1;
}
printf("Start: %p, point: %p, end: %p, end-start: %d\n", txt->start, txt->point, txt->end, (txt->end - txt->start));
printf("B: %p\n", txt->point);
for(i = 0; i < input_sz; i++)
{
txt->point[i] = input[i];
}
printf("A: %p\n", txt->point);
}
Main-function (testing purposes only):
int main(int argc, char* argv[])
{
void* memory = malloc(10000);
char* a = "hei pa deg din trekkbasun";
text_t* txt;
int memoverwritten;
txt = text_init(memory, 10000);
memoverwritten = text_store_entry(txt, a, (size_t)26);
printf("got through\n");
return 0;
}
The problem most probably is due to the initialization of structures of type struct text_storage. Such structures contain three pointers to text. Each pointer should be initialized, possibly with a malloc. Your text_init function does not do that properly.
In fact, the place where the start pointer is stored overlaps with the first bytes of the memory that you want to use.
I'm guessing that you need a structure like this:
typedef struct text_storage {
char* start;
char* point;
char* end;
char* data;
} text_t;
initialized with a function like this:
text_t text_init(void *memory, size_t size)
{
text_t to_return;
to_return.data = (char *) memory;
to_return.start = to_return.data;
to_return.end = to_return.start + size;
to_return.point = to_return.start;
return to_return;
}
Print txt->point in the loop and see the point at which it changes. I'm guessing it changes when assigning to txt->point[0]. I'm not fully familiar with printf, so I'm not sure what it's printing out for you, but the name of an array references the first location. If printf is printing out a pointer, txt->point[i] is always a char pointer, and printf may be dereferencing txt->point, which will get it the first entry, and then showing the address there, which you do assign when you change the point to input[i].
This is in reference to this question: Why is a pointer to pointer needed to allocate memory in this function?
The answer to the question explained why this didn't work:
void three(int * p)
{
p = (int *) malloc(sizeof(int));
*p = 3;
}
void main()
{
int *p = 0;
three(p);
printf("%d", *p);
}
... but this works:
void three(int ** p)
{
*p = (int *) malloc(sizeof(int));
**p = 3;
}
void main()
{
int *p = 0;
three(&p);
printf("%d", *p);
}
This also works, by returning a pointer from the function. Why is that?
int* three(int * p)
{
p = (int *) malloc(sizeof(int));
*p = 3;
return p;
}
void main()
{
int *p = 0;
p = three(p);
printf("%d", *p);
}
int* three(int * p)
{
p = (int *) malloc(sizeof(int));
*p=3;
return p;
}
Because here you're returning a copy of the pointer p and this pointer now points to valid memory, which contains the value 3.
You originally passed in a copy of your p as an argument, so you're not changing the one you passed in, but a copy. Then you return that copy, and assign it.
From the comment, which is a very valid point, this will also work just as well:
int* three()
{
//no need to pass anything in. Just return it.
int * p = (int *) malloc(sizeof(int));
*p=3;
return p;
}
They're completely different (and if you truly understand why the first works, you'd see there's no connection).
By returning, you're not attempting to modify the already existing pointer from inside the function. You're just returning a new pointer, and assigning its value outside.
Look at it as a question of scope.
In main() you have a pointer p.
int *p = 0;
p in main is set to NULL. When you make a call to the three function passing it p:
three(p);
You are passing a pointer to NULL. What happens to it is outside the scope of main(). main() does not know, nor does it care what happens. main() only cares about its copy of p, which at this point is still set to NULL.
Unless I reassign p within the scope of main() (including handing off the address of p), p is still just a pointer pointing to NULL.
If I give you this code:
void main()
{
int *p = 0;
funcX(p);
printf("%d",*p);
}
You can tell me definitively what is going to happen (Segmentation fault) without ever knowing what funcX() does because we're passing a copy of the pointer to this function, but a copy doesn't affect the original.
But if I give you this code:
void main()
{
int *p = 0;
funcX(&p);
printf("%d",*p);
}
You can't tell me what will happen unless you know what funcX() is doing.
That make sense?
I want to convert the type of the pointer 'p'. Begining ,the type of the pointer p is void .After allocating four bytes of memory for it, I cast pointer type into 'int',However ,this doesn't work . maybe the sentence p=(int *)p doesn't work.
Please tell me why and solve the problem.thanks.
The coding:
#include<stdio.h>
#include <stdlib.h>
int main(void)
{
void *p;
p=malloc(sizeof(int));
if(p == NULL)
{
perror("fail to malloc");
exit(1);
}
p=(int *)p;
*p=100;
printf("the value is : %d\n",*p);
return 0;
}
You'll have an easier time directly casting the void pointer returned by malloc to an int*
#include<stdio.h>
#include <stdlib.h>
int main(void)
{
int *p;
p = malloc(sizeof(int));
if(p == NULL)
{
perror("fail to malloc");
exit(1);
}
*p=100;
printf("the value is : %d\n",*p);
return 0;
}
You can't change a variable's type after you have declared it. For what you are asking, you need to declare a separate int* variable and assign your p variable to it with the type-cast:
int *i = (int *)p;
*i = 100;
printf("the value is : %d\n", *i);
Or, simply declare p as a int* to begin with and then type-cast the pointer returned by malloc(), like #GWW showed.
You cant change a variables type, you can do a type cast but that is only temporary.
what you want would be something like *(int*)p = 100;
printf("the value is : %d\n", *(int*)p);
You can't. But you can create a new pointer to int and point to that position, like:
void *p;
p= malloc...
int *pi;
pi= p;
*pi= 25;