allocate space for typedef data type

J

Janice

Always get segmentation fault for the following code.
If the data is primitive type instead, it will be fine.
What is the problem?
Thanx

main(){
typedef int pid_t;
pid_t pid=12;
pid_t* ary;
int count=0;
ary=(pid_t*)realloc(ary,(count+1)*sizeof(pid_t));
*(ary+count)=pid;
}
 
C

Chris McDonald

Janice said:
Always get segmentation fault for the following code.
If the data is primitive type instead, it will be fine.
What is the problem?
Thanx
main(){
typedef int pid_t;
pid_t pid=12;
pid_t* ary;
int count=0;
ary=(pid_t*)realloc(ary,(count+1)*sizeof(pid_t));
*(ary+count)=pid;
}


What is your initial value of ary?
 
M

Michael Mair

Janice said:
Always get segmentation fault for the following code.
If the data is primitive type instead, it will be fine.
What is the problem?
Thanx
#include said:
main() has return type int; make this
int main (void) {
typedef int pid_t;
pid_t pid=12;
pid_t* ary;
int count=0;
ary=(pid_t*)realloc(ary,(count+1)*sizeof(pid_t));
realloc() works on memory previously allocated by malloc().
As ary is initialized with an arbitrary value, realloc() tries
to access the information somewhere. You are lucky that it
crashes and not seems to work.

Also, it is a Bad Idea (TM) to cast the return value of
malloc and realloc. Don't do this. Just use google groups
search on comp.lang.c to find out why.
Note: If you use sizeof *ary instead of sizeof(pid_t),
this works even if you change the type of ary.
*(ary+count)=pid;
}

Your code does not work if you throw out the typedef
and replace pid_t by int, so there is no surprise.
Note: Do not use typedef at block scope. Sooner or later,
you will go for functions and will need a typedef at
file scope. Even better: Put it into a header file, then
you can #include it in order to make it available.

The comp.lang.c FAQ will help you understand the issues.
Best download the ASCII version and search through it for
malloc(), realloc(), typedef, ... Start here:
http://www.eskimo.com/~scs/C-faq/top.html


Cheers
Michael
 
A

Alex Fraser

Janice said:
Always get segmentation fault for the following code.
If the data is primitive type instead, it will be fine.
What is the problem?
Thanx

main(){
typedef int pid_t;
pid_t pid=12;
pid_t* ary;
int count=0;
ary=(pid_t*)realloc(ary,(count+1)*sizeof(pid_t));

As well as the problems already mentioned by others, note that it is
normally an error to assign the return value of realloc() to the pointer
used as the first argument. If realloc() fails, it returns NULL, and leaves
the old allocation intact. If you overwrite your only copy of the pointer to
the old allocation, then you can never deallocate it - you have a memory
leak.

For realloc(), I normally write something like:

pid_t *ary, *temp;
/* ... */
temp = realloc(ary, (count + 1) * sizeof *temp);
if (!temp) /* handle error, usually by returning to caller */;
ary = temp;
/* ... */

Some might prefer the equivalent but more explicit test "if (temp == NULL)"
or similar, but otherwise I think the above idiom is fairly universal.
*(ary+count)=pid;

ary[count] = pid;

Is equivalent, and usually makes more sense to me.

Alex
 
A

Al Bowers

Janice said:
Always get segmentation fault for the following code.
If the data is primitive type instead, it will be fine.
What is the problem?
Thanx

main(){
typedef int pid_t;
pid_t pid=12;
pid_t* ary;
int count=0;
ary=(pid_t*)realloc(ary,(count+1)*sizeof(pid_t));
*(ary+count)=pid;
}

The code is incomplete and flawed. The cause of the seg. fault
is your use of function realloc. You need to study the description
and use of the function. Function realloc requires that the first
argument, ary in this case, have a value that represents the
previous return or functions, malloc, realloc, or calloc. Or, that
the argument have value NULL. You use of the ary argument is none
of these which results in the fault.

Among the possible solutions are:
1)
ary = realloc(NULL,(count+1)*sizeof(pid_t));

2)
pid_t *tmp, *ary = NULL;
tmp = realloc(ary,(count+1)*sizeof(pid_t));
if(tmp) ary = tmp;

The code corrected:

#include <stdio.h>
#include <stdlib.h>

typedef int pid_t;

int main(void)
{
pid_t pid=12;
pid_t* ary;
int count=0;

ary = realloc(NULL,(count+1)*sizeof(pid_t));
if(ary)
{
*(ary+count)=pid; /* Equiv: ary[0] = 12 */
printf("ary[0] = %d\n",ary[0]);
free(ary);
}
return 0;
}
 
J

Joe Wright

Janice said:
Always get segmentation fault for the following code.
If the data is primitive type instead, it will be fine.
What is the problem?
Thanx

main(){
typedef int pid_t;
pid_t pid=12;
pid_t* ary;
int count=0;
ary=(pid_t*)realloc(ary,(count+1)*sizeof(pid_t));
*(ary+count)=pid;
}
/*

Hi Janice,
Fire your teacher. Burn the books. Alternatively, learn reading.

See if the following works for you. Examine it closely.

*/

#include <stdio.h>
#include <stdlib.h>

typedef int pid_t;

int main(void) {
pid_t pid = 12, *ary = NULL, *tmp;
int count = 0;
tmp = realloc(ary, (count + 1) * sizeof *tmp);
if (!tmp)
fprintf(stderr, "Allocation failed.\n"), exit(EXIT_FAILURE);
ary = tmp;
*(ary + count) = pid;
printf("Success! pid == %d\n", ary[0]);
return EXIT_SUCCESS;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top