FAQ Database Discussion Community


calloc created array is not acting as expected

c++,pointers,memory-management,calloc
I was bored and wanted to make a program to crash my computer :P. I would have it uselessly and redundantly allocate memory until the crash. The code I created so far is here: #include <cstdlib> #include <cstdio> int main(int argc, const char *argv) { int n = 0; while...

Initialize dynamically allocated structures within dynamically allocated structures

c,struct,calloc
I want to dynamically allocate structures within structures. I think I can do this. However when I want to perform operations with them, I get an access violation error under Visual C++. Perhaps there is something wrong with the initialization. By the way, how can I initialize myDynamicMembersBinArray being dynamically...

Calloc a Two-Dimensional Array

c,arrays,calloc
To make a two dimensional array, I'm currently using the following: int * own; own = (int *)calloc(mem_size, sizeof(int)); for (i=0;i<mem_size;i++){ own[i] = (int *)calloc(3, sizeof(int)); } However, every time I reference own[i][j], I get an error saying that the subscripted value is neither array nor pointer nor vector....

typedef pointer using fgets to input data, but takes last fgets

c,malloc,typedef,fgets,calloc
I'm trying to read postal codes from a file into an Object * array. file includes 123 Anywhere kirkland CA 99223 my .h file looks like typedef struct { char *street; char *city; char *state; int zip; }Address; my filling the array Address * fillArray(int * total, FILE * fin)...

Unable to properly free malloc of another malloc [duplicate]

c,malloc,free,dynamic-memory-allocation,calloc
This question already has an answer here: String assignment in C 4 answers Here's the snippet with issues. int main() { char** RESERV = (char**)malloc(sizeof(char*)*4); printf("%i, %i, %i, %i, %i", **RESERV, *RESERV, RESERV, &**RESERV, sizeof(char*)); int i; for(i = 0; i < 4; i++) { RESERV[i] = (char*)calloc(sizeof(char),16); RESERV[i][15]...

Segmentation fault in 2D array unsigned char using calloc in C

c,arrays,char,unsigned,calloc
I've trying to allocate an unsigned char** using calloc: newmatriz = (unsigned char**) calloc(width, sizeof(unsigned char)); for (j=0 ; j < width; j++) { if (newmatriz[j]=(unsigned char*)calloc(witdh, sizeof(unsigned char)) == NULL){ printf("Memory allocation error. Exit program\n"); exit(1); } } for (i=0;i<width;i++) { for(j=0;j<width;j++) { newmatriz[i][j] = 0; } } But...

unexpected output while using strlen function

pointers,sizeof,calloc
#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char *p; p=calloc(10,sizeof(char)); printf("the address of pointer is %p and size of the string is %d",p,strlen(p)); return 0; } I allocated 10 bytes of memory in heap using calloc and i expect the output of size will be 10 but the output is the...