Most voted "malloc" questions
Use this tag whenever the problem or question is related to memory allocation made through the function `malloc`
Learn more…52 questions
Sort by count of
-
10
votes2
answers158
viewsWhat happens to memory when "realloc()" reallocates a memory block to a value less than the original?
Suppose I allot a dynamic matrix of 10 ints and assign some values values to it, then I use the function realloc() to reallocate the matrix to 3 ints, what happens to the other 7 ints, they will be…
-
9
votes2
answers1582
viewsWhat is the difference in the syntax ptr = (int*) malloc (sizeof(int)) and ptr = malloc (sizeof(int))?
I have a question about dynamic allocation in C. At the time of allocating memory I have seen these two types of syntax: ptr = (int*) malloc (sizeof(int)); ptr = malloc (sizeof(int)); But I don’t…
-
7
votes2
answers7494
viewsHandling of malloc() and realloc()
I still don’t feel completely confident about using the malloc() or realloc(), these two ways are equivalent? 1) int main() { int x=0; char *s2,*s1; s1=NULL; s2=malloc(200); while(x++<3) {…
-
6
votes1
answer79
viewsWhat is the difference between the function "malloc()" and a created variable?
I was reading some codes in C and I realized that to generate some specific object is used the function malloc(). It is not easier to create this object in a variable than to use the malloc()? Here…
-
5
votes3
answers1148
viewsDynamic allocation in C - allocating without knowing the total amount of elements
I have a question about the dynamic allocation in c, as to the use of the function malloc(). If we take, for example, a case of registration of a full name, as we have no way of knowing the amount…
-
4
votes2
answers267
viewsBehaviour of malloc(1) in C
If I use char *char_commandout = (char *) malloc(1);, the allocated byte will store the " 0" at position 0 of the vector or will allocate a space for the value I want to store (at position 0) and…
-
4
votes2
answers196
viewsDo ( Casting ) of the return of Malloc(), Calloc() and Realloc() - C
According to the discussion Do I cast the result of malloc?, in C is not recommended or correct to cast Malloc() return. This also applies to Calloc() and Realloc functions()? Taking advantage, the…
-
4
votes2
answers70
viewsPointer pointing to another pointer, how to use the free() correctly?
When I have a pointer pointing to another pointer like: int *ponteiro1 = malloc(sizeof(int)); int *ponteiro2; *ponteiro1 = 5; ponteiro2 = ponteiro1; free(ponteiro2); And I use the command…
-
4
votes1
answer124
viewsHow abstract are pointers in C?
I have a vision, which from time to time seems wrong, that C pointers are simply and literally memory addresses. In this case it starts from a misconception that memory was a linear thing and…
-
3
votes1
answer98
viewsHow does malloc() organize memory?
When I allocate memory with malloc(), address equals an array? Or are spread across the PC memory? I want to create a list of structs, to do this, I have to have several structs in case, I can do…
-
2
votes1
answer319
viewsHow to fill an entire array 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 don’t know exactly how big my…
-
2
votes1
answer105
viewsHow to send a dynamically created matrix as a parameter to a function?
In the execution I am developing I try to pass a matrix created dynamically created with the function malloc, but in doing so the compiler points to type incompatible with pointer. #include…
-
2
votes1
answer263
viewsHow do I pass the first char address of a string to a function to write to it?
The problem is this: I made a function that takes the output of a given OS command and stores it in a string. The idea now would be to declare a single char string in my main function using malloc,…
-
2
votes3
answers80
viewsDo I need to re-allocate a structure in case I clean up just a part of it?
I have a struct who owns nome and horário for appointments. typedef Struct{ char nome[20]; char data[20]; }Dados; I make the statement: Dados *dados_cliente[quantidade]; I make the allocation…
-
2
votes2
answers231
viewssizeof does not work to determine the size of malloc
Well, I was doing a data structure job when I came across the need to dynamically allocate a vector, however, even allocating the space needed for the structure, the value returned by sizeof is…
-
2
votes0
answers53
viewsWhat makes this C program unusable when compiled in different versions of GCC or Clang?
This C language program is compiled in all versions of GCC without any error message, but specifically in GCC 6.3 and 8.3, the executable produces totally inconsistent results. In GCC 8.1 and 10.2…
-
1
votes1
answer179
viewsMalloc does not work in C code
While trying to compile the code I get the following error messages: #include <stdio.h> #include <stdlib.h> #define OK 0 void preencherVetor(int* vetor[], int tamanho) { int indice; for…
-
1
votes1
answer71
viewsSomething simpler than that to allocate a dynamically typed text?
// FUNCAO // char *nome(char text[20]) { char *n; int n2; int n3 = 0; printf("%s\n",text); while((n2 = getchar()) != '\n' && n2 != EOF) { if(n3 < 1) { n = (char*) malloc(sizeof(char));//…
-
1
votes2
answers451
viewsSemantic difference of "Malloc" and Calloc"
I was in programming class with C and I got the doubt 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…
-
1
votes0
answers58
viewsvector/dynamic queue segmentation failure
Hello, the following code is in segmentation failure. Basically I can’t scan this entrance: ISCOD4 2 3 10 20 1 10.1.1.151 10.1.2.151 10240 1 2 10.1.1.153 10.1.4.152 12288 2 3 10.1.1.156 10.1.4.153…
-
1
votes1
answer329
viewsHow to allocate a dynamic stack with user-provided size?
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…
-
1
votes2
answers267
viewsWhen to actually use malloc() and/or calloc()?
My doubt is due to the fact that I am learning from banal examples (in my view), as in: int *ptr; ptr = malloc(sizeof(int)); It seems to me useless to allocate a space of an integer to a pointer of…
-
1
votes1
answer80
viewsError reading values for a dynamically allocated matrix
Good afternoon, I’m doing some coding to study more about C memory allocation with the malloc function, and I was developing a code to allocate an array and then read values and save it, but it’s…
-
1
votes2
answers80
viewsUse extra space in addition to that reserved by "malloc"
When we request an amount of memory from the system and use much more than requested, what happens? I did this test and the code here compiled normally. At first it worked as it should, I wanted to…
-
1
votes1
answer275
viewsdynamic reallocation - struct array
I need to do an exercise where the code allocates the memory as needed, but I need to reallocate an array of struct, only I ended up locking in this part. My Struct is: typedef struct{ char…
-
1
votes1
answer68
viewsProblem with adjacency matrix
I’m having a hard time printing the data from an array, I believe I’m saving the data the wrong way, so you notice the data was not saved because when I go to print no data comes back that I added,…
-
0
votes1
answer165
viewsPrint memory available in C
How to know the amount of memory available before doing the malloc()? I would like to print the memory value that is still available in order to be allocated, the code has to run on Windows and…
-
0
votes1
answer62
viewsMalloc in a string, based on the size of a FILE
int main(void) { FILE *p = fopen("matriz.txt","r+"); char *arquivo; arquivo=(char*)malloc(sizeof(p+1)*sizeof(char)); while (fgets(arquivo,sizeof(arquivo),p)) { printf(" %s",arquivo ); } }//END the…
-
0
votes0
answers177
viewsC - Stack Smashing Detected - How to correctly initialize a graph by adjacency list and insert edges?
I am receiving, sporadically and without having made any changes to the code or input, an error called Stack Smashing Detected. The only things I’m doing is initializing a graph and inserting an…
-
0
votes0
answers109
viewsHow do the functions malloc( ) and calloc( ) work in C?
I know that both functions allocate a memory space in bytes and returns the address of that memory, where calloc frees this space while in malloc the release must be made by the function free. But…
-
0
votes1
answer48
viewsI have dynamically stored memory of a vector in C, does not return the allocated size, why?
This code is an example of another code with the same problem. I dynamically stored the memory, but when I’m printing the vector size with the len, strangely prints 1 and not 10 as expected. The…
-
0
votes1
answer61
viewsinsert names neatly into a list, I’m not aware of this
//function of inserting there; there only does it with the 3 first names, the others it does not order Aluno *cad(Aluno *aluno) { Aluno *aux; while(1) { aux = aluno; if(aux->prox == NULL) { Aluno…
-
0
votes1
answer181
viewsMalloc Function Error: sysmalloc: Assertion failed in C
I’m implementing a Red Black Tree in C, and when I allocate memory to the second node, it gives the error: sysmalloc: Assertion [...] failed. Aborted (core dumped) I have already researched and…
-
0
votes1
answer84
viewsReturn malloc to pointer does not stay between functions
I have these two examples here: example 1: void copiaStr(char *a,char *b){ b = (char *) malloc(strlen(a)); for(int i = 0; i < strlen(a) ;i++){ b[i] = a[i]; } } int main(){ char *a = "alou"; char…
-
0
votes3
answers335
viewsError when De-locating Matrix - double free or corruption C
Hello, I’m running a program to rotate a matrix, which after allocating executes the rotation function, and then displaces the data. The problem is when I try to dislocate, it’s returning me double…
-
0
votes2
answers249
viewsProblems with CRUD in C (DELETE method)
In the delete function, the user will inform an AR that he wants to delete, when the AR exists in the memory he deletes, that part of the code works... The problem is when it does not find in memory…
-
0
votes1
answer154
viewsDo you need to allocate memory when you have struct inside another struct?
I want to save and recover data from a struct, but this struct has other struct inside it, I don’t know if I did it correctly, I don’t know if I should use typedef or just struct. I don’t know if I…
-
0
votes1
answer48
viewsin C. I passed a vector to a function and changed it inside. Why didn’t you change my vector in the main function? because I passed a pointer
#include <stdlib.h> void matriz_transposta( int l, int c, int *matriz); int main (void){ int linha = 2, coluna = 2; int *mat = (int*)…
-
0
votes0
answers39
viewsProblem increasing array size within struct dynamically
Problem I am doing a test program that receives names of students and their grades, with the number of students being able to increase indefinitely, for this I used a struct with two arrays inside…
-
0
votes1
answer36
viewsHow to return a pointer to a dynamically allocated String, declared within a function?
//Função de inversão de String. #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 30 char* invertStr(char *source) { int size = strlen(source); char *inverted…
-
0
votes0
answers50
viewsExecution error in c
I have a problem with the following code: #include <stdio.h> #include <stdlib.h> #include <string.h> char **tabela_hash; int valorTotal=0; void inicializarTabela(int *pm) { for…
-
0
votes2
answers77
viewsMemory allocation with malloc()
Doubt 1: The pont should reserve a memory block for 1 integer which was requested on malloc(), but he reserves 32 bytes or a memory block for 8 integers. 'Cause he does it? Doubt 2: If I make one…
-
0
votes0
answers27
viewsPointer matrix with no type and variable size in C
I am creating a game for my college work and need to read a txt file to build a level (phase) of the game. The txt example can be accessed here: https://pastebin.com/f778Ucxa. The dash represents an…
-
0
votes1
answer37
viewsError assigning malloc to an integer in C language
Hello I’m following a tutorial on Youtube about dynamic memory allocation in c language, but I’m getting an error regarding the attribution of a malloc() to an integer type variable. The strange…
-
0
votes1
answer56
viewsHow do I pick up two values obtained in a function through pointers?
I am building a program, whose enunciation is to make the user provide 20 numbers and these are analyzed as pairs or odd. In this case, the intention is to create a vector A that takes 20 integers…
-
-1
votes1
answer40
viewsAlgorithm problem in C
Good afternoon, I’m trying to make a program to estimate the contagion from some parameters, but the program is not working properly, it does not create file, and has not printed any of the prints I…
-
-1
votes1
answer380
views"Local variable not initialized p used
I’m developing a c++ program to learn malloc, but is giving local variable error in main function: // testeMalloc.cpp : define o ponto de entrada para o aplicativo do console. // #include "stdafx.h"…
-
-1
votes2
answers63
viewsFunction of free() in abstract types
I expected the following code to record the students' data, and then immediately erase it from memory: testTurma. c #include "aluno.h" #include <stdlib.h> int main(){ aluno *turma; turma =…
-
-1
votes1
answer28
viewsMemory allocation problem, with large values
Well I’m in trouble on one issue Enunciation Given an integer vector, your task is to find the k-th occurrence (left to right) of an integer v in the vector. To make the problem more difficult (and…
-
-1
votes1
answer29
viewsProblems with assigning values from one vector to another, using malloc
I’m trying to make a program that reads a certain amount of values and inserts it into a vector that uses memory allocation. After that I created two more vectors (in the same way as the previous…