Okay, so this is my current code: #include <stdlib.h> #include <stdio.h> #include <stdint.h> struct Display { int width; int height; char **array; }; struct Display *display_create(int width, int height) { struct Display *display = (struct Display *) malloc(sizeof(struct Display)); display->width = width; display->height = height; display->array = (char **) malloc(sizeof(char) * height); for (int i = 0; i < height; i += 1) { display->array[i] = (char *) malloc(sizeof(char) * width); } return display; } void display_destroy(struct Display *display) { for (int i = 0; i < display->height; i += 1) { free(display->array[i]); } free(display->array); free(display); } void display_clear(struct Display …

Member Avatar
Member Avatar
+0 forum 3

explain malloc() function and its type casting how execute

Member Avatar
Member Avatar
+0 forum 2

hello i have some a problem with malloc, here is my code: int add(int m,int n) { int **pina, **pinb, **pinc; int c=m*n, i=0, j=0; pina=NULL; pinb=NULL; pinc=NULL; free(pina); free(pinb); free(pinc); pina = (int **) malloc (sizeof(int *) *c); pinb = (int **) malloc (c* sizeof(int *)); pinc = (int **) malloc (c* sizeof(int *)); for (i=0;i<m;i++) { for (j=0;j<n;j++) { pina[i][j]= NULL; pinb[i][j]= NULL; pinc[i][j]= NULL; } } printf("Give elements for A\n"); for (i=0;i<m;i++) { for (j=0;j<n;j++) { scanf("%d",&pina[i][j]); } } printf("Give elements for B\n"); for (i=0;i<m;i++) { for (j=0;j<n;j++) { scanf("%d",&pinb[i][j]); } } printf("\n"); for (i=0;i<m;i++) { for …

Member Avatar
Member Avatar
+0 forum 3

I don't know jack about Visual Studio, and I'd like to keep it that way because I like doing stuff on the command line, I have my preferences. But my teacher uses it to compile and I'm curious what I did wrong because gcc didn't throw anything. So, code (Without header, which just defines constants and function names): #include <allegro.h> #include <stdio.h> #include <stdlib.h> #include "midterm.h" void draw(); BITMAP *sc; SAMPLE *clap, *bounce; int color; void start() { score=0; b.sp = speed; b.rad = radius; p.x = width/2; p.y = height-margin; p.length = paddle_length; num_lives=3; num_blocks = 30; top=0; go=0; …

Member Avatar
Member Avatar
+0 forum 3

This is a basic question, but I can't seem to understand how this works: Suppose I pass a structure pointer to a function, do I need to malloc memory for this passed pointer inside the function? e.g., typedef struct example_data_ { int serial; char *name; } example_data; int main() { int ret; example_data *ptr_ex; // Should I malloc memory for ptr_ex here before assigning values to it??? ptr_ex->serial = 1; ptr_ex->name = 'test'; myfunc(ptr_ex); return 0; } void myfunc(example_data *myptr) { //Can I directly deference the values below??? or should I malloc memory for *myptr also??? printf("serial no: %d\n",myptr->serial); printf("name: …

Member Avatar
Member Avatar
+0 forum 2

I've used malloc countless times, but as far as I know, reading and writing to memory that isn't expressly yours (which is in fact done by the malloc function) causes a segfault. If I wanted to make my own dynamic allocation functions my computer would probably just laugh at me and triple fault all the way to the moon and back. So why doesn't malloc generate segfaults? Does it have special permissions? If it doesn't, would it be possible to make my own malloc functions (which would be really cool)?

Member Avatar
Member Avatar
+0 forum 4

Hi everyone well currently I am working on a simple malloc and free implementation but I am facing some problems . I get a segmentation fault this is my output inserting node 0 0xc87000 inserting node 1 0xc87014 inserting node 2 0xc87028 inserting node 3 0xc8703c inserting node 4 0xc87050 inserting node 5 0xc87064 inserting node 6 0xc87078 inserting node 7 0xc8708c inserting node 8 0xc870a0 inserting node 9 0xc870b4 The list head : 0xc87000 inside the del_search returned value by the del_search 0xc87050 back 0x400c8703c Segmentation fault (core dumped) here is the code typedef struct mem_chunk *ptr ; int …

Member Avatar
Member Avatar
+0 forum 4

Hi. Sooner or later I had to go into the depths of C. Today was the day I tried my first things with C. However, I am having a problem although I do not know what actually causes it. The goal of the pogram is to combine the parameters into one char array, instead of having an array of char arrays. Eventually it needs to call another exe, in which it will measure the time it needs to execute (not implemented yet). The problem is that the program always ends with an error, while the program does its job so …

Member Avatar
Member Avatar
+0 forum 3

My program begins by opening a dictionary file, the first line of which contains the number of words in the dictionary. If I print the words immediately after scanning them into the arrays, they appear fine, but if I later examine a previous entry, I can see it has been partially overwritten, usually after the first four letters. I think it's my mistake lies in my memory allocation, but I'm not sure. [CODE] #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int binsearch(char* word,int size,char* dictionary); int main(void) { FILE* dict; dict = fopen("dictionary.in","r"); int words; fscanf(dict,"%d",&words); int i; char** …

Member Avatar
Member Avatar
+0 forum 7

Im still learning C and I have a few questions about making a array of strings. Im doing an assignment for school, and I'm a little stuck with my program. Here is my code so far: [CODE] #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]){ FILE* fin; int a; char myString[3];; fin=fopen(argv[1],"r"); fscanf((fin,"%s", &myString)!=EOF); putchar('\n'); printf("%s\n", myString); return 0; } [/CODE] What I am trying to accomplish is to create a program that will take individual characters from a file, each on a separate line, and compose them into a single string in memory while keeping the first character …

Member Avatar
Member Avatar
+0 forum 7

Hi guys, I am implementing a singly linked list. The thing is whenever my str_temp goes above 12-15 elements, i get this error malloc: *** error for object 0x1099008b8: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug Run Command: line 1: 7322 Abort trap: 6 ./"$2" The error is happening in AppendNode method. Can you please tell me where I might be going wrong. Code snippet is given below. Please let me know if you need more info. [CODE] class node{ public: int data; class node* next; …

Member Avatar
Member Avatar
+0 forum 5

Hello just curious, I know that i'm supposed to use new in c++ but decided to experiemnt with malloc. Can you explain to me why the following code crashes and what is the reason. I was under the impression that the size of vector is fixed as it just stores a pointer to an dyanamic allocated array which holds the elements. [ICODE]#include<iostream> #include<vector> using namespace std; int main() { vector< string > * u = (vector< string >*) malloc( sizeof(vector< string >)) ; *u = vector< string >(10,"test"); system("pause"); }[/ICODE] My understanding is that malloc allocates memory to the size …

Member Avatar
Member Avatar
+0 forum 13

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. [COLOR="Red"][B]A brief on problem:[/B][/COLOR] Lets take example problem for simplicity: - I have a GSM network with a cell in it. - Cell's coverage area is divided into a pixel grid with dimensions 2000x2000. - at each pixel the signal strength is measured. Requirement is to find out the % of pixels in a cell that have below par signal strength. [COLOR="Red"][B]Solution 1 using the good old C:[/B][/COLOR] In C, memory is dealt in …

Member Avatar
Member Avatar
+3 forum 8

what is wrong with my code?i cant figure it out. [CODE] #include<iostream.h> #include<stdlib.h> #include<stdio.h> #define MAX 50 struct Node { char judul[MAX]; char pengarang[MAX]; int harga; Node* nextPtr; }; Node* makeNode(char judul[], char pengarang[], int harga) { Node* newNodePtr = (Node*)malloc(sizeof(Node)); if(newNodePtr == NULL) { cout << "Out of Memory" << endl; exit(1); } else { newNodePtr->judul = judul; newNodePtr->pengarang = pengarang; newNodePtr->harga = harga; newNodePtr->nextPtr = NULL; } return newNodePtr; } void insertNode(char judul[],char pengarang[],int harga) { Node* headPtr = NULL; Node* rearPtr = NULL; Node* newNodePtr = NULL; Node* myPtr = NULL; Node* newPtr = NULL; Node* prevPtr …

Member Avatar
Member Avatar
+0 forum 8

I am having trouble correctly allocating memory for a parent function that will remain upon the child functions exit. the call to the child function is [code=c]HTTP_packToGen((char*)http_headerContent.HTTP ..[/code] the HTTP variable within http_headerContent is a char pointer that isn't allocated until the child function is called, http_headerContent is a static struct. The following is called within HTTP_packToGen which is the child function [code=c] if (hdrContent == NULL) { hdrContent = (char*)malloc((strlen(command)+strlen(data)+sizeof(bool))+4); memset(hdrContent, '\0', sizeof(*hdrContent)); } [/code]

Member Avatar
Member Avatar
+0 forum 9

char* p=(char*)malloc(10); in the above state ment,has the memory been initialised??...what is the difference between allocatingg it and initialising it?? thanks in avdance...

Member Avatar
Member Avatar
+0 forum 3

Hello, This is an extension of this thread: [url]http://www.daniweb.com/software-development/c/threads/357681/1525001#post1525001[/url] My purpose is to create a class that handles a network message. Irrelevant, anyway. CMyMessage: [code=C] #include "stdafx.h" #pragma once #include <conio.h> #include <stdio.h> #include <iostream> #pragma once using namespace std; typedef unsigned char byte; class CMyMessage { protected: byte type; byte textlen; byte namelen; char *text; char *name; public: CMyMessage(byte* message) { } CMyMessage(byte Type, char *Name, byte Namelen, char *Text, byte Textlen){ printf("\ns-a apelat constructorul\n"); type= Type; name=( char*) malloc (Namelen+1); strcpy(name,Name); namelen=Namelen; text=( char* ) malloc (Textlen+1); strcpy(text, Text); textlen=Textlen; printf("\nType is: %d",type); printf("\nName is:"); while (*name) printf("%c",*name++); …

Member Avatar
Member Avatar
+0 forum 14

Hello, My program looks something like this: [code=C] char *name= (char *)malloc(200); unsigned char type=1; unsigned char GetType(){ return type; } char* GetName(){ return name; }; void function(unsigned char* message, int* intPtr){ message[0]= GetType(); // works correctly // CODE HERE } void main(){ unsigned char *message= (unsigned char *) malloc(256); int *ptrInt = (int *)malloc(sizeof(int)); function(message, ptrInt); printf("message[0]= %d",message[0]); // works correctly } [/code] I have a pointer to an array of 256 unsigned chars. I am parsing that pointer as a parameter to function(message, intPtr). Inside function(), I want to load the first byte from my array ( message[0]) …

Member Avatar
Member Avatar
+0 forum 3

The problem is I use malloc in the last function(toString()). I just wanna know if there is anyway to free it? or that I dont have to? Thanks [CODE] #include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> /* Function prototypes */ void process(char input); void update(char words[][51], int position); char * toString(char character); /* the global variables */ char words[100][51]; //the array of the words inside the text int lastWordPos = 0; //position of the last word in array words /* Main Function */ int main() { char character = getchar(); //get a character from stdin char tempWord[51] = ""; //temporary array for storing …

Member Avatar
Member Avatar
+0 forum 9

HMm... already frustrated at it... But is there something wrong in my malloc? Everytime I try to display the record, it gives me random things as answer(see code below)? Was it that I didn't free it or was I wrong in declaring malloc itself? Thanks in advance. [CODE] #include <stdio.h> #include <stdlib.h> typedef struct{ //Structure for student profile char fname[100]; char lname[100]; char college[50]; char course[50]; int batch; //Year Entered UP. int snum; //Student Number int age; float gpa; }student; int search_snum(); void display_All(); void edit(); void menu(); main(){ student s; int x; //Choices for the menu, x for the …

Member Avatar
Member Avatar
+0 forum 8

I am trying to create an abstract data type for matrices. I have a function matrixInit to dynamically allocate memory for the structure and for the array. matrixCopy copies one matrix into another. there are functions to subtract, add and multiply matrices that return a pointer to memory where the Matrix structure is. I have included my .c file and my .h file below. I'm not exactly sure what's not working. I would appreciate it if someone could look at what I have and let me know where I'm going wrong. I believe that my first function is correct, and …

Member Avatar
Member Avatar
+0 forum 2

Hi, This is probably, a really novice question, but I have some code that is using the malloc() function to declare an array, the problem is that I am using a special compiler to compile code for hardware which gives me a lot of errors because of the stdlib.h header file. My code looks as follows: [CODE] double (*x)[2]; // Pointer to variable is declared x = malloc(2 * N * sizeof(double)); //Memory is allocated //Then I fill the array for(i=0; i<N; i++) { x[i][0]=data[i]; x[i][1]=0; } //The array is then passed to a function function(x); [/CODE] In the header …

Member Avatar
Member Avatar
+0 forum 7

The program is fetching me the right output but still i am getting a warning #include<stdio.h> #include<string.h> int main() { char *p,*q; p=(char *)malloc(25); q=(char*) malloc(25); strcpy(p,"hello" ); strcpy(q,"hi"); strcat(p,q); printf("%s",p); } Warning test7.c: In function ‘main’: test7.c:6: warning: incompatible implicit declaration of built-in function ‘malloc’ wat exactly is the problem ?

Member Avatar
Member Avatar
+0 forum 2

I have basic idea about malloc and calloc i heard about falloc and dlmalloc, can any one explain about it?

Member Avatar
Member Avatar
+0 forum 4

I have an assignment that requires me to not use an array with [] at all throughout my code, read a file in a certain way, and I need a better understanding with malloc as well with pointers. The code with the [] is pretty easy for me, but pointers confuses me. I don't expect every single one of these to be answered which is fine. For example: /*input from datafile.dat*/ 3 2 4 1 2 3 4 5 6 7 8 I understand that I need to get all that in the array which I got down. Array[2] should …

Member Avatar
Member Avatar
+0 forum 4

I've a 2-d array declared as such [CODE]lower_tri = (float **)malloc(sizeof(float *)*(dimention)); for(i =0; i < dimention; ++i) lower_tri[i] = (float *)malloc(sizeof(float)*(i+1));[/CODE] So that it looks like... [] [][] [][][] [][][][] ... However, it causes a segmentation fault when I try to de-allocate the array with [CODE]free(lower_tri);[/CODE] (only in unix, Windows works fine) I've tried using a for loop to deallocate the array row by row, but just come up with more errors. Any idea how to deallocate this array?

Member Avatar
Member Avatar
+0 forum 3

Hi, I am developing a large scale code. In order to save the space, when I declare any pointer, I allocate it in another subroutine. Now it seems that it does not work when I use an array of pointers. I can summarize the code as: [CODE] #include "headers.h" /** Object of extrapolation coefficients **/ typedef struct{ double *zero[2]; double *one [2]; }prt_interpln; int main(){ /** Declare functions **/ double *interp(const int, const int ); /** Decalre variables **/ const int length = 4; int i; double y; /** Declare pointer **/ prt_interpln *interp_coeff; interp_coeff->zero[0] = interp(length, 0); interp_coeff->zero[1] = …

Member Avatar
Member Avatar
+0 forum 8

hello all i have a problem with a "while condition" useing c. my assignment was to create a dynamical array, using malloc. i did that and all worked as it should. the only thing i dont like,is when it asks you if you want to continue, putting in integer. you have to write an (int) 1 and i wanted a (char) 'y' insteed but then it dont work anymore :( the while condition is like this [CODE]int e=0; if(e==1) {.....;} while(e==1) {....;}[/CODE] i tried alternate the while condition like this [CODE]char c; if(c=='y') {....;} while(c=='y') {....;}[/CODE] of course i also …

Member Avatar
Member Avatar
+0 forum 4

Hi, I am attempting to create a dynamic array using realloc in 2 dimensions. Currently i have managed to get it to work using realloc to dynamically resize the amount of rows but when I attempt to use realloc to dynamically resize the amount of columns per row I keep getting errors. I am currently therefore using malloc() to set the amount of columns per row. I think that there is either something wrong in my code, or it is not possible. Below is a rough example of what I am trying to do, could someone please let me know …

Member Avatar
Member Avatar
+0 forum 7

Dear all, I am writting a code for high dimensional chebyshev interpolation. Since it is high dimensional, I MUST use extremely large arrays. Something like this: [CODE] #define DIM (int)2 #define TOTAL_STEPS (int)30 int main(){ double *a_data; long long a_dim; /** length of matrix a_data **/ int n; /** # of given points **/ int grid_length; grid_length = TOTAL_STEPS; n = pow( grid_length, DIM); /** Length of array is here **/ a_dim = n * pow(n + 1, DIM); /** which is 730620900 **/ /** Allocate memory **/ a_data = (double*) malloc( a_dim * sizeof(double)); if(NULL == a_data) printf("no memory"); …

Member Avatar
Member Avatar
+0 forum 6

The End.