Questions tagged as 'malloc'

2
answers

What is the difference between the syntax ptr = (int *) malloc (sizeof (int)) and ptr = malloc (sizeof (int))?

I have a doubt about dynamic allocation in C. At the moment of allocating the memory I already saw these two types of syntax: ptr = (int*) malloc (sizeof(int)); ptr = malloc (sizeof(int)); But I do not know the difference in use between th...
asked by 20.03.2018 / 21:11
2
answers

Do (Cast) the return of Malloc (), Calloc () and Realloc () - C

According to the discussion Do I cast the result of malloc? , in C it is not recommended or correct to do the cast of the return of Malloc (). Does this also apply to the Calloc () and Realloc () functions? Taking advantage, is the correct ta...
asked by 31.12.2018 / 14:57
2
answers

Behavior of malloc (1) in C

If I use char *char_commandout = (char *) malloc(1); , the allocated byte will store "\ 0" at position 0 of the vector or it will allocate a space for the value that I want to store (at position 0) and one for "\ 0" (in position 1)?     
asked by 19.06.2018 / 04:31
1
answer

How do I pass the address of the first char of a string to a function write to it?

The problem is this: I made a function that takes the output of a certain command from the OS and stores it in a string. The idea would now be to declare a string of just one char in my main function using malloc , call my function by pass...
asked by 21.06.2018 / 01:24
1
answer

How to send a dynamically created array as a parameter to a function?

In the exec I am developing, I try to pass a dynamically created array created with the malloc function, but doing so compiles the pointer type as incompatible. #include <stdio.h> #include <stdlib.h> #include <math.h>...
asked by 28.02.2018 / 21:25
1
answer

How to fill an entire vector with malloc (sizeof (int)) with some value

I have a problem, I have no idea how to fill this vector with some value, for example, I want to fill it all with -1 (start it all with -1). The problem here is that I'm not sure exactly what the size of my vector is. typedef struct argume...
asked by 04.10.2018 / 03:26
2
answers

Use extra space beyond what is reserved by "malloc"

When we request a quantity of memory to the system and we use much more than the requested what happens? I did this test and the code here compiled normally. At first it worked as it should, I wanted to know what this implies, I believe that...
asked by 09.04.2018 / 23:03
2
answers

The semantic difference of "Malloc" and Calloc "

I was in the programming class with C and I was wondering about the difference between Malloc and Calloc , but not in what each one does, but in the meaning of "M" and "C". I know that Malloc comes from memory al...
asked by 01.11.2017 / 12:22
1
answer

Error reading values for a dynamically allocated array

Good afternoon, I'm doing some code to study more about memory allocation in C with the malloc function, and I was developing a code to allocate an array and then read values and save it, but it is giving error while executing and closing the...
asked by 11.12.2017 / 20:33
1
answer

How to allocate a dynamic, user-supplied stack?

I want to allocate a dynamic stack with the size provided by the user, then treat it as a "vector" would be more or less what I did in the function ALOCA ? #include <stdio.h> #include <stdlib.h> #include <ctype.h> #incl...
asked by 10.12.2017 / 15:23