473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

allocate space for typedef data type

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;
}

Nov 14 '05 #1
5 3567
"Janice" <no@mail.com> writes:
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?

--
Chris
Nov 14 '05 #2
Janice wrote:
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 <stdlib.h>
main(){ 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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #3
"Janice" <no@mail.com> wrote in message
news:cm*********@imsp212.netvigator.com...
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
Nov 14 '05 #4


Janice wrote:
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;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #5
Janice wrote:
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;
}
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lixiaoyao | last post by:
hi all I use matrix & vector function to allocate the space myself in c, typedef struct matrix_array newdata; struct matrix_array{ float **sy,*sxx; }; newdata ndata;//new data struct...
14
by: dreamcatcher | last post by:
I always have this idea that typedef a data type especially a structure is very convenient in coding, but my teacher insisted that I should use the full struct declaration and no further...
15
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! )...
21
by: Peter Olcott | last post by:
I got the previous alias to std::vector working, and found that it takes up the space of a pointer. I want to find a way to do an alias to a std::vector that does not take up any space. Is there...
20
by: ramasubramanian.rahul | last post by:
hi folks i have a peculiar problem. i have to allocate more than size_t consequtive bytes on a system . after i do a malloc .. i am unable to do a realloc because it takes size_t as a new size...
13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
17
by: dtschoepe | last post by:
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable...
16
by: mdh | last post by:
A quick ? :-) question about Typedefs. There is a very brief discussion about this in K&R ( p146). Googling this group, there is a surprising dearth of questions about these. From one of the...
28
by: Trups | last post by:
HI, I want to dynamically allocate array variable of type LPWSTR. Code looks like this... main() { LPWSTR *wstr; int count = Foo (wstr); for (int i = 0; i < count; i++)
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.