struct pointer and void pointer confusion while doing malloc - c

typedef struct data
{
int *data_array;
int *thread_number;
}array;
....
int main(int argc, char *argv[]){
...
int size = atoi(argv[1]);
array *array_ptr;
array_ptr->data_array = malloc((size+2)*sizeof(int));//failing line
....
}
because of the failing line it gives me seg. fault
how must solve it

In your code,
array *array_ptr;
array_ptr->data_array = malloc((size+2)*sizeof(int));//failing line
array_ptr itself is used uninitialized. If you try to dereference an unitialized pointer, you'll invoke undefined behavior. Segmentation fault is one of the many effects of UB.
You should allocate memory to array_ptr first, like
array *array_ptr = NULL;
array_ptr = malloc(sizeof *array_ptr);
if (array_ptr) //check for malloc success
{
array_ptr->data_array = malloc((size+2)*sizeof(int));
.....
}
else
{
printf("failure\n");
exit(-1);
}

When you define a local variable like this
array *array_ptr;
array_ptr pointer is given a place in memory, but it does not point to a valid array struct in memory: the value of uninitialized variable contains some random "garbage" value. Therefore, dereferencing it in any way, including
array_ptr->data_array = malloc ...
is undefined behavior.
You can fix it by allocating some memory to the array pointed to by array_ptr, like this:
array *array_ptr = malloc(sizeof(array));

Segmentation fault is expected as you have not created any memory for array_ptr and you are trying to reference it which eventually will try to access the data_array(for which there is no memory yet ) So , kernel will signal the process to terminated and hence you will get segmentation fault here .
int size = atoi(argv[1]);
array *array_ptr;
array_ptr->data_array = malloc((size+2)*sizeof(int));//failing line
However , to fix this , you shold first create memory for array_ptr .
array *array_ptr = (array * )malloc(sizeof(array));//type casting is necessary as malloc returns pointer to void .
After this you can check the pointer and assign memory to its data members
if(array_ptr )
{
array_ptr->data_array = malloc((size+2)*sizeof(int));//type cast here also
}

int size = atoi(argv[1]);
array *array_ptr;
array_ptr->data_array = (*int) malloc(sizeof(int)*(size+2));
Works?
-- Edit --
I get the number from argv and transform in int.
Create the structute.
I create the memory whith the size than a int plus (arg + 2), and associate this in the struct, only

Related

Freeing a pointer in a structure referenced by a pointer

I have a pointer to several structures that have been allocated memory via:
STRUCTNAME *ptr;
ptr = (STRUCTNAME *)malloc(sizeof(STRUCTNAME)*numberOfStructs);
The structures are accessed via a offset like so:
(ptr + i)->field;
The structures have 2 fields that are character pointers as follows:
typedef struct
{
char *first;
char *second;
}STUCTNAME;
These fields are allocated memory as follows:
(ptr + i)->first = (char *)malloc(strlen(buffer));
This appears to work but when I try to free the pointers within the structures I get a segmentation fault 11 when I do this:
free((prt + i)->first);
Help?
Notes:
buffer is a character array. Offsetting a pointer by a integer should increment the pointer by the size of what it is pointing to times the integer correct?
Here is a link to my full source code. I have not written some of the functions and I am not using the freeAllpointers and printAll yet.
https://drive.google.com/file/d/0B6UPDg-HHAHfdjhUSU95aEVBb0U/edit?usp=sharing
OH! Thanks everyone! Have a happy Thanksgiving! =D (If you're into that kinda stuff)
In case, you don't initialize all those members in that piece of code, you're not showing us:
Allocate the struct storage (STRUCTNAME*) with calloc(), so that all allocated memory, namely firstand second are zero at the beginning. Passing NULL to free() will result in a no-op. Passing any wild (garbage) pointer to free() may cause a segmentation fault.
To detect a double-free, set ptr[i].first = NULL; after free(ptr[i].first); as a defensive measure for testing.
Notes: buffer is a character array. Offsetting a pointer by a integer
should increment the pointer by the size of what it is pointing to
times the integer correct?
Yes, except for void* on those compilers, which don't define sizeof(void), which is defined to have undefined behavior, to a value > 0: What is the size of void?
Edit:
void makeReviews(FILE *input, REVIEW *rPtr, int numReviews) <-- This does NOT return the new value of rPtr. In main(), it will remain NULL.
Do something like this:
REVIEW* makeReviews(FILE *input, int numReviews);
//...
int main(){
//...
rPtr = makeReviews(input,numReviews);
//...
}
or
void makeReviews(FILE** input,REVIEW** rPtrPtr,int numReviews){
REVIEW* rPtr = *rPtrPtr;
//...
*rPtrPtr = rPtr;
}
//...
int main(){
//...
makeReviews(input,&rPtr,numReviews);
//...
}
fgets(cNumReviews, sizeof(cNumReviews), input); <-- Perhaps, you could use something like fscanf().

integer pointer is not working while assigning two dimensional array via another pointer?

I was trying to copy the contents of one 2d array to another using pointers. I wrote this simple test program but it shows me segmentation fault but i still cannot find a rock solid reason why?
#include <stdio.h>
void main(){
int m[2][3]={
{2,3,4},{5,6,7}
};
int *p=m;
int *n;
int i,j;
for(i=0;i<2;i++){
for(j=0;j<3;j++){
printf("%d \t", *(p+3*i+j));
printf("Debug here\n");
*(n+3*i+j)=*(p+3*i+j);
}
printf("\n");
}
}
// Output:
// 2 Debug here
// Segmentation fault (core dumped)
int *n is an unitialized pointer, you never allocated memory for it, therefore you are trying to write your copy onto unknown territory.
You can alloc space to n using this:
int *n = (int *)malloc(2 * 3 * sizeof(int));
You are not allocating space for your target array. At minimum, you should do:
int *n = malloc(2 * 3 * sizeof(int));
or
int n[2][3];
A pointer is a variable that points to a memory location. It stores the address of the memory location. You can access that location by dereferencing the pointer using *.
In your case,
int *n;
This only declares a pointer to an int. This can point to a single integer or an array integers. You have not yet assigned any value to it (It is still not assigned a memory location). We don't know where it is pointing right now (You might not be allowed to access the memory etc.). (That's why the seg fault)
You need to allocate memory as such,
int *n = malloc(2 * 3 * sizeof(int));
The advantage of a pointer to a dynamic array than a static array
int n[2][3];
is that the pointer can be re-used (Of course you need to take care of freeing the existing memory before resuing the pointer [If you dont have any other access paths to the memory])
reason why?
The destination area is not ensured
#include <stdlib.h>
int *p=&m[0][0];
int *n = (int*)malloc(sizeof(m));

Pointer to a Pointer to a Structure and malloc memory for array

I'm creating a pointer to a pointer to a structure to create a dynamic array with malloc in C, but I get a segmentation fault calling the struct array. Here is a quick rundown of my code:
#include <stdio.h>
typedef struct {
int test1;
int test2;
}testStruct;
int main() {
testStruct **neato;
neato = (testStruct **) malloc( sizeof(testStruct *) * 5);
// Array of 5 for convience
// any neato[x]->testy call results in segmentation fault.
scanf("%d", &neato[0]->test1); // Segmentation fault
return 0;
}
I tried other calls like (*neato)[0].test1 and all result in segmentation fault. This is obviously not the proper way to do this or my GNU compiler is seriously outdated.
You've allocated enough memory for 5 pointers. You have not however initialized the pointers, so they are garbage. Allocate the pointers and then proceed to initialize each pointer.
int elems = 5;
neato = malloc(sizeof(testStruct *) * elems);
for( i = 0; i < elems; ++i ) {
neato[i] = malloc(sizeof(testStruct));
}
On a side note, I don't see a need for an array of pointers here. Why not simply allocate enough space for 5 testStructs (i.e., neato becomes a testStruct*) and pass the address of that pointer to the function that initializes it?
you aren't mallocing space for all the structures themselves you have to add
for(int i = 0; i < 5; i++) {
neato[i] = malloc(sizeof(testStruct));
}
After you malloc neato. Also you should check your return value from malloc for NULL to make sure malloc passed.
You allocated array of pointers, but did not assign valid address to these pointers.
If you only want to create dynamic array, use just pointer to the struct:
testStruct *neato;
neato = malloc( sizeof(testStruct) * 5);
scanf("%d", &neato[0].test1);

c malloc in other function and structs

Im having problems with c and pointers. I keep grinding on this and it has to be easy. I have a struct and I allocate in one function, then pass the pointer back to the original function. But when I try to fill the values of the struct with other variables, and then print them or copy them , the app segfaults saying the memory address is out of bounds.
struct memcache_buffer{
int elements, action;
char keys[MAX_KEYS], values[MAX_KEYS], returns[MAX_KEYS]; //action 0 = delete , 1 = get 2 = set
}memcache_buffer;
struct memcache_buffer* memcache_allocate_buffer(int size){
struct memcache_buffer *buffer;
buffer =malloc(sizeof(struct memcache_buffer));
return buffer;
}
void memcache_set(char * key, char * value){
pthread_t process_t;
struct memcache_buffer *buffer=memcache_allocate_buffer(1);
char keys,values;
buffer->elements = 1;
buffer->action=2;
//printf("crash?\n");
printf("%s %s",key,value);
snprintf(buffer->keys[0],KEY_SIZE,"%s",key);
snprintf(buffer->values[0],VALUE_SIZE,"%s",value);
pthread_create(&process_t,NULL,memcache_process,buffer);
}
am I allocating the memory right? allocating memory and these pointers are sure rough, especially only messing with php in the past.
Here's your problem:
struct memcache_buffer{
char keys[MAX_KEYS], values[MAX_KEYS]
}
snprintf(buffer->keys[0],KEY_SIZE,"%s",key);
^^^
snprintf(buffer->values[0],VALUE_SIZE,"%s",value);
^^^
Drop the [0] or snprintf will try to dereference some bogus value.

Segmentation fault when initialization array

I have a structure called string
typedef struct {
char *s;
int len;
} string_t;
typedef struct {
uint32_t numb;
} msg_t;
where in the function
void myfunct()
{
msg_t msg;
memset(&msg, 0, sizeof(msg));
msg.numb = 1;
char *ClientSendBuf[sizeof(msg)];
string_t buffer = {ClientSendBuf[sizeof(msg)],strlen(ClientSendBuf[sizeof(msg)])};
}
Tried to initialize an array (basically a buffer that I need to send later on) using UDP,
but it gives me an error of segmentation fault (on the third line in void myfunct.
So the thing with buffer is that it should be a type of string_t, how can I fix this segmentation fault?
P.S. I forgot to mention, I want to copy the whole structure to the buffer variable (that should be type string_t) using memcopy. So am I doing the wrong thing above? How can I do this?
There are a few things you have to consider in initializing your structure, as it has a pointer member char *s simple assignment will not work. Simple assignment will just copy the pointer address and not the content it is pointing to.
There are a few problems in your assignment code:
1. You declared an array of char * with sizeof(msg) elements, none of which are allocated memory; but your structure need char * and not char *[]
2. You are accessing an array element which is out of bounds (ClientSendBuf[sizeof(msg)]) and also not pointing to any valid address.
You can create a simple char array & copy it to the structure. As you are using a pointer member it is your responsibility to allocate memory and free memory.
Hope the code below can provide you with some references:
void myfunct()
{
msg_t msg;
memset(&msg, 0, sizeof(msg));
msg.numb = 1;
char ClientSendBuf[] = "This is my message";
string_t buffer = {
strdup(ClientSendBuf), /*Can return NULL so add error check*/
strlen(ClientSendBuf)
};
/** Or **/
string_t buffer;
buffer.s = malloc(strlen(ClientSendBuf)+1);
if(NULL == buffer.s)
{
/* Memory allocation failed. Handle error.*/
}
/* Zero fill */
memset(buffer.s, 0, strlen(ClientSendBuf)+1);
strcpy(buffer.s, ClientSendBuf);
buffer.len = strlen(ClientSendBuf);
/*Opeartions with buffer*/
/*Call free in both cases !*/
free(buffer.s);
}
Hope this help!
ClientSendBuf - put some thing in it and also put it on the heap.
The problem is that you don't allocate memory to any element of ClientSendBuf. You should use malloc here to first allocate the memory.
I see two things that are wrong. First, accessing ClientSendBuf[sizeof(msg)] is undefined behavior, because that character is after the end of CliendSendBuf. Then you're assigning a char (namely ClientSendBuf[sizeof(msg)]) when a char * is expected.
And if you want to use buffer outside that function you have to put ClientSendBuf on the heap, because it will be overwritten by other stack frames after you exit (i.e. sort of deleted), so the pointed data will be throwed off.
Now, since you want a copy of the whole ClientSendBuff, you need an array of string_t. Then, you assign every pointer in ClienSendBuff to buffer:
char *ClientSendBuff[sizeof(msg)];
string_t buffer[sizeof(msg)];
int i;
for(i = 0; i < sizeof(msg); i++) {
ClientSendBuff[i] = malloc(100); // you have to initialize (and free when
buffer[i].s = ClientSendBuff[i]; // you don't need them anymore) every pointer
buffer[i].len = 100;
}
But I'm not sure if I got your point. How can a char * [] fit in a char*?

Resources