dynamic memory allocation

This is a discussion on dynamic memory allocation within the C Programming forums, part of the General Programming Boards category; how can i hardcode data into the records,thanks... Code: void Handle_Array_Dynamic() { //it dynamically allocates for an array of 10 ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    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. #2
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    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. #3
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    Quote 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?
    Thanks,
    Sahil

  4. #4
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    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. #5
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote 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.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote 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.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.

    Quote 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!
    Quote 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:
    Quote 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.
    Quote 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?
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    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.
    Last edited by cbastard; 10-20-2005 at 02:46 PM.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  10. #10
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote 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
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  11. #11
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    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.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    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?
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 06:04 PM
  4. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 04:59 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21