memory allocation in bytes

This is a discussion on memory allocation in bytes within the C Programming forums, part of the General Programming Boards category; I want to allocate 'n' bytes of memory. How can i do that. I have to get this n value ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    39

    memory allocation in bytes

    I want to allocate 'n' bytes of memory. How can i do that. I have to get this n value from another variable. If i give n=3, it has to allocate 3 bytes of memory. How can i do this. I tried with the following code. But its not allocating exact bytes.

    Code:
    int n;
    int *m;
    
    printf("enter n value:");
    scanf("%d",&n);
    
    m=(int *)malloc(n);
    Can anyone help me

    Thanks in advance

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    The code is correct. It will allocate at least n bytes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    - 3 bytes maybe not enough to store the int
    - malloc allocates at least requested number of bytes - they are available to the program, it can allocate some additional space for internal usage
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sarathius View Post
    But its not allocating exact bytes.
    Define what you mean by this. I have written a couple of responses to similar questions from you. malloc is not guaranteed to give you an EXACT number of bytes. It will give you something that can store the number of bytes you requested - or a NULL pointer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    39
    I want to get the 'n' value from other variable and i want to allocate those number of bytes. if i give n=3, it has to allocate 3 bytes, if n=4, it has to allocate 4 bytes. I think, now you will be clear

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Quote Originally Posted by sarathius View Post
    I want to get the 'n' value from other variable and i want to allocate those number of bytes. if i give n=3, it has to allocate 3 bytes, if n=4, it has to allocate 4 bytes. I think, now you will be clear
    What are you doing this for? Suppose a little more memory is allocated by malloc() than requested. What would be the problem?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And that code will do that. Are you saying it is not working, if so, by what means are you testing it?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Code:
    int n;
    int *m;
    
    printf("enter n value:");
    scanf("%d",&n);
    
    m=(int *)malloc(n);
    Can anyone help me

    Thanks in advance
    This should be the 100% correct
    Code:
    	int n;
    	int *m;
    
    	printf("enter n value:");
    	scanf("%d",&n);
    
    	m=(int *)malloc(n*sizeof(int));

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Casting malloc in C is bad, so everyone knows.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Why do u think casting malloc is bad? i always use malloc, it works fine for me, i dont know why you are thinking it is bad in c? if even it were bad in c, it would not be part of c.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Malloc itself isn't bad, but casting the pointer it returns is:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by pCBoard.Member View Post
    Why do u think casting malloc is bad? i always use malloc, it works fine for me, i dont know why you are thinking it is bad in c? if even it were bad in c, it would not be part of c.
    You can read FAQ that explains why it is preffered not to cast malloc in C
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >This should be the 100% correct
    You'd be wise to avoid that claim around here. There are some pretty smart people hanging around who will prove you wrong. For example:

    >printf("enter n value:");
    While your implementation is nice enough to flush stdout after this call, not all implementations will do so. In those cases, the only way this prompt will be visible before scanf starts to block for input is if you're lucky enough that the output buffer gets filled up at this point and flushes automatically. Rather than rely on luck or implementation specifics, your code would be more correct to force a flush on stdout:
    Code:
    printf ( "Enter n value: " );
    fflush ( stdout );
    >scanf("%d",&n);
    You can't claim your code to be correct because if scanf fails, you've invoked undefined behavior by accessing the indeterminate value of n. Of course, some people don't really care about undefined behavior, so imagine calling malloc with a completely unpredictable size. Even worse, some compilers will kindly initialize local variables to 0, and that's a can of worms with malloc that you don't want to deal with. Your code would be more correct if it took into account scanf failure:
    Code:
    if ( scanf ( "%d", &n ) == 1 ) {
      /* Use n to allocate memory */
    }
    else {
      /* Handle an input error */
    }
    >m=(int *)malloc(n*sizeof(int));
    This isn't technically an error because you've posted a snippet, but I'll mention that the return value of malloc should always be checked for NULL. Also, it's more correct in C not to cast malloc because the cast can hide a legitimate error of forgetting to include stdlib.h. Since you're removing the cast, you can also remove all mention of m's type (which helps a lot if you decide to change the type):
    Code:
    m = malloc ( n * sizeof *m );
    
    if ( m == NULL ) {
      /* Handle an allocation error */
    }
    My best code is written with the delete key.

  14. #14
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I would like to add that if you are manipulating "bytes" instead of, example, integers, you should use a void* instead of a int*, that would be semantically a bit better.

    Code:
    int n;
    void *m;
    
    printf("enter n value:");
    scanf("%d",&n);
    
    m = malloc(n);
    Of course, if you want to allocate memory for n integers, then of course, you should use an int*, but as far as i understand, it's not what you want (you only talk about bytes in your post).
    I hate real numbers.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if you are manipulating "bytes" instead of, example, integers,
    >you should use a void* instead of a int*, that would be semantically a bit better.
    unsigned char would be more intuitive and easier to work with though. Using int is a problem as you're allocating n bytes rather than n integers. That'll cause a lot of confusion when indexing the resulting memory unless you change the size to (n * sizeof(int)). Good catch, I completely missed that. Yet another reason not to claim 100% correctness. Programming is too hard to be 100% correct.
    My best code is written with the delete key.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Memory allocation
    By p3rry in forum C Programming
    Replies: 25
    Last Post: 12-23-2008, 08:30 AM
  2. Increasing memory allocation in function
    By Ramses800 in forum C Programming
    Replies: 3
    Last Post: 12-16-2008, 05:30 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 07:22 AM
  4. Memory allocation at runtime?
    By electrolove in forum C Programming
    Replies: 6
    Last Post: 02-05-2003, 11:39 AM
  5. Memory Allocation :: C/C++
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 08-15-2002, 11:38 AM

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