473,387 Members | 1,798 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,387 software developers and data experts.

Segmentation Fault due to strcpy

Hi,
I bet you've all been asked this a million times but here goes! :)

I'm coding an English language dictionary which is stored in a tree by way of the following structure:

Expand|Select|Wrap|Line Numbers
  1. #define MAXWORDSIZE 100
  2.  
  3. typedef char word[MAXWORDSIZE];
  4. typedef struct dictionary *dict;
  5.  
  6.  struct dictionary {
  7.    dict left,right;
  8.    word theword;
  9.  } ;
  10.  
Now comes the problem; I've written a function which adds a word into the appropriate null left/right leaf; which should work as intended apart from the following line which I singled out as causing a segmentation fault (using GDB & note that w is a char w[] argument of the function and that a dict d is also passed):

Expand|Select|Wrap|Line Numbers
  1. strcpy(d->theword, w);
I believe this is due to the fact that I've not allocated memory (using malloc) to the 'theword' element of 'd'.

I have attempted to do this using the following code on the line above the 'strcpy' line:

Expand|Select|Wrap|Line Numbers
  1. d->theword = (char *) malloc(sizeof(d->theword[MAXWORDSIZE]));
But I am getting these errors from that line:

Expand|Select|Wrap|Line Numbers
  1. error: incompatible types in assigment
By this you have all probably realised that I'm very new to C's menial ways but if anyone could give me a simple indication of how to fix this I'm sure I could go ahead and complete my program.

Thanks in advance,
P
Nov 9 '07 #1
3 5939
weaknessforcats
9,208 Expert Mod 8TB
You have typedef'd a char array of MAXWORDSIZE as word.

In C and C++, the name of an array is the address of element 0. That makes word the address of the MAXWORDSIZE array. You can't overstore that address with one form malloc since that would change the address of a lopcal variable.

As a sidelight, I would not use fixed arrays of MAXWORDSIZE because a word like FOX would occupy 100 bytes. Instead, I would make the struct member a char* and I would allocate using malloc the exact length of the word plus 1 for a null terminator and then do the strcpy.
Nov 9 '07 #2
Thank you for your response! I'm not at liberty to change the definition of the structure as it's defined in a header file that I don't have access to.

I have, however, taken into account what you said as regards the type mismatch and have come up with a statement that should be allocating correctly but still chokes up a Segmentation Fault upon running.

Expand|Select|Wrap|Line Numbers
  1.  
  2. int len = strlen(d->theword)+1;
  3. *d->theword = (char *) malloc(len); 
  4. strcpy(d->theword, w);
  5.  
I've still no idea what I could have done wrong; the only notion is perhaps that I'd want to assign the length of w to the malloc and not the length of the d->theword. But my take on this is that the length of d->theword should be defined as word is; a length of MAXWORDSIZE?

On a slightly lighter note; I miss Java!
Nov 9 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
have, however, taken into account what you said as regards the type mismatch and have come up with a statement that should be allocating correctly but still chokes up a Segmentation Fault upon running.


Code: ( text )
int len = strlen(d->theword)+1;
*d->theword = (char *) malloc(len);
strcpy(d->theword, w);


I've still no idea what I could have done wrong;
You didn't read my post accurately. I said to allocate the length plus 1 more byte for the null terminator.
Expand|Select|Wrap|Line Numbers
  1. d->theword = (char*) malloc(len + 1);
  2.  
BTW, what's with the *d?? If d is a pointer to dictionary variable then you use d->theword. If d is a pointer to a dictionary and you use *d then you have a dictionary and have to switch to member-of syntax (*d).theword. The preferred syntax is d->thewword. It's wasier to read.

I am assuming at this point that you have the word declared as a char* and not as an array.
Nov 9 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: VB | last post by:
Hi, here File.cpp and File.h: File.cpp: ---------------------- #pragma warning (disable: 4786)
6
by: AMT2K5 | last post by:
Hello guys. I have a function, cleanSpace . I was told from another source that the problem is, is that the function is acting on constant string literal. To fix this I was told to take the...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
59
by: Christian Christmann | last post by:
Hi, I'm wondering why this program does not crash with a segmentation fault: #include <malloc.h> #include <string.h> #include <stdio.h> int main()
27
by: lovecreatesbea... | last post by:
This code snippet is an exercise on allocating two dimension array dynamically. Though this one is trivial, is it a correct one? Furthermore, when I tried to make these changes to the original...
8
by: Andrea | last post by:
I wrote this code: void * xmalloc (size_t size){ register void *value = OPENSSL_malloc(size); if (value == 0) printf("virtual memory exhausted"); return value; } int _chooseTSK(char*...
10
by: Cconfused | last post by:
Howdy! I'm mega confused with all this pointer malarky in C and it's resulting in an ever annoying segmentation fault which just won't go away! Essentially the program I am building has a Get()...
7
by: aarklon | last post by:
char *f() { char *s = malloc(8); strcpy(s,"good bye"); } int main(void) { printf("\n %s",*f()='A');
6
drumgirl67
by: drumgirl67 | last post by:
I am getting a segmentation fault in a function in a C++ program. "fields" is a two dimensional array that was passed to the function. Each "row" in fields is a 32 character array, and the total...
61
by: arnuld | last post by:
I have created a program which creates and renames files. I have described everything in comments. All I have is the cod-duplication. function like fopen, sprint and fwrite are being called again...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.