sizeof dynamic allocation

This is a discussion on sizeof dynamic allocation within the C++ Programming forums, part of the General Programming Boards category; When i allocate dynamically a char* of 256 array for example, i thought that its size is 256 bytes. But ...

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

    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;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,853
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

    Thanks!
    Last edited by Ducky; 08-10-2009 at 08:22 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,029
    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
    821
    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?
    Compiler MSVC++ 2013 with Code::Blocks.

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, 09:21 AM
  2. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 10:56 AM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 06:04 PM
  4. Replies: 14
    Last Post: 06-28-2006, 02: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

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