I'm tasked to create a program which dynamically allocates memory for a structure.
normally we would use
x=malloc(sizeof(int)*y);
However, what do I use for a structure variable?
I don't think its possible to do
struct st ...
|
I asked a question earlier on defining a structure using malloc.
This was the answer I was given by the majority:
struct retValue* st = malloc(sizeof(*st));
I was showing a friend my code, ... |
I have a structure defined like so:
typedef struct {
int n;
int *n_p;
void **list_pp;
size_t rec_size;
int n_buffs;
size_t buff_size
} fl_hdr_type;
and in my code I Have a function for initlialization ... |
My question's pretty basic, but it's been a while. I'm reading in a text file and saving numbers in the text to a struct 'Record'. After I read text to my ... |
I'm writing a program with struct Record. As I read in records from text in a loop, I assign them to buffer before saving buffer into the array. nRange is just ... |
I am trying to implement a Queue in C. Coming from Java and other managed languages, I am really struggling with memory management. Here is the enqueue() function:
int enqueue(Queue q, int ...
|
int i=0;
void push(int *ptr)
{
if(i==0)
{
ptr= (int *)calloc(1,sizeof(int));
}
else
...
|
|
It's amazing how even the littlest program can cause so much trouble in C.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct ...
|
I malloc a 2d array. The 2d array is part of a struct and when I try malloc is I get an error that malloc has too many arguments.
malloc(world->representation, sizeof(int ...
|
Here is my code:
int main(void)
{
int i;
Coords** latLng;
Quadrado* q1;
latLng[0] = AdicionaValores(latLng[0],-23.000490,-43.346687);
latLng[1] = ...
|
I would like some advice on safe ways to deal with struct's when the size of certain members are not known at code time.
For example I have a Struct Named "Channel". ... |
I just learned that it's possible to increase the size of the memory you'll allocate to a struct when using the malloc function. For example, you can have a struct like ... |
I have the following header file:
typedef struct my_data my_data_t;
my_data_t* new_my_data(void);
void free_my_data(my_data_t* my_data);
And the corresponding c file:
typedef struct my_data
{
int val;
} my_data_t;
my_data_t* new_my_data()
{
my_data_t* ptr = (my_data_t*)malloc(sizeof(my_data_t));
return ptr;
}
void ...
|
Thank you for the clarification, but I'm still unsure of the meaning of the double star **. As well I'm not sure how i would malloc one of the members of the struct. If you or someone could show me an example of mallocing a struct that is declared the way mine is (with the **) I would greatly appreciate it. ... |
hi I am a newbie to c coding and going thru K&R ... i have a structure to represent an image as struct image{ char* imagename; int imagewidth; int imageheight; long* pixels; }; typedef struct image img; typedef img* imgptr; here long * pixels shd point to an array of long values representing pixels of the image.Arraysize (ie number of pixels ... |
typedef struct { int a; int b; int c; } ABC; typedef struct { int d; int e; int f; ABC *abc; } DEF; Create function should use a single call to malloc to allocate memoryfor structure DEF and its constituting structure ABC. In other words, after a call to the CreateDEF() function, the following statements should be valid and should ... |
|
I have a structure defined as: struct channel { int chanid; int channum; char callsign[20]; char name[64]; }; I then have a global variable defined as: static struct channel *chan=NULL; I then malloc the space when I know the total number of rows (dynamic variable Total_rows) I have: if ( (chan=(struct channel*)malloc(sizeof(struct channel)*Total_rows)) = = NULL) { snprintf(buf, sizeof(buf),"Mallor Error.\n" ); ... |
My question is quite basic. I have a function "ds707_async_orig_hdlr" which gets called by another module. cm_cdma_orig_params_s_type : is a struct defined in teh same file as this function. Now, when the entire code (with all other files) is run, I get a lint error that points to the possibility of assignment to a NULL pointer in the lines assigning values ... |
|
#include #include struct nodePayload{ int number; }; struct node{ struct nodePayload *payload; struct node *next; } *headSingle, *tailSingle, *temp; S_ADD(int z){ temp = malloc(sizeof(struct node)); //Crashes here temp->payload->number = z; if (headSingle == NULL){ headSingle = temp; tailSingle = temp; } else{ headSingle->next = temp; tailSingle = temp; } } main(int argc, char *argv[]){ headSingle = NULL; tailSingle = ... |
|
In the most recent C standard (C99), the last member of a structure may be a variable-sized array, so maybe that's what's going on. Try compiling under strict ANSI rules (-ansi on gcc) and see what happens. Malloc just returns a block of bytes, it doesn't know what type of object you're trying to allocate. So you get a block of ... |
|
help with structs and malloc! I'm writing a header files and I must be allocating the space wrong because I'm trying to do a linked list but the nodes keep changing. table.h (some of these I haven't written yet) Code: #ifndef TABLE_H_ #define TABLE_H_ #include struct lineNode; struct entry; struct table; /* returns an empty table */ struct table * ... |
|
|
Hi all, I'm writing a program to read some experimental data from a file, then do some manipulations, and then to write the new data into another file. The program works ok, but after a few datapoints, the program does not read values and prints zeros instead. For a single reading, I have the format of "date time value", like the ... |
Code: #include "stdio.h" #include "math.h" #include "string.h" #include "stdio.h" #define MaxNameLen 512 #define MaxNum 10 struct Students { char **names[MaxNameLen + 1]; char grades; }; typedef struct Students Students; int main(int argc, char** argv) { int i,j; int numStudents; Students stu; do{ printf( "Enter the number of students (1-10): "); scanf( " %d", &numStudents); }while(numStudents <= MaxNum && numStudents > 0); ... |
#define MAX_T 42 #define MAX_CHARS 42 #define SOME_INT 42 struct t_container { struct t_named *ptrFirst; }; struct t_named { char name[MAX_CHARS]; int x; }; int function(int index, struct t_container *ptrContainer); int main(){ int i; struct t_container *new; new = malloc( sizeof *new ); new->ptrFirst = malloc( MAX_T * sizeof *new->ptrFirst ); for(i=0; i |
#define MaxNameLength 512 #define MinStudents 1 #define MaxStudents 10 struct Student { char* names; float pGrades; }; typedef struct Student Student; int main( int argc, char** argv ) { int numStudents = 0; int i; char longName[MaxNameLength]; int nameLength; Student* students; Student.names = malloc( numStudents * sizeof( Student.names* ) ); if( !Student.names ) { printf( "Insufficient memory to allocate names array ... |
|
|
I see. I don't really care if the content is there, just that there are no memory leaks (i.e. free() has done its job) Should I care about the content, I think a bzero() before free() ought to do the trick, right? PS: I suppose that this is going to happen with every object in the structure that is not a ... |
I'm trying to understand some code I am debugging. Is it correct to assign dynamic memory to a struct and then allocate dynamic memory to a member of the struct? It seems to me when you first allocate memory to a struct, that would be all the memory the struct as whole should use. Example. Say I have a struct defined ... |
|