473,626 Members | 3,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1209
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
380
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
10673
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 work? Thanks yin99
7
1527
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 for a long time and not find an answer to my question. I am currently integrating the Moller-Trumbore ray/triangle intersection algorithm (http://www.ce.chalmers.se/old/staff/tomasm/raytri/raytri.c) into an existing raytracer. I have...
16
2289
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 subject pointer does not refer to an object suitably aligned in storage. It is guaranteed that a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and back again without change;...
42
5907
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 printed will it be all 0's or 0x12345678 int main(void) { char *ptr; ptr = 0;
17
3240
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: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
7
1396
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
3269
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 and arrays of pointers to them. I have gotten the program to compile with gcc on WinXP. If the file i read doesnt have alot of records, it runs thru. But once i add more, it dies. In this program i have 4 files setup to read. The
12
1922
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
8268
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8202
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7199
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1512
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.