Memory allocation of a float array in C [duplicate] - c

This question already has answers here:
Determine size of dynamically allocated memory in C
(15 answers)
Closed 4 years ago.
I would like to create a float array of 9 elements. Thus I made use of malloc primitve function to do so :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(void){
float* y;
printf("sizeof(float)=%ld\n",sizeof(float));
y = malloc (10*sizeof(float));
printf("sizeof(y)=%ld\n", sizeof(y));
float *x;
x = (float *) malloc(9*sizeof(float));
printf("sizeof(x)=%ld\n", sizeof(x));
}
The output is the following:
sizeof(float)=4
sizeof(y)=8
sizeof(x)=8
Therefore I am wondering why both sizeof(y) and sizeof(x) return 8. Shouldń't they return 10*sizeof(float)=10*4=40 and 9*sizeof(float)=9*4=36 respectively? Could someone please clarify to me why am I getting 8 instead for both arrays?

x and y are not declared as arrays. They are pointers to memory locations. Depending on the system, the pointers could be 8 bytes. That's what you're seeing.
If you were to declare an array int z[10], and did sizeof(z), you'd get 10*sizeof(int).

Related

pointer array doesn't gets printed [duplicate]

This question already has answers here:
Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
(5 answers)
Closed 1 year ago.
#include <stdio.h>
#include <stdlib.h>
void main()
{
int i;
int *as[2];
for(i=0;i<2;i++)
{
*as[i]=i;
printf("%d\n",*as[i]);
}
}
This is my code. I am expecting to print values 1
2
But when I run the code the console prints nothing. What have I done wrong?
The * character in C language is not a simple decoration. It declares a pointer that has to point to something. Here you declared an array of two pointers, but never initialized the pointer themselves. And dereferencing an uninitialized pointer is explicitely Undefined Behaviour (the hell for C programmers). If you want to process integers, get rid of the pointers:
int as[2];
for(i=0;i<2;i++)
{
as[i]=i;
printf("%d\n",as[i]);
}
Or if you really want to process pointers, ensure that they are initialized to point to a valid object:
int *as[2];
int data[2]
for(i=0;i<2;i++)
{
as[i] = &(data[i]); // Ok, as[i] now points to a valid integer
*as[i]=i;
printf("%d\n",*as[i]);
}
BTW, the idiomatic way to get the address of an element of an array would be:
as[i] = data + i;

Two different functions with same body with different parameters return values in same memory location [duplicate]

This question already has answers here:
returning a local variable from function in C [duplicate]
(4 answers)
How to access a local variable from a different function using pointers?
(10 answers)
Closed 5 years ago.
I cannot understand what happens here clearly.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int *f(int x){
int p;
p=x;
return &p;
}
int *g(int x){
int y;
y=x;
return &y;
}
int main(){
int *x,*y;
x=f(1000);
y=g(250);
*x = *x + 250;
printf("%d\n",*y);
return 0;
}
output:- 500
How come line "*x = *x + 250" change "*y" value? why the output is not 250?
int *f(int x){
int p;
p=x;
return &p;
}
In this function (and in g), you are returning the address of a local variable. When the caller uses this address, it is invalid, because it is referring to a variable which is destroyed (its like using a pointer to freed dynamic memory). This results in undefined behavior.

C - crash when running this application [duplicate]

This question already has answers here:
Modifying String Literal [duplicate]
(4 answers)
Closed 8 years ago.
Where is the problem? When running, the application crashes...
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void test(char* x) {
(*x)++;
}
int main() {
char* x = "xD";
test(x);
puts(x);
getch();
return 0;
}
You are Trying to modify a string literal that is stored in a read-only memory adress, because with char* x = "xD"; you declare a pointer to that kind of data. use this char x [] = "xD"; instead, that is NOT a pointer, is an array that you are allowed to modify because it is stored in the stack. or if you want to use a pointer you need to allocate memory for it.
it crashes in the line (*x)++; because x Points to a read only Memory due to the Definition char* x = "xD";.
Change it to char x[] = "xD";. so x is an Array and it´s values can be changed

Array size in functions [duplicate]

This question already has answers here:
Why isn't the size of an array parameter the same as within main?
(13 answers)
Closed 8 years ago.
I'm working with some arrays in plain C99 and I need to know their size. Does anyone knows why this is happening:
int function(char* m){
return sizeof(m) / sizeof(m[0]);
}
int main(){
char p[100];
int s = sizeof(p) / sizeof(p[0]);
printf("Size main: %d\nSize function: %d\n",s,function(p));
return 0;
}
Output:
Size main: 100
Size function: 8
Thanks!
Arrays decay to pointers at the first chance they get, thus in function all you have is a pointer, which is 8 bytes in your case.
This is happening because sizeof(m) == sizeof(char*), which is 8 on your platform.
When you call function(p), p decays into a pointer and loses its size information.

Why the size of a char variable changes from 4 to 1 when inside and outside a struct? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
Here is the code:
#include <stdio.h>
struct small{
int a;
int b;
char c;
};
void main(){
printf("The size of int is: %d\n",(int)sizeof(int));
printf("The size of char is: %d\n",(int)sizeof(char));
printf("The size of small is: %d\n",(int)sizeof(struct small));
}
Here is the output:
The size of int is: 4
The size of char is: 1
The size of small is: 12
I expect the size of small to be 9,but it turns out to be 12
It's because of alignment requirements. If you were to declare an array of struct smalls:
struct small arr[10];
then each element's a would be immediately after the preceding element. On your system, apparently, ints need to be aligned to four-byte boundaries — either as an absolute requirement, or simply for optimal performance — so struct small includes three bytes of padding to ensure that a subsequent struct small's a is aligned properly.

Resources