Thread: determining size

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    58

    determining size

    ok here is the code.

    Code:
    char *buf=(char *)malloc(4096);
    in linux when i use sizeof(buf) it shows me 4 as the answer (which is actually the size of a char *)

    what should i do for it to show me 4096 here.
    i mean sizeof(?) what is the question mark part?
    even a fish would not have been in danger had it kept its mouth shut.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use an array.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Gee, I just told you here that it doesn't work with pointers
    http://cboard.cprogramming.com/showthread.php?t=53198

    edit:
    LOL - Beaten by Quzah, and we both got the same thread
    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.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    58
    ok i got the point. but how do i make it show 4096 as answer here instead of 4 which it is showing becoz buf is a char *. ok here is the problem which i am working on.
    i am supposed to write a fuction which takes i/p from a buf(like the one described above). now all this buffer contains is values which are packed in 12 bit sequences. so the function i should write should take i/p from this buffer and extend this 12 bit values into proper 16 bits. so while doing this whole thing how will i come to know if i have gone out of bounds?
    what is the condition i have to give for boundary checking of this buffer.
    Code:
    void func(char *buf)
    {
    }
    so now here how do i know the actual memory allocated to buf.is there any way?
    even a fish would not have been in danger had it kept its mouth shut.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    ok i got the point. but how do i make it show 4096 as answer here instead of 4 which it is showing becoz buf is a char *.
    You obviously didn't get the point then did you?

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You have to pass another parameter that tells you the size of the buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizeof: determining the size of char array problems...
    By what3v3r in forum C++ Programming
    Replies: 17
    Last Post: 02-09-2006, 02:40 AM
  2. Determining size of block of memory
    By ExxNuker in forum C Programming
    Replies: 9
    Last Post: 01-12-2006, 11:54 PM
  3. size of text file without traversing
    By vaibhav in forum C++ Programming
    Replies: 10
    Last Post: 12-29-2005, 04:32 PM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM