malloc

Under what circumstances can malloc return NULL?

老子叫甜甜 提交于 2020-01-22 04:49:07
问题 It has never happened to me, and I've programming for years now. Can someone give me an example of a non-trivial program in which malloc will actually not work? I'm not talking about memory exhaustion : I'm looking for the simple case when you are allocating just one memory block in a bound size given by the user, lets say an integer, causes malloc to fail. 回答1: Yes. Just try to malloc more memory than your system can provide (either by exhausting your address space, or virtual memory -

Dynamically Growing an Array in C++

こ雲淡風輕ζ 提交于 2020-01-22 02:20:10
问题 I have an array of pointers of CName objects. I have the following constructor which initializes my array to size one. Then when I add an object I grow the array by 1 and add the new object. It compiles fine, however when I try to print them I just get segmentation fault error. Can you look and see if I'm doing anything wrong? //constructor Names_Book::Names_Book() { grow_factor = 1; size = 0; cNames = (CName**)malloc(grow_factor * sizeof(CName*)); cNames[0] = NULL; } void Names_Book:

This malloc shouldn't work

拈花ヽ惹草 提交于 2020-01-22 01:50:07
问题 Here is my code. int main() { char *s; int i = 0; printf("%lu \n", sizeof(s)); s = malloc(sizeof(char) * 2); printf("%lu \n", sizeof(s)); /*Why is this working?*/ while (i <= 5) { s[i] = 'l'; i++; } printf("%s \n", s); printf("%lu \n", sizeof(char)); printf("%lu \n", sizeof(s[0])); } In my opinion, this should segfault as I'm trying to write more than I allocated. Why is this working? 回答1: In illustration (and complete agreement with) @Ed S's answer , Try this example of your code with the

Allocating 2-D array in C

我与影子孤独终老i 提交于 2020-01-21 14:56:08
问题 I want to allocate a 2-D array in C at runtime. Now this can be achieved in the conventional manner like this: int *matrix[rows] for (row = 0; row < rows; ++row) { matrix[row] = (int *)malloc(ncol*sizeof(int)); } But I found another method, which does the same thing: int (*p)[rows]; p=(int (*)[rows])malloc(rows*cols*sizeof(int)); Can anyone explain how the 2nd declaration works? Specifically, what is meant by (int (*)[rows])malloc ? To the best of my knowledge, malloc is used like (int *

malloc and scanf string

自作多情 提交于 2020-01-21 10:26:36
问题 A simple program below with malloc and scanf with %s to get a string as below, gives me an output I cannot comprehend. While I have 'malloced' only 5 bytes, my input string has exceeded the above size but no segmentation fault. Is scanf overiding malloc allocation? #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char * name; int SZSTRING; printf("Enter size of name :"); scanf("%d", &SZSTRING); name = (char*) malloc ((SZSTRING + 1) * sizeof(char)); printf("Enter name :"

malloc and scanf string

核能气质少年 提交于 2020-01-21 10:26:33
问题 A simple program below with malloc and scanf with %s to get a string as below, gives me an output I cannot comprehend. While I have 'malloced' only 5 bytes, my input string has exceeded the above size but no segmentation fault. Is scanf overiding malloc allocation? #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char * name; int SZSTRING; printf("Enter size of name :"); scanf("%d", &SZSTRING); name = (char*) malloc ((SZSTRING + 1) * sizeof(char)); printf("Enter name :"

what happens when we call Malloc with negative parameter?

夙愿已清 提交于 2020-01-20 03:54:04
问题 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. Prototype: void *malloc(size_t size); I tried passing a negative value as a parameter: malloc(negative) returns NULL . Is it because the [size_t] negative converted to unsigned [some big value] and cannot allot required space or is the function checking parameter and returns NULL ? If its getting converted to big positive, then when calling malloc

Using free() on a copy of the actual pointer is acceptable / correct?

冷暖自知 提交于 2020-01-17 08:20:29
问题 Is this: int *a = malloc (sizeof (int) ); int *b = a; free (b); the same as this: int *a = malloc (sizeof (int) ); free (a); If yes, no need to explain, but if no, please elaborate why not! 回答1: Yes, they are equivalent. Quoting C11 , chapter §7.22.3.3, ( emphasis mine ) The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier

Increase array size at runtime in c++ without vector, pointer [closed]

不问归期 提交于 2020-01-17 08:16:44
问题 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 . I have declared a array of int in c++ with some size. say, int a[6] at runtime if my array size exceeds 6, then i need to increase it. i am not going to use pointer, vector and the size will not be given by the user. 回答1: You can not change the size of your array at run time. An alternative is to

understanding malloc.h difference: __attribute_malloc__

删除回忆录丶 提交于 2020-01-17 01:18:10
问题 can someone help me find where or explain what __attribute_malloc__ means? And also __wur ? I am working with some old code about 120 fortran f77 files but uses a few C files where it only uses malloc() and they do # include <malloc.h> . I have kdiff'd stdlib.h and malloc.h ; not really sure I can find any difference between the two implementations ? Can the linux distribution being used be responsible for the contents of /usr/include/malloc.h ? I am using SLES 11.4 x86-64 having GCC 4.3.4