Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
In the Samsung R&D placement test a question was there:
What's the maximum size allocated by calloc?
a)16k b)32k c)64k d)128k
I dont know the right answer. Can anyone tell me the right answer for it.
This is a nonsense question, as it is highly system-dependent. None of the answers is right; there are system where you can calloc() mega- and gigabytes of memory.
Theoratically the maximum is the value one could request. For
void *calloc(size_t nmemb, size_t size);
that is:
nmemb * size
SIZE_MAX * SIZE_MAX
With SIZE_MAX being the limit of size_t.
However as size_t is the result of the sizeof operator the theoratical maximum is limited to SIZE_MAX.
It's related to your compiler and your machine architecture,For example:
int = 2 byte in some machine
int = 4 bye in some machine
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Essentially, this is my problem.
FILE *fp = "/my/textfile/location";
fseek(fp, 0L, SEEK_END);
int size = ftell(fp);
char *output_string[size];
printf("%d", size); // gives me 25
printf("%ld", sizeof(output_string)); // gives me 200.
Why does sizeof(output_string) give me 200 when the int returned from ftell is 20?
char *output_string[size];
This is array of pointer, if you are running in 64-bits machine,
one pointer is 64-bits, which is 8 bytes.
if size is 25, then :
sizeof(output_string) = 25 * sizeof(char *) = 25 * 8 = 200
Silly me.
I accidentally wrote * in my array declaration, which made output_string an array of pointers, not chars
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi i new learn here please explain below question i m learning on my own from many days i do have some dout with malloc function .please help i know this site it not for beginners but i couldn't get alternative way to find the solution
1) p=malloc(0) // what will it return ?when i calculate size using sideof operator it throw 4 byte?
2) int *p=malloc(4) // when i scan string it throw 0 why sir?
3) *p=2 // while p is store in heap
scanf("%d",*p)//why *p is not possible to scanf here *p why p only?
4) int *p=(int*)malloc(20*sizeof(int))
for(i=0;i<5;i++)
p[i]=i+1;
free(p);
//now after free i am still get same previos value.. why not garbage coz malloc default value is garbage?
5) int main()
{
int *p;
p=(int*)malloc(4);
printf("%d\n",*p); // even here i am getting 0 why nt garbage?
}
Thank you sir
"Freeing" means "making available for allocation again". There is no automatic deleting / overwriting of memory contents because it would negatively impact performance. If you want the area to be set to a value, you have to do it yourself before you call free(). That would be bad practice in release code, though (for anything else but data security reasons).
The same is true when allocating the memory: It does not get set to any specific value, but contains what it happened to contain previously. If you want it to be initialized to zero, use calloc(). If you want to set it to a specific other value, use memset() after you allocated it. Again, consider that this has performance implications, and is usually not necessary.
As for your last question, "%d" is for signed integers. For unsigned, use "%u".
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
an interview question on glassdoor is as follows. With my knowledge, it is hard to deduce anything out of it. What could be an appropriate question?
A macro that computes a size_t number. Putting in a loop, it casts -1
to a size_t number, making the loop impossible to start.
as suggested by Michael Aaron Safyan, following might be the case
operates in the reverse:
for (size_t i = 0; i > ((size_t) -1); i--) {}
For explanation see the answer
The issue is that size_t is unsigned, so casting -1 to it will produce the maximum-valued size_t. One would fix this case by using a signed type (such as int or ssize_t).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
mid= n / 2;//(where n is 10)
int left[sizeof(mid)];
but compiler allocate space for 4[0,1,2,3] elements, it suppose to allocate for 5[0,1,2,3,4] elements.
what could be the issue?
sizeof mean: the size in memory. int is 4 bytes in memory. so sizeof(int)=4.
You probably want to write int left[mid];
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
In C I wish to create a data type (like int or float) that has only 1byte. How can I possible do this? I've tried with malloc() but didn't work that way I tried.
Could you please give me a hand here?
Example:
sizeof(int) = 4 bytes
sizeof(char) = 1 byte
sizeof(float) = 4 bytes
sizeof(myDataType) = 1 byte
There is such data type. char is always guaranteed to be one byte long. If you want another name for that type, just use typedef and create a new type based on char.
More detailed explanation can be found in this question: Are there machines, where sizeof(char) != 1, or at least CHAR_BIT > 8?