1. ## dynamic memory allocation

how can i hardcode data into the records,thanks...

Code:
```void Handle_Array_Dynamic()
{	//it dynamically allocates for an array of 10 student records
int i;
int *A;
A = (int*)malloc(10*sizeof(int));
PrintStudentArray(A);
// how can i hardcode data into the records
free(A);
}```

2. One question: Why using malloc() at all when the data is hardcoded anyway?

You can access A like you would with an array:

Code:
```A[0] = 5;
A[1] = 2;
...```
And you should not cast malloc().

3. Originally Posted by TactX
One question: Why using malloc() at all when the data is hardcoded anyway?

You can access A like you would with an array:

Code:
```A[0] = 5;
A[1] = 2;
...```
And you should not cast malloc().

I thought p=malloc(10*sizeof(int)), would allocate memory large enough to hold ten integers in p not create an array of 10 integers in p.

I think p[0] or *(p+i) is illegal.

Am i wrong?

4. An array would do it if your just storing ten integers.

No need to cast malloc to int. You already have sizeof(int). Means your allocating elements the size of an integer.
Code:
`A = (int*)malloc(10*sizeof(int));`
>> I think p[0] or *(p+i) is illegal.

Both are legal on Dynamically allocated arrays.

5. Originally Posted by sahil_m
I thought p=malloc(10*sizeof(int)), would allocate memory large enough to hold ten integers in p not create an array of 10 integers in p.

I think p[0] or *(p+i) is illegal.

Am i wrong?
You declared p as int so the *p will take 32 bits*.
Any arithmatic operation like p+1 will increment the memory p(32 in this case) times.so when you are allocating memory of 10 ints operation till p+(0-9) are legal references to memory.

6. Originally Posted by cbastard
You declared p as int so the *p will take 32 bits*.
Any arithmatic operation like p+1 will increment the memory p(32 in this case) times.so when you are allocating memory of 10 ints operation till p+(0-9) are legal references to memory.
No it won't. There's nothing in the standard that says sizeof(int) * CHAR_BIT will be 32 bits. Also you are acting like incrementaiton moves bits at a time. It doesn't. It moves bytes at a time. You cannot increment a pointer 1 bit at a time. Your assumptions and inaccuracies only confuse the issue.

Quzah.

7. Originally Posted by quzah
No it won't. There's nothing in the standard that says sizeof(int) * CHAR_BIT will be 32 bits. Also you are acting like incrementaiton moves bits at a time. It doesn't. It moves bytes at a time. You cannot increment a pointer 1 bit at a time. Your assumptions and inaccuracies only confuse the issue.

Quzah.
Do you know why i had * after writing 32 bits????Its because size of int depends on the compiler.
I dont know from where you got this
sizeof(int) * CHAR_BIT
I have'nt said anything about incrementing a bits in pointer.
Anything like p+1 will increment memory size of *p times(as 32 bits in this case) from where the bits came in your mind.
There can only be one of the two reasons:-
1.I need to work on my english
2.You need to improve your IQ.

8. Originally Posted by cbastard
Do you know why i had * after writing 32 bits????Its because size of int depends on the compiler.
Ok, if you're denoting something with an asterisk, such as you claim you were doing, you actually need to provide a bullet point to match it. Like so:

It is proven* that you are in fact a complete idiot.

Originally Posted by cbastard
I dont know from where you got this
sizeof(int) * CHAR_BIT
Like I said, you're an idiot. You can pull the number 32 out of your ass, and claim that it's how many bits it will produce when you dereference a pointer, but somehow you're confused when I actually show you the only way possible for you to have come to that conclusion.

Since you're apparently really really slow, let me tell you where you got the idea for the 32.

a) The sizeof operator deals in units that are based off of the size of a char. The size of any object will be referenced in units of char. Thus, it's possible that the line:
Code:
`x = sizeof( int );`
fills the variable x with perhaps 2, or 4, or 8, or whatever. The point is, it is to say "int is the size of 2 char units". Everything has its size based on the size of a char

b) Ah, but how big is a char? Well that's where CHAR_BIT comes in. It tells you how many bits make up each char.

c) Let's pretend that sizeof( int ) yields 4. Let's pretend that CHAR_BIT is 8. What happens when you multiply 4 by 8? Since I have serious doubts as to your mental capacity, I'll do the math for you:
Code:
```  4
x 8
---
32```
Holy mathmatics, Batman! There's our mysterious number 32!
Originally Posted by cbastard
I have'nt said anything about incrementing a bits in pointer.
Yes you did, you moron. You even quoted yourself saying it:
Originally Posted by cbastard
Any arithmatic operation like p+1 will increment the memory p(32 in this case) times.so when you are allocating memory of 10 ints operation till p+(0-9) are legal references to memory.
Just when I think you can't get any dumber, you go and prove me wrong.
Originally Posted by cbastard
There can only be one of the two reasons:-
1.I need to work on my english
2.You need to improve your IQ.
You should never go unarmed into a battle of wits. What a maroon.

Quzah.
* This is proven time and time again by nearly every post you ever make here. Now do you see how to properly make footnotes?

9. Either you are frustated from life or you are a kid ,who just wants to prove his point by moving things here and there like mysterious no. 32.
Only one thing for you ,You need to grow up dude.

10. Originally Posted by cbastard
Either you are frustated from life or you are a kid who needs to grow up,who just wants to prove his point.
He gave you a good explanation to the problem replies ago, and even put it in terms I can understand above (thanks BTW, Quzah).

I know the last time I got involved in someone else's argument it turned out quite badly for me, but you seriously need to consider what you just said.

Quzah = 2nd to Salem

11. yeah i have realized i am really slow.
a) The sizeof operator deals in units that are based off of the size of a char. The size of any object will be referenced in units of char. Thus, it's possible that the line:

x = sizeof( int );fills the variable x with perhaps 2, or 4, or 8, or whatever.
If variable x can take value 2,4, 8 means size of int can be 2,4 8.
The point is, it is to say "int is the size of 2 char units". Everything has its size based on the size of a char
it means sizeof int = 2 * sizeof char to me.
means the sizeof char is also changing.
But i have'nt seen sizeof char 1,2, 4 on any machine or compiler. Always seen allowed values of char as 256(2^8).
would you mind explaining this thing to me.

12. Originally Posted by cbastard
yeah i have realized i am really slow.

If variable x can take value 2,4, 8 means size of int can be 2,4 8.

it means sizeof int = 2 * sizeof char to me.
means the sizeof char is also changing.
But i have'nt seen sizeof char 1,2, 4 on any machine or compiler. Always seen allowed values of char as 256(2^8).
would you mind explaining this thing to me.
Given this scenario:
Code:
`x = sizeof( int );`
If x is 2, it means that the size of one int is equal to the size of 2 char units.
If x is 4, it means that the size of one int is equal to the size of 4 char units.

And so on, for whatever the value of x happens to be.[code]
x = sizeof( char );[/color] The above will always be 1. Always. The size of a char is set by the standard to always evaluate as 1. The size of other data types and objects is not defined by the standard, other than it's size is a reflection of how many units of char equal its size.

Now then, the number of bits in each char may be different. It may be that CHAR_BIT is 20 on your system. The chances are it's 8, but that's not guarinteed. However, it is guarinteed that the size of char will evaluate as 1 no matter what the size of CHAR_BIT is.

So to answer your above, no, size of char isn't changing. It's the size of the integer. Some compilers set integers to be 2 bytes, some use 4 bytes, and so what not. And again, while the number of bits per byte may change, char's size will always evaluate to 1.

Quzah.

13. Thanks for explaining,
but
the number of bits per byte may change, char's size will always evaluate to 1.
I have been through networking,microprocessor but i have never seen bytes<>8 bits.Are you taking byte as size of register?

14. > Are you taking byte as size of register?
As in the unit of memory which has a unique address.
Bit's within a 'byte' do not have a unique address, you have to access the 'byte' which they're contained in.

Some DSP chips for example have only 32 bit 'bytes', so char, short, int, long and pointer all have a size of 1.