[Solved] How to use ‘new’ insted of ‘malloc’ in code [closed]

The equivalent of SP->xs = malloc(size * sizeof(double)); is SP->xs = new double[size]; This does not require any #includes. To free the allocated array, use delete[]: delete[] SP->xs; The square brackets are important: without them, the code will compile but will have undefined behaviour. Since you’re writing in C++, consider using std::vector<double> instead of managing … Read more

[Solved] malloc void return char array somtimes not working (terry davis was right about C++);

One of the problem to cause undefined behavior is In void update_typeArray(char * char_array, int index, char input) you free the memory pointed by the __array_buffer using char_array. update_typeArray(__array_buffer, index, __char_buffer); free(char_array); and you allocate the new memory to char_array char_array = (char*)malloc(sizeof(char)*(index + 1));//+1 cause string but your __array_buffer will be still pointing to … Read more

[Solved] Valgrind detects memory leak, can’t find it C

Your initDictionary doesn’t return the pointer dictionary anywhere. That means when you do Dictionary* dictionary = initDictionary(); the value of dictionary will be indeterminate (seemingly random or garbage), and dereferencing this pointer or passing it to free will result in undefined behavior. You solve this by adding a simple return dictionary; at the end of … Read more

[Solved] C I need to use malloc and a dynamically allocated array however I need to print the user input

issue 1: you are allocating sizeof(double) to store int issue 2: You are not traversing array to print numbers int main(void) { int user_input = 0, elements = 0; printf(“How many int elements will you enter?\n”); scanf(“%d”, &elements); int* dynamic_array = (int *)malloc(sizeof(int)* elements); if(dynamic_array == NULL) { perror(” Out of memory “); return 0; … Read more

[Solved] Malloc , Realloc , Memset : Struct pointers , arrays of char, int

Error was in v_push_back implementation : v->e_array = check_a(memcpy((void*)((char*)v->e_array + (v->no_e * v->e_sz)) ,new_val, v->e_sz)); //pointer has not to be saved check_a(memcpy((void*)((char*)v->e_array + (v->no_e * v->e_sz)) ,new_val, v->e_sz)); Because memcpy returns a pointer , in this case, to the last element of the array so we would have lost access to all previous memory solved … Read more

[Solved] How many bytes will be deallocated with free() after changing the pointer?

As the man page for free will tell you, any argument except a pointer returned from malloc has undefined behaviour: The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is … Read more

[Solved] using realloc in C with malloc [closed]

if (newsize == 0) { free(ptr); return; } if (ptr == NULL) return malloc(size); // otherwise do a true realloc As for What if there is not a contigious size of the size wanted. Then realloc returns NULL and sets errno to indicate the error. 9 solved using realloc in C with malloc [closed]

[Solved] Allocate matrix of integer with c

NUMI does not have rows and columns, it is a pointer-to-pointer-to-int, and happens to be pointing at an allocated memory that has room for dim pointers-to-int, not dim * dim ints. This would be equivalent to treating as having been declared int* NUMI[dim] A call int* NUMI; NUMI= malloc( dim*dim*sizeof(int) ); will allocate a dim … Read more

[Solved] memset after malloc [closed]

My guess without a code example is that you were operating on the buffer or struct you malloc’d with assumptions that its contents would be initialized with certain default values. Malloc doesn’t initialize the memory it hands back, so unless you memset or use some other initialization, the values in that memory could be anything, … Read more