what is the default value after malloc in c? [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following code:
int *p;
p = (int*)malloc(sizeof(int) * 10);
but what is the default value of this int array with 10 elements? are all of them 0?

The real content is indeterminate.
C11, ยง 7.22.3.4 The malloc function
The malloc function allocates space for an object whose size is specified by size and
whose value is indeterminate.
However, the operating system often initializes this memory space to 0 (see this answer). You can use rather calloc to be sure that every bits are setted to 0.

are all of them 0?
No, malloc does not initialize the memory allocated, use calloc in order to initialize all values to 0
int *p;
p = calloc(10, sizeof(int));

It's undefined. Using calloc( ) instead then the allocated buffer is defined to be filled with binary 0

The "default" value of an element created with malloc is the value stored at this point in your memory (often 0 but you can have other values, if an another program wrote in this place).
You can use calloc, or after the malloc, you can memset your var.
And don't forget to verify the value of your pointer, check if it's not NULL.

You need to use calloc() for this purpose. malloc() allocats only memory, so this memory contains garbage values by default.

Related

Does calloc on an already allocated block of memory invoke a duplicate allocation? [closed]

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 4 years ago.
Improve this question
I've already allocated a block of memory for 1024 char elements. *p is pointing on its first address.Now I want to set all values to zero. If I'd usep = (char *) calloc(1024, sizeof(char))Would it reuse the already allocated block of memory or would I allocate 1024 new bytes in addition somewhere else and change the address *p is pointing to? And if that's what's happening, what happens with the old block? Because I literally couldn't call free() on it anymore.
Does calloc on an already allocated block of memory invoke a duplicate allocation?
No it does not. You lose the old memory location.
Would it reuse the already allocated block of memory or would I allocate 1024 new bytes?
Again, no it would not. You get a new memory location.
What happens with the old block?
There will be a memory leak because there is no way to get access to it again.
I literally couldn't call free() on it anymore.
You are right here.
The man page says:
DESCRIPTION
operation is performed.
The calloc() function allocates memory for an array of nmemb elements of size bytes each
and returns a pointer to the allocated memory. The memory is set to zero. If nmemb or
size is 0, then calloc() returns either NULL, or a unique pointer value that can later be
successfully passed to free().
RETURN VALUE
The malloc() and calloc() functions return a pointer to the allocated memory, which is
suitably aligned for any built-in type. On error, these functions return NULL. NULL may
also be returned by a successful call to malloc() with a size of zero, or by a successful
call to calloc() with nmemb or size equal to zero.

can anyone explain the meaning of this peace of code from c [closed]

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 5 years ago.
Improve this question
the code is from C the code is written below:-
int main(){
char* time = (char *)malloc(10240 * sizeof(char));
scanf("%s",time);
return 0;
}
Error:
Because there is no prototype in scope for malloc the compiler interprets the result (of type void*) as type int.
Then it converts that value to type char* and assigns the resulting (meaningless) value to time.
Then, still using the meaningless value, it attempts to store there a value read and interpreted as an integer from standard input.
It then exits without doing anything more. Particularly it doesn't use the value read or release the memory (probably) allocated earlier.
it has first reserved memory dynamically from the heap by using malloc function, then it is scanning a string. pay attention to the format fo %s conversion specifier: the argument related to it is a pointer.(not for example time[0].)
don't forget that the memory should be free after the work is done
syntax: free (time);
char* time = (char *)malloc(10240 * sizeof(char));
Here, you use malloc() to allocate 10240 * sizeof(char) blocks of memory for you. sizeof(char) is equal to 4 bytes, so you are allocated 10240 * 4 = 40960 blocks of memory as int by default, as you have not specified any prototype in scope (seen as void*) for malloc()
You then cast the memory space to a char * and have *time point the first block in that memory allocated to you.
scanf("%s",time);
In this line, you try to store a value read as integer input from scanf() in time. Your format specifier is wrong here.
The program then terminates, without freeing the memory you allotted in it, and without doing anything with the inout you took with scanf().

Stack Memory "leak" in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Consider the below code segment:
void f() {
int arr[10];
arr = malloc(sizeof(int) * 100);
for (int i = 0 ; i < 100 ; i++) {
printf("%d ", arr[i]);
}
puts("");
free(arr);
}
Will the original arr[10] stack memory be freed when the function f returns? (Or is this a stack memory leak?)
You cannot do
arr = malloc(sizeof(int) * 100);
like you've done (tried to do) in your code snippet. arr is array type and assignment is not permitted on that.
Subsequently, maybe worthy to mention, calling free() on a non-dynamically allocated pointer invokes undefined behavior.
FWIW, "leak" comes into picture for the memory allocated by the dynamic memory allocation (generally, heap). For variables allocated in "stack" area (by compiler) need not to managed (for de-allocation or free -ing) from your program.
int arr[10]; this array is already allocated from stack;
to allocate array dynamically declare it as a pointer
int *arr;
dynamic allocation will not be automatically freed, use free or similar to free it;
When you declare the array arr, you are already assigning space (on your function stack) to it. It doesn't make sense to make the malloc'd memory (which gets allocated on the heap) assigned to that array.You are invoking compile error doing that. You should have used a pointer instead of declaring that array. A pointer will grab and store that malloc'd space for you.

How can I implement an array whose size is not known at compile time in C89? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Sorry, I'm a bit of a newbie to C and was wondering how you could create an array whose size is not known at compile time before the C99 standard was introduced.
It's very easy. For example, if you want to create a variable length 1D int array, do the following. First, declare a pointer to type int:
int *pInt;
Next, allocate memory for it. You should know how many elements you will need (NUM_INTS):
pInt = malloc(NUM_INTS * sizeof(*pInt));
Don't forget to free your dynamically allocated array to prevent memory leaks:
free(pInt);
Use malloc function from stdlib.h to create a dynamic array object.
the ordinary way would be to allocate the data on the heap
#include <stdlib.h>
void myfun(unsigned int n) {
mytype_t*array = (mytype_t*)malloc(sizeof(mytype_t) * n);
// ... do something with the array
free(array);
}
you could also allocate on the stack (so you don't need to free manually):
#include <alloca.h>
void myfun(unsigned int n) {
mytype_t*array = (mytype_t*)alloca(sizeof(mytype_t) * n);
// ... do something with the array
}
You can do this by dynamic memory allocation. Use malloc function.
malloc or calloc
YourType* ptr = malloc(sizeof(YourType)*NumberOfItemsYouNeed)

assignment of string using strcpy and equal operator [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is difference between the following code?
1.
char *p;
strcpy(p,"String");
2.
char *p;
p = "String";
The pointer points to the same string, but is there any difference?
In order for the first operation to work, p must point to a writable block of memory at least 7 bytes in size. The second operation does not require it.
After the first operation the string remains writable: you can do this on the first string, but not the second:
*p= 's'; // Make the value all lowercase
The second pointer assignment points p to a memory of a string literal; writing to that memory is undefined behavior.
(1) is a memory scribble and possibly a runtime error.
You cannot copy into memory you don't own (haven't allocated in some way).
In the first point you say you want to copy the string to the memblock p is pointing to
(so you have to make sure, there is enough space where the string can be copied to)
In the second case you just make p pointing to the read only address of "String".
p -> [S][t][r][i][n][g][0]
But you should get compiler warnings as far you don't declare p as p const *

Resources