Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the size of an allocated C buffer? [duplicate]

I have a buffer and want to do a test to see if the buffer has sufficient capacity I.e. find number of elements I can add to the buffer.

char *buffer = (char *)malloc(sizeof(char) * 10);

Doing a

int numElements = sizeof(buffer); 

does not return 10, any ideas on how I can accomplish this?

like image 876
godzilla Avatar asked May 17 '12 16:05

godzilla


People also ask

How do you determine the size of a buffer?

To check the buffer window, multiply the bit rate (bits per second) by the buffer window (in seconds) and divide by 1000 to get the size, in bits, of the buffer for the stream.

What is maximum buffer size in C?

The maximum size of the buffer. The default value is 65536 (0x10000) bytes.

How can you tell the size of memory allocated by malloc using pointers?

ptr = (cast-type*) malloc(byte-size) For Example: ptr = (int*) malloc(100 * sizeof(int)); Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory. And, the pointer ptr holds the address of the first byte in the allocated memory.

What is buffer allocation?

The buffer allocation model of NvSciBuf is summarized as follows: If two or more hardware engines wants to access a common buffer (e.g., one engine is writing data into the buffer and the other engine is reading from the buffer), then: 1. Applications create an attribute list for each accessor.


1 Answers

You cannot make such a test. It is your own responsibility to remember how much memory you allocated. If the buffer is given to you by someone else, demand that they pass the size information as well, and make it their responsibility to pass the correct value or have the program die.

like image 84
Kerrek SB Avatar answered Sep 20 '22 13:09

Kerrek SB