how much space use an array variable [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In c ,How much memory consume an array,That is only one single array
ie,int a[0]; or char a[0];
I want to know it when the program writes on a paper ,not at program running on compiler
Here I cant use sizeof function , my compiler is avrgcc ,
In the part of my program some where I require an array of int a[13];only
or Instead of int a[13]; an int a[3]; along with an integer type additionally ie, int i.
specifically I require
if i require 13 integer array or 4 integer array along with an integer variables.
which is less memory used

The size of an array is the sum total of the size of each element in the array.
For example,
if the array size is 5
the array element (type) size is 4 bytes
The whole array would consume (size * sizeof individual element), i.e., in this case 5 * 4 == 20 bytes.
This is irrespective of the usage, i.e., how many elements you actually plan to use.
FWIW, a 0-size/ 0-length array is non-standard. It's a gcc extension for a particular purpose (before the addition of flexible array member as a standard) that supports a 0-sized array, but you better not reply on it.

An int (integer) type variable has a size of 2 bytes, a char has 1 byte. In an array the size of array multiplied by size of variable according to its type will give you the size of the array.
And you can use the sizeof also.

Related

How to give a size using realloc? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
I want to give a random size to pointer and I want to add some elements to the pointer. I want to give place to the pointer enough size. For example, if I have 10 elements in pointer, I want to give place 40 byte to pointer. But when I looked to my code, initially I gave so much size for the element.
I want to see as an output:
67
11
64
7
67
11
64
7
23
81
88
35
12
5
7
The size of memory=40
#include <stdio.h>
#include <stdlib.h>
int main() {
int *k;
int i=0,j=0,t=0;
int array[20]={67,11,64,7};
k=malloc(5*sizeof(int));
k=array;
for(i=0;i<4;i++){
printf("%d\n",k[i]);
}
k=realloc(k,50*sizeof(int));
k[4]=23;
k[5]=81;
k[6]=88;
k[7]=35;
k[8]=12;
k[9]=5;
k[10]=7;
for(j=0;k[j]!='\0';j++){
printf("%d\n",k[j]);
}
k=realloc(k,(j+1)*sizeof(int));
for(t=0;k[t]!='\0';t++){
printf("%d\n",k[t]);
}
printf("the size of the memory=%d \n",j*4);
printf("the size of the memory=%d \n",t*4);
return 0;
}
You can't resize an automatic variable like array which is what you are trying to do since you do k=array; (and leak the memory previously allocated).
You can copy the data from array into the allocated memory pointed out by k instead:
int array[20] = {67, 11, 64, 7};
int *k = malloc(sizeof array);
memcpy(k, array, sizeof array);
The loop condition you use is a tad confusing though:
for (j = 0; k[j] != '\0'; j++)
I suggest comparing with 0 instead.
The "the size of the memory" that you print out at the end is however not accurate. The size of the allocated memory at the end is (j+1)*sizeof(int), that is, 12 * sizeof(int), since k[11] is the first element being 0.
A word of caution is also in place: Your program currently has defined behavior because the array does contain 0:s in the positions [11, 19]. Just be aware that the extra memory allocated by realloc contains indeterminate values, so had your original array been only 11 elements big, you had read those indeterminate values when you compare with \0 (or preferably 0) and you would never know what the result would be. It could continue searching for \0 out of bounds.
Note: Don't forget to free(k); when you're done with it.

Convert multiple bytes array into single long variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Im getting 10 bytes of data in an char array like which contains hex value
Data1[0] = 0x00,Data1[1] = 0x00,Data1[0] = 0x9 Data1[2]=0x01and so on...
Now I want to get this different array bytes into single long variable . Like
Long_var = 091...
How can do it any method can be accepted.
Sorry, i forgot to mention, i want to do this in 8051 code
There are generally two ways to do type punning in C, both involving arrays.
The first is to use a plain array of 32-bit integers, and then copy the bytes into that array:
char data[12];
// data is initialized...
uint32_t integers[3];
memcpy(integers, data, 12);
printf("First value is 0x%08x\n", integers[0]);
The other way is to use unions:
union type_punning_union
{
uint32_t integers[3];
char data[12];
};
union type_punning_union u;
// Initialize u.data...
printf("First value is 0x%08x\n", u.integers[0]);
Big important note 1: Your byte array have a size mismatch for matching all data evenly to 32-bit integers.
Big important note 2: The code shown above doesn't care about endianness, meaning the results printed might not be exactly what you expect.

I Want to Clarify whether Following answer Correct or Not? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
int sum(int A[], int n)
{
int sum = 0, i;
for(i = 0; i < n; i++)
sum = sum + A[i];
return sum;
}
'n*2' bytes of memory to store array variable 'a[]'
2 bytes of memory for integer parameter 'n'
4 bytes of memory for local integer variables 'sum' and 'i' (2 bytes
each)
2 bytes of memory for return value.
Total space Needed = 2n + 8
let assume c compiler takes 2 bytes for store integer value
I want to know whether above calculated total space answer is correct or not?
I have doubt in return statement because i first allocate memory to sum variable and sum is return last so i need to no whether return sum statement needed memory allocation.
ASSUMING -> the posed question was about how much stack space this function will require:
Well, the A[] parameter is not an array; it's a pointer to the first element of the array, so that's sizeof(int*), not n * sizeof(int).
Whether you need space for the return value depends on whether your environment returns integers in CPU registers, or pushes them onto the stack. If on the stack, then yes, you need to allow for that space.
ASSUMING -> the posed question was about BigO space complexity
Caveat: How you calculate space complexity for an algorithm depends LARGELY on the programming language and / or hardware environment. Contrast this with time complexity, which is mostly language / hardware agnostic. Be advised that most examples for space complexity on the web make some pretty bald assumptions.
For example, the A[] parameter in this example is almost always assumed to consume the space required for every element in the array. In C, and languages that pass object references, this is simply not true. In some of the web examples, this is mentioned in a footnote; in others, not at all.
So, for this post, the answer for BigO space complexity is -> It Depends. It could either be O(1) or O(n), depending on who's asking and where it's expected to run.

Why does array indexing begin at 0 rather than at 1 in languages such as Java and C? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Why aren't arrays indexed starting at 1? Why do array indexes begin at 0?
Basically it is because of the way the array is actually built in memory.
Your variable points to the start of the array and the "Index" is actually an offset which is syntactic sugar for address arithmetic.
So basically you have the first memory location and you are moving n elements over to get the element you are looking for.
More information here:
http://see.stanford.edu/materials/icsppcs107/07-Arrays-The-Full-Story.pdf
Note this isn't the case with all languages but because of the connection between the index notation and the address addition in most implementations the zero indexing approach is extremely common.
Actually an array var is pointer which points to the base address. suppose int arr[5] is an array allocated in a memory {a,b,c,d,e}
Element--> MemoryLocation
a --> 1000
b --> 1002
c --> 1004
d --> 1006
e --> 1008
here arr acts as pointer and holds address 1000
arr[0] means arr+2*0=1000 (2 indicates size of datatype in this case it is int and int size is 2B)
arr[1] means arr+2*1=1000+2=1002
arr[2] means arr+2*2=1000+2=1004
arr[3] means arr+2*3=1000+2=1006
arr[4] means arr+2*4=1000+2=1008
because of this array indexes starts with zero.

How do you create an array of a certain size using malloc() in C? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Part of the program I am writing requires an array to be created using malloc instead of the regular way of doing it. I have to have the user enter a number, assign that number the name MAX, and create an array using malloc() with numbers 2 through the number entered. How would I go about coding this?
You create an "array" with malloc() by specifying the size (in bytes) of the array and assigning the return value to a pointer of the appropriate type. If you're intending for this to be an array of objects that are larger than one byte, you can multiply the number of objects by the size of the object, which can be obtained with the sizeof operator.
For example, you can create an "array" of fifty int objects like so:
int *ar = malloc(50 * sizeof (int) );
You can do that by:
T *dynamic_memory;
....... //Get the desired array size from user input and store in 'array_max_size'
dynamic_memory=malloc((sizeof(T) * array_max_size);
T : data type of array
You can then use dynamic_memory for your purpose.

Resources