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

Problems with pointers.

Hello

I am not quite sure if this is the right place to post this question, but I
hope this group is one of the rare ones that forgive less wise ones like
me. :)

I have quite a hard time trying to figure out what I am doing wrong.
Pointers are involved, and structures, and functions taking pointers as
parameters and returning pointers. It's one big pointer mess really.

I am coding and compiling under Win98 (MS-DOS) using DJGPP, GCC version
3.22 with the -g switch (though debugging won't work :(). The machine is an
AMD Athlon 1.2GHz, 512MB SDRAM. Hope I diden't miss any important specs.

Now to the problem at hand. I successfully manage to compile the following
program:

typedef struct {
char *name;
char *msg;
char *desc;
} testt;

testt *init_test(testt *test, char *name, char *msg, char *desc);

int main (int argc, char **argv){
testt *test;

test = init_test(test, "Test name", "Test msg", "Test desc");

free(test->name);
free(test->msg);
free(test->desc);
free(test);

return 0;
}

testt *init_test(testt *test, char *name, char *msg, char *desc){

test = (testt *)malloc(sizeof(testt *));

test->name = malloc(sizeof(char) * (strlen(name)));
test->msg = malloc(sizeof(char) * (strlen(msg)));
test->desc = malloc(sizeof(char) * (strlen(desc)));

strcpy(test->name, name);
strcpy(test->msg, msg);
strcpy(test->desc, desc);

return test;
}

Obviously, this is no real program, only the basic principle that I use in
the real project. When I run this program, it crashes with the following
ouput:

Exiting due to signal SIGSEGV
General Protection Fault at eip=000036d5
eax=ffffffd4 ebx=ffffffcc ecx=ffffffcc edx=00000000 esi=0008dda4
edi=ffffffd4
ebp=0008d938 esp=0008d92c
program=F:\CODING\PROJECTS\C\TW\CORE\TEST\PROBE.EX E
cs: sel=00a7 base=83ed2000 limit=0009ffff
ds: sel=00b7 base=83ed2000 limit=0009ffff
es: sel=00b7 base=83ed2000 limit=0009ffff
fs: sel=0087 base=00019190 limit=0000ffff
gs: sel=00c7 base=00000000 limit=0010ffff
ss: sel=00b7 base=83ed2000 limit=0009ffff
App stack: [0008d978..0000d978] Exceptn stack: [0000d8d8..0000b998]

Call frame traceback EIPs:
0x000036d5
0x00001727
0x00003068

I am guessing that it has something to do with illegal memory access or the
like. But that is just a guess.

I really hope someone have the kindness to shed some light on this problem.
Because I am clueless.

Many thanks in advance.

/Locke

Nov 13 '05 #1
8 3681
Magnus Malm <te********@hotmail.com> scribbled the following:
Hello I am not quite sure if this is the right place to post this question, but I
hope this group is one of the rare ones that forgive less wise ones like
me. :) I have quite a hard time trying to figure out what I am doing wrong.
Pointers are involved, and structures, and functions taking pointers as
parameters and returning pointers. It's one big pointer mess really. I am coding and compiling under Win98 (MS-DOS) using DJGPP, GCC version
3.22 with the -g switch (though debugging won't work :(). The machine is an
AMD Athlon 1.2GHz, 512MB SDRAM. Hope I diden't miss any important specs. Now to the problem at hand. I successfully manage to compile the following
program: typedef struct {
char *name;
char *msg;
char *desc;
} testt; testt *init_test(testt *test, char *name, char *msg, char *desc); int main (int argc, char **argv){
testt *test; test = init_test(test, "Test name", "Test msg", "Test desc"); free(test->name);
free(test->msg);
free(test->desc);
free(test); return 0;
} testt *init_test(testt *test, char *name, char *msg, char *desc){ test = (testt *)malloc(sizeof(testt *));
Lose the cast, it's useless. Here's a better version:
test = malloc(sizeof *test);
test->name = malloc(sizeof(char) * (strlen(name)));
test->msg = malloc(sizeof(char) * (strlen(msg)));
test->desc = malloc(sizeof(char) * (strlen(desc)));
Lose the casts. Also sizeof(char) is always 1, so multiplying by it is
superfluous.
strcpy(test->name, name);
strcpy(test->msg, msg);
strcpy(test->desc, desc);
The problem here is that strlen() does not count the null terminator,
but strcpy() tries to copy it anyway. You have to add it yourself to
the length:
test->name = malloc(strlen(name)+1);
test->msg = malloc(strlen(msg)+1);
test->desc = malloc(strlen(desc)+1);
return test;
} Obviously, this is no real program, only the basic principle that I use in
the real project. When I run this program, it crashes with the following
ouput:
(snip Windows-specific stuff)
I am guessing that it has something to do with illegal memory access or the
like. But that is just a guess.
It's an illegal memory access. strcpy() is poking its nose where it does
not belong. You did not allocate enough memory for it to poke its nose
to.
I really hope someone have the kindness to shed some light on this problem.
Because I am clueless.


--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"You have moved your mouse, for these changes to take effect you must shut down
and restart your computer. Do you want to restart your computer now?"
- Karri Kalpio
Nov 13 '05 #2

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote
Magnus Malm <te********@hotmail.com> scribbled the following:
testt *init_test(testt *test, char *name, char *msg, char *desc){

test = (testt *)malloc(sizeof(testt *));


Lose the cast, it's useless. Here's a better version:
test = malloc(sizeof *test);


While the other (snipped) corrections were necessary, I think this line
in the uncorrected version was the most likely cause of the seg fault, as
the original call to malloc has sizeof(testt *) rather than sizeof(testt)

Joona didn't just improve the clarity of the code, he removed a bug as
well.
Nov 13 '05 #3


Magnus Malm wrote:
Now to the problem at hand. I successfully manage to compile the following
program:

typedef struct {
char *name;
char *msg;
char *desc;
} testt;

testt *init_test(testt *test, char *name, char *msg, char *desc);

int main (int argc, char **argv){
testt *test;

test = init_test(test, "Test name", "Test msg", "Test desc");

free(test->name);
free(test->msg);
free(test->desc);
free(test);

return 0;
}

testt *init_test(testt *test, char *name, char *msg, char *desc){

test = (testt *)malloc(sizeof(testt *));

test->name = malloc(sizeof(char) * (strlen(name)));
test->msg = malloc(sizeof(char) * (strlen(msg)));
test->desc = malloc(sizeof(char) * (strlen(desc)));

strcpy(test->name, name);
strcpy(test->msg, msg);
strcpy(test->desc, desc);

return test;
}


You should redesign the function init_test. malloc of a struct
is probably not neccessary. Remember, all dynamic allocations
have the potential of failure. You should always test the return
value of the allocating function (malloc in this code). In addition,
you have not allocated enough space to hold the string literal
arguments.

A redesigned example:

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

typedef struct testt
{
char *name;
char *msg;
char *desc;
}testt;

testt *init_test(testt *test, const char *name,
const char *msg, const char *desc);
void freeALL(testt *test);

int main (void)
{
testt test = {NULL,NULL,NULL}; /* Neccessary initialization */

if(init_test(&test, "Test name", "Test msg", "Test desc") == NULL)
puts("Failure in allocations");
else printf("test.name = \"%s\"\ntest.msg = \"%s\"\n"
"test.desc = \"%s\"\n",test.name,test.msg,test.desc);
freeALL(&test);
return 0;
}

testt *init_test(testt *test, const char *name,
const char *msg, const char *desc)
{
if((test->name = malloc(strlen(name)+1)) == NULL)
{
freeALL(test);
return NULL;
}
if((test->msg = malloc(strlen(msg)+1)) == NULL)
{
freeALL(test);
return NULL;
}
if((test->desc = malloc(strlen(desc)+1)) == NULL)
{
freeALL(test);
return NULL;
}
strcpy(test->name, name);
strcpy(test->msg, msg);
strcpy(test->desc, desc);
return test;
}

void freeALL(testt *test)
{
free(test->name);
free(test->msg);
free(test->desc);
test->name = test->msg = test->desc = NULL;
return;
}

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

Nov 13 '05 #4
Joona I Palaste <pa*****@cc.helsinki.fi> wrote in message news:<bg**********@oravannahka.helsinki.fi>...
Magnus Malm <te********@hotmail.com> scribbled the following:
test = (testt *)malloc(sizeof(testt *));


Lose the cast, it's useless. Here's a better version:
test = malloc(sizeof *test);


The cast is harmless, but the size is wrong. sizeof(testt *) is the
size of a pointer. sizeof(testt) is the size of the struct.
I really hope someone have the kindness to shed some light on this problem.
Because I am clueless.


Yes. Get a debugger.

Sam
Nov 13 '05 #5
op*****@yahoo.com (Samuel Barber) wrote:
Joona I Palaste <pa*****@cc.helsinki.fi> wrote in message news:<bg**********@oravannahka.helsinki.fi>...
Magnus Malm <te********@hotmail.com> scribbled the following:
test = (testt *)malloc(sizeof(testt *));
Lose the cast, it's useless. Here's a better version:
test = malloc(sizeof *test);


The cast is harmless,


Actually, since the OP forgot to

#include <stdlib.h>

the cast isn't harmless at all. If it hadn't been there, he would've
been warned that he forgot to include a proper declaration of malloc().
but the size is wrong.


That, as well.

Richard
Nov 13 '05 #6
Magnus Malm <te********@hotmail.com> wrote in message news:<oq********************************@4ax.com>. ..
Hello

I am not quite sure if this is the right place to post this question, but I
hope this group is one of the rare ones that forgive less wise ones like
me. :)

I have quite a hard time trying to figure out what I am doing wrong.
Pointers are involved, and structures, and functions taking pointers as
parameters and returning pointers. It's one big pointer mess really.

I am coding and compiling under Win98 (MS-DOS) using DJGPP, GCC version
3.22 with the -g switch (though debugging won't work :(). The machine is an
AMD Athlon 1.2GHz, 512MB SDRAM. Hope I diden't miss any important specs.

Now to the problem at hand. I successfully manage to compile the following
program:

typedef struct {
char *name;
char *msg;
char *desc;
} testt;

testt *init_test(testt *test, char *name, char *msg, char *desc);

int main (int argc, char **argv){
testt *test;

test = init_test(test, "Test name", "Test msg", "Test desc");

free(test->name);
free(test->msg);
free(test->desc);
free(test);

return 0;
}

testt *init_test(testt *test, char *name, char *msg, char *desc){

test = (testt *)malloc(sizeof(testt *));
1. there is no need for the cast. in fact, it would help you
if you left it out.
2. you are allocating enough memory to store a *pointer* to
testt, not enough memory to store an object of testt.

test->name = malloc(sizeof(char) * (strlen(name)));
remember that strlen() returns the number of characters
in the string. e.g.
char *s = "12345";
printf ("%u\n", strlen(s)); /* prints out 5 */

you allocate enough memory to store all the characters
in the string, but not enough to store the terminator '\0'.

do the following:
test->name = malloc (strlen (name) + 1);
test->msg = malloc(sizeof(char) * (strlen(msg)));
test->desc = malloc(sizeof(char) * (strlen(desc)));

strcpy(test->name, name);
strcpy(test->msg, msg);
strcpy(test->desc, desc);

return test;
}


<snipped>

hth
goose,
Nov 13 '05 #7
Magnus Malm <te********@hotmail.com> wrote:
[...]
int main (int argc, char **argv){
testt *test;

test = init_test(test, "Test name", "Test msg", "Test desc");


In addition to the errors that others have pointed out, you're passing
the value of an uninitialised variable here ("test"). You could
initialise test:

testt *test = NULL;

or just remove that parameter entirely - it's not needed, you overwrite
its value immediately in the function anyway, so you might as well just
have a local variable.

- Kevin.

Nov 13 '05 #8
On 29 Jul 2003 19:31:59 -0700, op*****@yahoo.com (Samuel Barber) wrote:
Joona I Palaste <pa*****@cc.helsinki.fi> wrote in message news:<bg**********@oravannahka.helsinki.fi>...
Magnus Malm <te********@hotmail.com> scribbled the following:
> test = (testt *)malloc(sizeof(testt *));


Lose the cast, it's useless. Here's a better version:
test = malloc(sizeof *test);


The cast is harmless, but the size is wrong. sizeof(testt *) is the
size of a pointer. sizeof(testt) is the size of the struct.


This was the *big* bad error. The minor one was that I forgot to allocate
strlen(name) + 1. All corrected now and it works like a charm.
> I really hope someone have the kindness to shed some light on this problem.
> Because I am clueless.


Yes. Get a debugger.


As you might have read in my original post, the debugger doesn't work even
though I have the switch -g on (in DJGPP). Sure, I could run the debugger
used by DJGPP, outside the editor. Guess I don't like the idea of exiting
the editor, running the debugger, return back to the editor, etc. when
there should be a feature to use the debugger straight from the editor. Oh
well, that's a different story entirly.

Thanks for all the help from all. Cheers

/Locke

Nov 13 '05 #9

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

Similar topics

11
by: Bob Hairgrove | last post by:
The following class contains start and end points of a range of values. The range can have a direction which is implied from the relative order of start and end, or it can be without direction. IOW...
3
by: kohvirus | last post by:
Well after embaressing myself and posting in the wrong fourm, I found my way to the right one and I'm hoping to seek some help. This is the program without any modifications that I am making...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
5
by: JS | last post by:
I give a function a void pointer as an argument. But in the function I would like to treat this argument as an integer (only pointers to integers will be sent to the function) therefor I would like...
4
by: pcnate | last post by:
I've been having some problems with pointers and such.This is homework, so I don't want people writing codeand telling me to use it. I just want some direction on what isn't working. here is...
9
by: bwaichu | last post by:
I am starting this thread after having run into two separate programming problems, where the compiler offered no help. The compiler did not even warn. The programs compiled fine. And the...
9
by: olf | last post by:
Hello all, I have very recently trying to make a small program in c++ and I am having problems with pointers. I want to read a line from a file, send that line to a function that parses and...
10
by: Jess | last post by:
Hello, I have a program that stores dynamically created objects into a vector. #include<iostream> #include<vector> using namespace std;
84
by: jacob navia | last post by:
As many people know, I think that garbage collection is a good solution for many memory allocation problems. I am aware however, that nothing is "the silver bullet", not even the GC. A recent...
14
by: stevenruiz | last post by:
Hello All My question mainly is how to use/reference Double Pointers? I am currently trying to understand what the meaning of a 'vector of pointers' means also? What I am trying to do is take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.