473,405 Members | 2,300 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,405 software developers and data experts.

pointer suddenly destroys

Hello, All!

I faced with unknown problem for me, hopefully local guru can help me out.

#define MSGLEN 32
#define BUFFLEN 1024

typedef struct msg_s {
unsigned char msg_flag;
unsigned char msg_type;
unsigned char msg_rc;
char msg_id[MSGLEN];
char msg_passwd[MSGLEN];
char msg_fqdn[MSGLEN];
char msg_ipaddr[MSGLEN];
char msg_serialno[MSGLEN];
} msg_t;
....
msg_t *msg, *msg2;
char szQuery[BUFFLEN] = { 0 };

msg = malloc( sizeof(msg_t) );
msg2 = malloc( sizeof(msg_t) );
if ( !msg || !msg2 ) {
/* error message */
}

/* keep copy of buffer */
memcpy(msg2, msg, sizeof(msg_t));
....

Here function is called, which absolutely doesn't deal with 'msg',
nevertheless after this 'msg' pointer is crashed and I'm unable to access it
properly, resulting with 'segmentation fault'.
(for reference: in GDB "print *msg" or "print *msg2" gives "Cannot access
memory at address 0x0"). Seems like memory occupied by 'msg' and 'msg2' was
corrupted and flew away, but how? Might be there are some common ways,
methods or rules to check correctness of memory allocation and keeping? Are
there any typical errors to look for in code...

Big thanks for any help~

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Nov 25 '05 #1
3 1197
Roman Mashak wrote:
Hello, All!

I faced with unknown problem for me, hopefully local guru can help me out.

#define MSGLEN 32
#define BUFFLEN 1024

typedef struct msg_s {
unsigned char msg_flag;
unsigned char msg_type;
unsigned char msg_rc;
char msg_id[MSGLEN];
char msg_passwd[MSGLEN];
char msg_fqdn[MSGLEN];
char msg_ipaddr[MSGLEN];
char msg_serialno[MSGLEN];
} msg_t;
...
msg_t *msg, *msg2;
char szQuery[BUFFLEN] = { 0 };

msg = malloc( sizeof(msg_t) );
msg2 = malloc( sizeof(msg_t) );
if ( !msg || !msg2 ) {
/* error message */
}

/* keep copy of buffer */
Why? It doesn't contain anything valuable: memory
obtained from malloc() has indeterminate content.
memcpy(msg2, msg, sizeof(msg_t));
...

Here function is called, which absolutely doesn't deal with 'msg',
nevertheless after this 'msg' pointer is crashed and I'm unable to access it
properly, resulting with 'segmentation fault'.
(for reference: in GDB "print *msg" or "print *msg2" gives "Cannot access
memory at address 0x0"). Seems like memory occupied by 'msg' and 'msg2' was
corrupted and flew away, but how? Might be there are some common ways,
methods or rules to check correctness of memory allocation and keeping? Are
there any typical errors to look for in code...


You haven't provided enough code for a serious attempt
at debugging, so all I can do is guess. My guess is that
the mystery function tries to put more characters in szQuery
than will fit there, possibly with a call like

strncpy(szQuery, "Hello, world!", BUFSIZ);

(Note the change from BUFFLEN to BUFSIZ.)

Of course, I'm only guessing.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 25 '05 #2
Roman Mashak wrote:
Hello, All!

I faced with unknown problem for me, hopefully local guru can help me out.

#define MSGLEN 32
#define BUFFLEN 1024

typedef struct msg_s {
unsigned char msg_flag;
unsigned char msg_type;
unsigned char msg_rc;
char msg_id[MSGLEN];
char msg_passwd[MSGLEN];
char msg_fqdn[MSGLEN];
char msg_ipaddr[MSGLEN];
char msg_serialno[MSGLEN];
} msg_t;
...
msg_t *msg, *msg2;
char szQuery[BUFFLEN] = { 0 };

msg = malloc( sizeof(msg_t) );
msg2 = malloc( sizeof(msg_t) );
if ( !msg || !msg2 ) {
/* error message */
}

/* keep copy of buffer */
memcpy(msg2, msg, sizeof(msg_t));
...

Here function is called, which absolutely doesn't deal with 'msg',
nevertheless after this 'msg' pointer is crashed and I'm unable to access it
properly, resulting with 'segmentation fault'.
(for reference: in GDB "print *msg" or "print *msg2" gives "Cannot access
memory at address 0x0"). Seems like memory occupied by 'msg' and 'msg2' was
corrupted and flew away, but how? Might be there are some common ways,
methods or rules to check correctness of memory allocation and keeping? Are
there any typical errors to look for in code...


You don't provide enough information for us to help you.
<OT>
Use gdb's watch command; I have not worked with it for a while
but IIRC you have to abuse it to get what you want. Something
along the lines of getting the address of msg and watching
*((msg_t *)address) (otherwise, the watchpoint evaporates when
in the new function. With this, you just continue and wait until
the break at the watch point when the contents at &msg are
changed.
Try it or ask for details in gnu.gcc.help as my memory may be
wrong.
</OT>

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 25 '05 #3
Hello, Eric!
You wrote on Fri, 25 Nov 2005 10:38:14 -0500:

ES> You haven't provided enough code for a serious attempt
ES> at debugging, so all I can do is guess. My guess is that
ES> the mystery function tries to put more characters in szQuery
ES> than will fit there, possibly with a call like

ES> strncpy(szQuery, "Hello, world!", BUFSIZ);

ES> (Note the change from BUFFLEN to BUFSIZ.)

ES> Of course, I'm only guessing.
Thank you for reply, seems like I missed the correct version of MySQL header
files and library while linking code. Perhaps it caused the above-mentioned
behavior, when I made connection to MySQL. At least now pointer is not
corrupted.

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Nov 26 '05 #4

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

Similar topics

1
by: mk | last post by:
In C++ one can get an absolute address of data member, having a pointer-to-member and a pointer to class object: struct A { int m; }; void f(A *a) {
12
by: Yin99 | last post by:
Why am I still able to use this object even though I have deleted it? The Output I get is: Cat Constructs Cat De-structs //deleteing object Cat Eats. //My question- why does object still...
7
by: Marcus Jacobs | last post by:
Before I am flamed, I did search the FAQ for an answer to the question that I am about to post. Even with a search of the newsgroup archive, there are so many subjects about pointers I could search...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
42
by: junky_fellow | last post by:
Consider an implementation that doesn't use all bits 0 to represent a NULL pointer. Let the NULL pointer is represented by 0x12345678. On such an implementation, if the value of NULL pointer is...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
7
by: manuhack | last post by:
In Chapter 6 of Eckel's thinking in C++, there is an example: //: C06:Stack3.h // With constructors/destructors #ifndef STACK3_H #define STACK3_H class Stack { struct Link {
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
12
by: MQ.john | last post by:
//Working Example: #include <stdio.h> #include <time.h> int main () { time_t rawtime; /* define rawtime as time_t */ time ( &rawtime );
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
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,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.