malloc

Small program that uses pointers to sum integers

痞子三分冷 提交于 2021-02-08 12:01:19
问题 I need to create a program which calculates the cumulative sum of a dynamically allocated vector and the vector should be filled with random values (not values from stdin) ​​using only pointers . I couldn't think of a version that uses only pointers (i'm kinda new to this matter). This is the code I have so far: #include <stdio.h> #include <malloc.h> int main() { int i, n, sum = 0; int *a; printf("Define size of your array A \n"); scanf("%d", &n); a = (int *)malloc(n * sizeof(int)); printf(

EXC_BAD_ACCESS error for std::string member of a structure

一世执手 提交于 2021-02-08 09:39:13
问题 On accessing a struct member of type std::string, the error Bus Error: 10 popped up. Code is as following. #include <iostream> #include <string> struct KeyValuePair { std::string key; std::string value; }; struct KeyValuePair *temp = (struct KeyValuePair *) malloc(sizeof(struct KeyValuePair)); int main(void) { temp->value = "|"; temp->value += "someval|"; std::cout << temp->value << std::endl; return 0; } Running gdb on the code shows the following at the line temp->value = "|" . Program

stack implementation using malloc in c [BEGINNER]

て烟熏妆下的殇ゞ 提交于 2021-02-07 19:47:18
问题 for learning purpose I'm implementing a stack with it's functions in c. I added some small additional functionality to use malloc the first time and try to understand it properly. I wrote a function which is initially creating my stack struct. The return value of the function is a new struct with an already allocate memory. What is the best way to handle a malloc exception in a function which return value should be a struct? Maybe should I design the function different? I'm aware that the

stack implementation using malloc in c [BEGINNER]

a 夏天 提交于 2021-02-07 19:46:39
问题 for learning purpose I'm implementing a stack with it's functions in c. I added some small additional functionality to use malloc the first time and try to understand it properly. I wrote a function which is initially creating my stack struct. The return value of the function is a new struct with an already allocate memory. What is the best way to handle a malloc exception in a function which return value should be a struct? Maybe should I design the function different? I'm aware that the

free() function without malloc or calloc

送分小仙女□ 提交于 2021-02-07 17:24:20
问题 quick question Can you use the free() function without having to prior call a malloc ?? ei. void someFunc( void ) { char str[6] = {"Hello"}; //some processing here .... free(str); } I get no compiling errors but Does this work or is it correct at all ? Thank you, 回答1: This is not at all correct: You cannot free a static array such as char str[6] . free() should only be called on memory you allocated (or on NULL). 回答2: When you call malloc() or any other allocation function, memory will be

use malloc to set an array of strings then clear it

跟風遠走 提交于 2021-02-07 17:15:43
问题 I want to make an array of string using malloc, and then clear ALL the allocated memory, I believe that I use malloc correct but I can't understand what I'm doing wrong when I try to clear it: #include <stdlib.h> #include <stdio.h> #include <string.h> void print_arr(char* arr[]); void clear_arr(char* arr[], int size); int main() { int number = 10; char **myarr = malloc((number+1)*sizeof(char*)); for (int i = 0; i <= number; i++) { myarr[i]=NULL; } for (int i = 0; i < number; i++) { char str[]

use malloc to set an array of strings then clear it

时光怂恿深爱的人放手 提交于 2021-02-07 17:15:25
问题 I want to make an array of string using malloc, and then clear ALL the allocated memory, I believe that I use malloc correct but I can't understand what I'm doing wrong when I try to clear it: #include <stdlib.h> #include <stdio.h> #include <string.h> void print_arr(char* arr[]); void clear_arr(char* arr[], int size); int main() { int number = 10; char **myarr = malloc((number+1)*sizeof(char*)); for (int i = 0; i <= number; i++) { myarr[i]=NULL; } for (int i = 0; i < number; i++) { char str[]

use malloc to set an array of strings then clear it

*爱你&永不变心* 提交于 2021-02-07 17:13:13
问题 I want to make an array of string using malloc, and then clear ALL the allocated memory, I believe that I use malloc correct but I can't understand what I'm doing wrong when I try to clear it: #include <stdlib.h> #include <stdio.h> #include <string.h> void print_arr(char* arr[]); void clear_arr(char* arr[], int size); int main() { int number = 10; char **myarr = malloc((number+1)*sizeof(char*)); for (int i = 0; i <= number; i++) { myarr[i]=NULL; } for (int i = 0; i < number; i++) { char str[]

use malloc to set an array of strings then clear it

南笙酒味 提交于 2021-02-07 17:12:21
问题 I want to make an array of string using malloc, and then clear ALL the allocated memory, I believe that I use malloc correct but I can't understand what I'm doing wrong when I try to clear it: #include <stdlib.h> #include <stdio.h> #include <string.h> void print_arr(char* arr[]); void clear_arr(char* arr[], int size); int main() { int number = 10; char **myarr = malloc((number+1)*sizeof(char*)); for (int i = 0; i <= number; i++) { myarr[i]=NULL; } for (int i = 0; i < number; i++) { char str[]

Does memory allocated in a function still stay allocated after the function returns?

做~自己de王妃 提交于 2021-02-06 02:07:48
问题 For the code below: (1) "main" calls a function "f1". (2) function "f1" does some number crunching; creates an array of "char" with malloc and then, returns the pointer of the array to the main (without de-allocating -freeing- the array). I have 3 questions related to the case: (1) I assume, although the function "f1" has terminated, the allocated char array still stays allocated until the main program terminates completely. That is, the allocated memory still belongs to the main and no other