Thread: sizeof dynamic allocation

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    sizeof dynamic allocation

    When i allocate dynamically a char* of 256 array for example, i thought that its size is 256 bytes.
    But sizeof() always gives back the size of a char*.
    So how would i go to get the size allocated?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int  main()
    {
        char * buf = new char[256];
        cout << sizeof(buf) <<"\n";
        delete [] buf;
       
        return  0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use another variable.

    Or use a "smart" container like std::string (in particular for chars) or std::vector
    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.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Ok, now i start to understand the differences between C and C++.

    Thanks!
    Last edited by Ducky; 08-10-2009 at 07:22 AM.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    sizeof cannot determine the size of memory pointed to by a pointer. It only works with arrays.
    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.

  5. #5
    The Registered User Aparavoid's Avatar
    Join Date
    May 2009
    Posts
    74
    Quote Originally Posted by Ducky View Post
    Ok, now i start to understand differences between C and C++.

    Thanks!
    Yeah, C++ programmers cant do anything for themselves. Just joking I like C++.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Location
    MD
    Posts
    10
    If you know the size of the buffer then you can use:
    Code:
    sizeof(char) * buffer_size
    However the suggestion of using a smart container is much better.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Quote Originally Posted by ryanrigdon View Post
    If you know the size of the buffer then you can use:
    The point is to "know" the size.

    If you already know it why would you use anything?
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic allocation from 1 instead of zero
    By cfdprogrammer in forum C Programming
    Replies: 27
    Last Post: 04-28-2009, 08:21 AM
  2. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  5. Dynamic allocation (I thought it would crash)
    By Baaaah! in forum C Programming
    Replies: 16
    Last Post: 11-30-2005, 05:10 PM