Can still print a string after I freed it?

I am learning and testing memory allocation in C and I want to test what happens if free() is called. I expected there could be a segmentation fault or pointer is NULL after I run the program below. However, I can still successfully print the strin...
more »

2017-07-19 20:07 (2) Answers

Heap exploit fails: Memory corruption

Currently learning heap exploitation but there is something weird I don't understand: This is the next chunk to be allocated by a call to malloc(0x80): 0x602090: 0x0000000000000000 0x0000000000000091 0x6020a0: 0x00007ffff7dd1b78 0x00000000006...
more »

2017-07-03 19:07 (0) Answers

Confusion about malloc and calloc function in C

Declaration of malloc function: void *malloc(size_t size); Here, malloc returns void pointer. So, A void function returns nothing, then Why we assign malloc(function call) value to pointer? For example: int *ptr; ptr = malloc(10 * sizeof (*pt...
more »

2017-07-03 12:07 (3) Answers

Output struct data from allocated memory

I have a question, I have created a struct: struct ipOut { unsigned int ipVer; unsigned int headerDWORDS; unsigned int headerBytes; unsigned int typeOfService; unsigned int ipLength; unsigned int ident; unsigned int ttl; ...
more »

2017-06-21 17:06 (0) Answers

Malloc vs New for Primitives

I understand the benefits of using new against malloc in C++. But for specific cases such as primitive data types (non array) - int, float etc, is it faster to use malloc than new? Although, it is always advisable to use 'new' even for primitives, i...
more »

2017-06-16 13:06 (4) Answers

heap overflow affecting other programs

I was trying to create the condition for malloc to return a NULL pointer. In the below program, though I can see malloc returning NULL, once the program is forcebly terminated, I see that all other programs are becoming slow and finally I had to rebo...
more »

2017-06-07 06:06 (4) Answers

Why can't I use pointer addition instead of malloc

I was wondering why I have to use malloc to dynamically create memories, while I already have pointer addition to freely manipulate the memories. So I created this code, but this code corrupts, saying that 'stack around the variable 'a' was corrupted...
more »

2017-06-03 04:06 (4) Answers

What does allocating memory of size char * do?

char *str; str = malloc(sizeof(char) * 5); This code allocates 5 consecutive slots of memory to the variable str, which is of type char *. char *str; str = malloc(sizeof(char *) * 5); This is supposed to allocate 5 times the memory of an a...
more »

2017-05-25 15:05 (5) Answers

2d dynamic array in C fault

I have to write a program with dynamic 2d array but I've got a problem. Everything works fine until I try to print the array with more than 4 rows. Up to 4 rows all looks good, but with 5th row the length of last row expands to 7631472 which cause a ...
more »

2017-04-09 16:04 (2) Answers

Memcpy takes the same time as memset

I want to measure memory bandwidth using memcpy. I modified the code from this answer:why vectorizing the loop does not have performance improvement which used memset to measure the bandwidth. The problem is that memcpy is only slighly slower than me...
more »

2017-04-02 14:04 (2) Answers

C array of pointers to double

I'm getting a memory problem in c++ code, for sure I'm wrong but I don't get the problem. I've declare an array of three pointer to double and allocate memory. double *myDoubles[3]; for(int i=0;i<3;i++) { myDoubles[i]= (double *) malloc(1000*...
more »

2017-04-02 13:04 (3) Answers

Interview about malloc in function

I faced a question in an interview of a company, and I cannot figure out the reason about the answer. void newArray(int* local, int size) { local = (int*) malloc( size * sizeof(int) ); } int main() { int* ptr; newArray(ptr, 10); } ...
more »

2017-03-22 17:03 (5) Answers

Double-asterisks and `malloc` in C

I've been studying pointers for a while and I can't seem to wrap my head around it fully. There seems to be a gap that isn't explained when you jump from tutorials explaining pointers to actual functions and code that assume you know about them. The ...
more »

2017-03-22 04:03 (5) Answers

Interview questions about string and char

I faced two questions from a company. The two questions confused me. Can anyone help explain the reason of the answers? Write the result. void Test(void){ char *str = (char *) malloc(100); strcpy(str, “hello”); free(str); if(str != NUL...
more »

2017-03-20 10:03 (3) Answers

Why Can I Trust Memory Allocation?

I'm currently taking an introductory CS course that uses C. My textbook seems to imply that a pointer variable still holds the address for memory previously allocated to it, even after free() was called on it -- assume I previously used malloc(). Doe...
more »

2017-03-19 08:03 (5) Answers

Is this an acceptable use of malloc and free? (C)

I am currently learning C. My lecturer gave this as a bad example of using malloc and free, but to me it seems okay. this is the code: int *p1,**p2; p1 = malloc(sizeof(int)); *p1 = 7; p2 = malloc(sizeof(int*)); *p2 = p1; free(p1); free(*p2); My le...
more »

2017-01-28 22:01 (1) Answers

Dynamically allocating array of structs C

I'm trying to make an array of structs in c, but I can't make it work. When I try to run it, the program crashes. typedef struct{ char name[20]; char manufacturer[20]; unsigned int price; } product; unsigned int stringToNr(char *numbers...
more »

2017-01-05 22:01 (1) Answers

How much memory does int x[10] allocate?

Is there any difference in the memory usage of these two code lines? int *a = malloc( 10 * sizeof(int) ); int b[10]; The first line should allocate memory for 10 ints and 1 pointer. But I'm not sure about the second. Will that also allocate memory...
more »

2017-01-04 16:01 (2) Answers