The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

View Poll Results: C program memory stack problem on SCO Unix
Is there any tool out there that can be used to debug my program that is easy to use? 0 0%
Option 2 1 100.00%
Voters: 1. You may not vote on this poll

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem with stack overflow karthikb23 HP-UX 0 03-26-2008 02:02 AM
what is stack winding and stack unwinding amitpansuria High Level Programming 2 08-07-2007 02:46 PM
memory stack aladdin UNIX for Dummies Questions & Answers 1 05-16-2007 06:17 AM
how to round up a memory address(memory alignment problem) nj302 High Level Programming 6 09-21-2005 05:57 PM
stack problem mastaCow UNIX for Dummies Questions & Answers 4 04-02-2002 12:59 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-08-2005
Registered User
 

Join Date: Aug 2005
Location: Pennsylvania between Philadelphia and Allentown
Posts: 1
Post memory stack problem

Hi, I am writing a C program under SCO Unix. I have a memory stack problem but do not know how to go about fixing it. I have tried running INSURE but that does not detect any problems.

Essentially the problem is that the memory address shifts on return from a routine. I pass a pointer to function "get_dsp_data" which passes a pointer to that pointer (AKA double pointer) to a database routine. In the db routine I fill in the double pointer structure after malloc'ing memory. The data is fine within the db routine. The data is fine in the "get_dsp_data" routine. However, when the data gets back to the first routine the memory location has shifted, see below.


*****************************************************
stbm.c 310 before: p_number_of_dsps is 0 and
&p_number_of_dsps is 2147481140
stbm.c 1052 In get_dsp_data, p_number_of_dsps is 16 and
&p_number_of_dsps is 2147481140

** memory shifts here but I don't know why or how **

stbm.c 312 after: p_number_of_dsps is 0 and
&p_number_of_dsps is 2147481034
*****************************************************


If I change things around so that the variable is a global then the program cores at the end of the last routine before exit. I am thinking that possibly memory is going past its bounds but I don't know how to verify or fix this.

One other bit of information, I ported my code over to LINUX and ran valgrind on it, but no problems were detected. But that could be because the memory did not shift when run on the LINUX system. Perhaps it is a difference in the way the compilers handle memory??

Please if anyone has any ideas for me on how to troubleshoot let me know.

Thanks, Jeanne
Reply With Quote
Forum Sponsor
  #2  
Old 08-08-2005
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
I think data adjacent to the pointer is overwriting the pointer in question - it's a so-called one-off error. One byte off - you are writing the LSB of a longword pointer.

The only way to fix this is to get into gdb, then examine the pointer after every line
of code is executed - just after you load the struct in get_dsp_data.
Reply With Quote
  #3  
Old 08-10-2005
mbb mbb is offline
Registered User
 

Join Date: Aug 2001
Location: UK
Posts: 103
Memory problems are always a bugger to find, usually because the point of failure is not the fault, the fault lies elsewhere in your code.

One thing you can try - which is rather crude - is to comment out lines of code, recompile and run. If you continue to comment out lines of code sooner or later you will comment out the faultly line of code and the problem goes away.

In my own experience once you have identified the line of code causing your problem, a fix is not far behind.

This method has its limitations, of course, but it may help.

MBB
__________________
Senior Analyst/Programmer
Reply With Quote
  #4  
Old 08-11-2005
Driver's Avatar
Registered User
 

Join Date: Jul 2001
Location: Bremen, Germany
Posts: 34
Here's what I use to catch accidental variable modifications from other functions:

Code:
#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

void *
debug_alloc_pages(size_t nbytes) {
        long    psize = sysconf(_SC_PAGESIZE);
        size_t  npages = nbytes / psize;
        int             fd;
        void    *ret;

        if (npages * psize < nbytes) {
                ++npages;
        }

#ifdef MAP_ANON
        ret = mmap(0, npages * psize, PROT_READ|PROT_WRITE,
                MAP_ANON, -1, 0);
        if (ret == MAP_FAILED) {
                perror("mmap");
                exit(EXIT_FAILURE);
        }
#else
        /*
         * Use MAP_ANONYMOUS on HP-UX and mmap() with an 
         * fd for /dev/zero everywhere else
         */
        puts("debug_alloc_pages() does not work on this system");
        exit(EXIT_FAILURE);
#endif
        return ret;
}
Now, instead of writing

char buf[128];

... write

char *buf = debug_alloc_pages(128);

When you're done initializing ``buf'', do

(void) mprotect(buf, sysconf(_SC_PAGE_SIZE), PROT_READ);

In every function that is allowed to modify ``buf'', execute an

(void) mprotect(buf, sysconf(_SC_PAGE_SIZE), PROT_READ|PROT_WRITE);

... when you enter it and

(void) mprotect(buf, sysconf(_SC_PAGE_SIZE, PROT_READ);

... when you return from it.

An invalid write access should now yield a bus error or segmentation fault which will provide you with a core dump from which you can obtain a stack trace showing you which function attempted to modify the data.

Hope this helps
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 01:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0