This question already has answers here:
Using sizeof with a dynamically allocated array
(5 answers)
Closed 9 years ago.
I'm trying to find out the length of an array but i get weird numbers…
#include <stdio.h>
#include <stdlib.h>
const int numOfCoordinates = 100;
typedef struct {
int x;
int y;
int counter;
} Coordinate;
Coordinate *coordinatesMainArray;
Coordinate endPoint;
Coordinate startPoint;
int main(int argc, const char * argv[])
{
endPoint.x = 8;
endPoint.y = 3;
endPoint.counter = 0;
startPoint.x = 1;
startPoint.y = 4;
coordinatesMainArray = malloc(sizeof(Coordinate) * 1);
coordinatesMainArray[0] = endPoint;
int a = sizeof(coordinatesMainArray);
int b = sizeof(coordinatesMainArray[0]);
int coordinatesMainArrayLength = (a / b);
This is my code up until the part i need the length of coordinatesMainArray.
But I get a = 8 and b = 12.
I assumed i would get two similar sizes so it shows i have one element in my array.
What am i doing wrong?
sizeof(coordinatesMainArray) gives you the size of the coordinatesMainArray variable - a Coordinate*
sizeof(coordinatesMainArray[0]) gives you the size of the first element in the coordinatesMainArray array - a Coordinate instance. sizeof(*coordinatesMainArray) would have been equivalent to this.
As an aside, there is no way to use a pointer to determine the size of an array. You'd need to store the array size separately and either pass this as well as the array pointer to other functions or guarantee that the array ends with a known terminator value (e.g. NULL)
In this case sizeof will only return the size of the coordinatesMainArray pointer (regardless of the size of its contents), because it is allocated on runtime.
sizeof(coordinatesMainArray) gives you sizeof(Coordinate *) and sizeof(coordinatesMainArray[0]) gives you sizeof(Coordinate).
First is sizeof pointer and second is sizeof array element.
When you allocate a memory with malloc it doesn't maintain the size of the allocated chunk. At least not accessible to your regular code. If you allocate memory, you have to keep track of the size yourself.
In your example you just got the size of the pointer (a) and the size of a single structure (b).
Related
This question already has answers here:
If free() knows the length of my array, why can't I ask for it in my own code?
(9 answers)
Closed 9 months ago.
I need to read inputs from a csv and put it into a dynamically allocated array
Ay.
int* Ay = malloc(nz * sizeof(int));
The number of inputs from the csv will vary in each case. Later I pass Ay to another function where I need to calculate its length. I have used the following methods but it doesn't give me the right result.
int Aylen = sizeof(Ay) / sizeof(Ay[0]);
or
int Aylen= 1[&Ay] - Ay;
or
int Aylen=*(&Ay+1)-Ay;
Please suggest a way to find the length. Thank you.
There is no portable way to determine the size of a dynamically allocated buffer. You need to keep track of that yourself.
This means you have to pass both Ay and nz to any function that accesses Ay.
It is not possible in C language.
You need to keep trach of the size.
Example:
typedef struct
{
size_t length;
int data[];
}data_t;
data_t *add_element(data_t *arr, int element)
{
size_t newsize = arr ? arr -> length + 1 : 0;
arr = realloc(arrr, newsize * sizeof(arr -> data[0]) + sizeof(*arr));
if(arr)
{
arr -> length = newsize;
arr -> data[newsize - 1] = element;
}
return arr;
}
i have code like this:
int main()
{
double *u;
int len;
u=(double *)malloc(sizeof(double)*10);
len = sizeof(u);
printf("Length Of Array = %d\n", len);
return 0;
}
but the length is 4 Not 10.
how can i extract 10 from pointer u?!
please help me
thank you
That's your job. C does not provide a portable way of knowing, given a pointer, how much memory has been allocated.
sizeof will give you sizeof(double*), that's all. That's 4 on your system.
It is not possible. sizeof is giving the size of the object. In your case the object is u which is a pointer. Your system is 32 bits as pointers are 4 bytes.
if you sizeof(*u)- you will get the size of referenced type. In this case it is the double . It will be 8 bytes long at most systems.
using sizeof to get the size of the length of the string is one of the most frequent questions asked here.
A pointer doesn't include information about the size of the memory area. You have to keep track of the size yourself. For instance, you can make a custom type (for example, a struct) that contains both the pointer and the size of the allocation at the same time.
Here's a simple implementation to get you started:
typedef struct {
double* ptr;
size_t len;
} double_arr_t;
// prototype
double_arr_t alloc_double_arr(size_t len);
int main(void) {
// alloc the new array of 10 `double` elements
double_arr_t arr = alloc_double_arr(10);
printf("Length of 'arr' is %zu\n", arr.len); // Length of 'arr' is 10
// assign a value to the first element
arr.ptr[0] = 3.14;
// get the value of the first element
double first_element = arr.ptr[0];
// free the array when you're done using it
free(arr.ptr);
}
double_arr_t alloc_double_arr(size_t len) {
double_arr_t res;
res.ptr = malloc(len * sizeof(double));
res.len = len;
return res;
}
This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 3 years ago.
I'm trying to get size of an array in c and for some reason I'm getting a wrong answer for this popular method.
int innerprod(int *v, int *u)
{
int n = sizeof(v)/sizeof(v[0]);
printf("%d\n",n); // ouptut here is giving me '2' for a '3' size array
double result = 0.0;
for (int i = 0; i < n; i++)
{
result += v[i]*u[i];
printf("%d\n",v[i]*u[i]);
}
return result;
}
int main()
{
int V[3] = {3,4,-1}; //a 3 size array
int W[3] = {7,-2,3};
int x = innerprod(&V,&W);
printf("%d\n",x);
}
Unfortunately arrays in C do not have boundary checking and have no way of knowing the length. Arrays are literally pointers into memory where your elements are lined up after another. You use the provided index to compute the memory location of the demanded element. Eg. adress of a[2] = a += 2. Accessing an elements outside the range of your array will most probably (this is also the best case) result in a segmentation fault. If not you will corrupt the memory of something which you should not be able to.
If you want this functionality you could use c++ with vectors from the <vector> library. Or you could build a struct which includes an entry for the size of the array.
If I'm trying to create a global array to hold an arbitrary number of integers in this case 2 ints. How is it possible that I can assign more numbers to it if I only allocate enough space for just two integers.
int *globalarray;
int main(int argc, char *argv[]) {
int size = 2;
globalarray = malloc(size * sizeof(globalarray[0]));
// How is it possible to initialize this array pass
// the two location that I allocated.
for (size_t i = 0; i < 10; i++) {
globalarray[i] = i;
}
for (size_t i = 0; i < 10; i++) {
printf("%d ", globalarray[i]);
}
printf("%s\n", "");
int arrayLength = sizeof(*globalarray)/sizeof(globalarray[0]);
printf("Array Length: %d\n", arrayLength);
}
When I run this it gives me
0 1 2 3 4 5 6 7 8 9
Array Length: 1
So I wanted to know if someone could clarify this for me.
(1) Am I creating the global array correctly?
(2) Why is the array length 1? When I feel that it should be 2 since I malloced the pointer for 2.
And background info on why I want to know this is because I want to create a global array (shared array) so that threads can later access the array and change the values.
How is it possible to initialize this array pass the two location that I allocated.
Short answer: This is undefined behaviour and anything can happen, also the appearance that it worked.
Long answer: You can only initialize the memory you've allocated, it
doesn't matter that the variable is a global variable. C doesn't prevent you from
stepping out of bounds, but if you do, then you get undefined behaviour and anything can happen
(it can "work" but it also can crash immediately or it can crash later).
So if you know that you need 10 ints, then allocate memory for 10 int.
globalarray = malloc(10 * sizeof *globalarray);
if(globalarray == NULL)
{
// error handling
}
And if you later need more, let's say 15, then you can use realloc to increase
the memory allocation:
globalarray = malloc(10 * sizeof *globalarray);
if(globalarray == NULL)
{
// error handling
// do not contiue
}
....
// needs more space
int *tmp = realloc(globalarray, 15 * sizeof *globalarray);
if(tmp == NULL)
{
// error handling
// globalarray still points to the previously allocated
// memory
// do not continue
}
globalarray = tmp;
Am I creating the global array correctly?
Yes and no. It is syntactically correct, but semantically it is not, because you are
allocating space for only 2 ints, but it's clear from the next lines that
you need 10 ints.
Why is the array length 1? When I feel that it should be 2 since I malloced the pointer for 2.
That's because
sizeof(*globalarray)/sizeof(globalarray[0]);
only works with arrays, not pointers. Note also that you are using it wrong in
two ways:
The correct formula is sizeof(globalarray) / sizeof(globalarray[0])
This only works for arrays, not pointers (see below)
We sometimes use the term array as a visual representation when we do stuff
like
int *arr = malloc(size * sizeof *arr)
but arr (and globalarray) are not arrays,
they are pointers. sizeof returns the amount in bytes that the
expression/variable needs. In your case *globalarray has type int and
globalarray[0] has also type int. So you are doing sizeof(int)/sizeof(int)
which is obviously 1.
Like I said, this only works for arrays, for example, this is correct
// not that arr here is not an array
int arr[] = { 1, 2, 3, 4 };
size_t len = sizeof arr / sizeof arr[0]; // returns 4
but this is incorrect:
int *ptr = malloc(4 * sizeof *ptr);
size_t len = sizeof ptr / sizeof ptr[0]; // this is wrong
because sizeof ptr does not returns the total amount of allocated
bytes, it returns the amount of bytes that a pointer needs to be stored in memory. When you are dealing with
pointers, you have to have a separate variable that holds the size.
C does not prevent you from writing outside allocated memory. When coding in C it is of the utmost importance that you manage your memory properly.
For your second question, this is how you would want to allocate your buffer:
globalarray = malloc(sizeof(int) * size);
And if you are on an older version of C than c11:
globalarray = (int*) malloc(sizeof(int) * size);
This question already has answers here:
Dynamic array in C — Is my understanding of malloc and realloc correct?
(3 answers)
Closed 5 years ago.
So for my school project, a large CSV file will be entered through stdin and we will have to sort it based on column and print it out as a sorted csv file.
The step I am on right now is figuring out how to keep reallocing a struct of arrays so that it will grow if there is not big enough to hold the data coming in from stdin. We don't know the exact amount of rows that will be inputted in the CSV file. Right now we just used a static amount to test and see if the values are assigned to the structs.
I am still a beginner at C so I do not clearly know how I would iterate through a pointer like I would iterate through an array. Since we are using a static amount of structs in the array, we can just iterate using array[i] like in Java but how would you iterate through something like *array?
I do not know where to start for creating this dynamic array. I tried
struct array* testArray = (array*)malloc(sizeof(testArray));
but I have no idea how to iterate through it like I did with the static array by using array[i].
Any help would be greatly appreciated, sorry for the wall of text...
You can navigate through a malloced space the same way as with an array (using indicies), but it seems that your main issue lies in your use of malloc. Malloc's argument is the size in number of bytes that you want to allocate. So if you want to have an array of structs, you would first need to find out how many bytes one struct contains using sizeof(struct array), and then determine how large of an array you want, let's say N. So that line of code should look more like struct array* testArray = malloc(N * sizeof(struct array));. The return value of malloc will be a void pointer containing the memory address of the first byte of allocated space. Upon assigning this value to testArray, it will be type-casted to the assigned variable type (struct array *). Now you can use pointer arithmetic to access a specific index i with *(testArray + i), or simply testArray[i]. If you find that N was not a sufficient size, you can use realloc to increase the array size to 2N, or whatever size deemed necessary.
struct array* testArray = (array*)malloc(sizeof(testArray));
is a little wrong as you only allocate 1 element of testArray.
It is more like:
struct A
{
int a;
int b;
....
};
struct A* arr = malloc( N * sizeof(struct A) );
^^^
N element of struct A
int j;
for (j=0; j<N; ++j) // Iterate it like any other array
{
arr[j].a = 5;
arr[j].b = 42;
....
}
Use realloc when you need the array to grow.
When reading from a file/stdin it could look like (based on comment from David C. Rankin):
int n=0; // Count of the number of structs read from the file
struct A* arr = malloc( N * sizeof(struct A) );
while (read line from file)
{
arr[n].a = val1;
arr[n].b = val2;
++n; // Increment count
if (n == N) // Check if current array is full, i.e. realloc needed
{
// realloc array to 2 * N; N = N * 2
}
}