Why doesn't the debugger follow dynamic memory allocations well?

Here's an example of a code block that doesn't seem to work right with the CVI compiler/debugger, but works fine with other C compilers/debuggers
struct arg_int* arg_intn(const char* shortopts,
const char* longopts,
const char *datatype,
int mincount,
int maxcount,
const char *glossary)
size_t nbytes;
struct arg_int *result;
/* foolproof things by ensuring maxcount is not less than mincount */
maxcount = (maxcount<mincount) ? mincount : maxcount;
nbytes = sizeof(struct arg_int) /* storage for struct arg_int */
+ maxcount * sizeof(int); /* storage for ival[maxcount] array */
result = (struct arg_int*)malloc(nbytes);
if (result)
/* init the arg_hdr struct */
result->hdr.flag = ARG_HASVALUE;
result->hdr.shortopts = shortopts;
result->hdr.longopts = longopts;
result->hdr.datatype = datatype ? datatype : "<int>";
result->hdr.glossary = glossary;
result->hdr.mincount = mincount;
result->hdr.maxcount = maxcount;
result->hdr.parent = result;
result->hdr.resetfn = (arg_resetfn*)resetfn;
result->hdr.scanfn = (arg_scanfn*)scanfn;
result->hdr.checkfn = (arg_checkfn*)checkfn;
result->hdr.errorfn = (arg_errorfn*)errorfn;
/* store the ival[maxcount] array immediately after the arg_int struct */
result->ival = (int*)(result+1);
result->count = 0;
/*printf("arg_intn() returns %p\n",result);*/
return result;
When I try to dereference this structure's 'ival[0]' the debugger constantly complains of a fatal runtime error and declares it out of the array bounds.
This is from the argtable2 open source library available at http://argtable.sourceforge.net/ if you want to try and reproduce it.  I'm using CVI 2010 SP1.

Unfortunately, you have run into one of the inherent limitations of CVI's run-time checking. Even though it is perfectly legal in C and somewhat common practice, our run-time checking doesn't like it when you conceptually split up a block of memory and treat it as two or more separate blocks. 
While I cannot fix the problem in our run-time checking without breaking other use cases, I can explain what's causing the error and how you can work around it.
When you malloc memory, we assume that the memory block is going to hold one or more instances of the type to which you're casting the memory block. In this case, CVI is treating the new memory block as an array of arg_info structures. But your new memory block is not big enough to hold more than one instance of struct arg_info, so we implicitly truncate the memory block to sizeof(struct arg_info). Because of the implicit truncation, s->values is now pointing past the end of the memory block and any access will result in an error.
#include <ansi_c.h>
struct arg_int {
char some_large_block[64];
int count;
int *values;
int main (int argc, char *argv[])
int i, n = 4;
struct arg_int *s = malloc(sizeof *s + n * sizeof *s->values);
s->count = n;
s->values = (int *)(s+1);
for (i = 0; i < n; ++i)
s->values[i] = i;
return 0;
You can avoid the implicit truncation in the original code by assigning to a (void*) first. This retains the original block size by keeping the actual type of the data in the memory block in limbo. Subsequent casts do not truncate the block. We truncate only when the cast occurs implicitly as part of a malloc. s->values points to the remainder of the block and you can access it freely, but you do not get any run-time checking on it.
#include <ansi_c.h>
struct arg_int {
char some_large_block[64];
int count;
int *values;
int main (int argc, char *argv[])
int i, n = 4;
struct arg_int *s;
void *memory = malloc(sizeof *s + n * sizeof *s->values);
s = memory;
s->count = n;
s->values = (int *)(s+1);
for (i = 0; i < n; ++i)
s->values[i] = i;
return 0;
If you want full run-time checking on s->values, you have to allocate the two components separately.
#include <ansi_c.h>
struct arg_int {
char some_large_block[64];
int count;
int *values;
int main (int argc, char *argv[])
int i, n = 4;
struct arg_int *s = malloc(sizeof *s);
s->count = n;
s->values = malloc(n * sizeof *s->values);
for (i = 0; i < n; ++i)
s->values[i] = i;
return 0;
Another option is to use an incomplete array. An incomplete array is an array of unspecified or zero size at the end of a structure definition. CVI is implicitly growing the array to fill up the rest of the allocated memory block. You do not get run-time checking for the array.
#include <ansi_c.h>
struct arg_int {
char some_large_block[64];
int count;
int values[0];
int main (int argc, char *argv[])
int i, n = 4;
struct arg_int *s = malloc(sizeof *s + n * sizeof *s->values);
s->count = n;
for (i = 0; i < n; ++i)
s->values[i] = i;
return 0;
You can also disable run-time checking for your memory block by going through a series of casts: http://zone.ni.com/reference/en-XX/help/370051K-01/cvi/disablinguserprotectionforindividualpointer/
Best regards.

Similar Messages

  • Why doesn't the new flash player I downloaded work on my mac?

    Hi,
    I download the last new flash player on my mac under Lion and I can't open some website like adobe websites

    Hi Sunil,
    I try http://www.adobe.com/software/flash/about/ and the file attach is the result: a white page.
    I uninstall it yesterday and download the last version on adobe
    it's on firefox, I can use safari but I need to reload many time to have something
    Le 27 sept. 2011 à 13:09, Sunil_Bhaskaran a écrit :
    Re: Why doesn't the new flash player I downloaded work on my mac?
    created by Sunil_Bhaskaran in Flash Player - View the full discussion
    After downloading, you need to run the installer.
    Could you please visit this site: http://www.adobe.com/software/flash/about/
    If Flash Player is installed in your machine, it will give you the version number.
    In the following link, you have step-by-step instructions on installing Flash Player on Mac:
    http://kb2.adobe.com/cps/908/cpsid_90893.html
    Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page: http://forums.adobe.com/message/3940045#3940045
    To unsubscribe from this thread, please visit the message page at http://forums.adobe.com/message/3940045#3940045. In the Actions box on the right, click the Stop Email Notifications link.
    Start a new discussion in Flash Player by email or at Adobe Forums
    For more information about maintaining your forum email notifications please go to http://forums.adobe.com/message/2936746#2936746.

  • Why doesn't the iPad have a guest screen like the PC for extra security?

    I just want to start off first by saying I just purchased my iPad about six months ago and I'm really enjoying it,but if you're like me,I believe in protecting my privacy.
    I don't like to turn down any of friends or coworkers when I'm asked to use my iPad,but when you have such things as your personal pictures,movies,music,business documents,etc...you'd like to keep them from prying eyes. I thought that with the new iOS 7 update,that issue would've been fixed, but I was wrong. I'm hoping Apple would correct this problem with the next iOS update,it would make a lot of loyal Apple consumers &amp; anyone who's new to purchasing the iPad for the first time very happy to know that feature is available.
    Why doesn't the iPad offer a guest screen?

    Tell Apple at the link below.
    http://www.apple.com/feedback/ipad.html

  • Why doesn't the CC app work properly?

    I've been using Creative Cloud for some time now and am really loving it, however I must say it is a source of annoyance that the Creative Cloud app just doesn't work properly.
    Firstly, I used it initially to download all my apps, then after an update those apps no longer appeared in the "Apps" list. Now only those that have been installed since that update appear in this list. Why doesn't the "Apps" list display all of the Adobe CC apps that I have installed on my computer?
    Why does it keep reinstalling fonts? I'm always getting "Lush Script Regular" and other fonts being added.
    It regularly comes up telling me that Dreamweaver and Premiere Pro need to be updated, but always I get an error, then the update request disappears.
    I'm regularly getting 4 files that it can't sync - even though they are the same files as others in the folder. If nothing else, why can't I say "Great, got the message". Also, why is the error message just "due to server error" with no advice on how to resolve it? Why is there a server error on only these 4 files?
    It seems to me there are still some teething issues with the CC app, but what I don't understand is why they aren't being resolved much faster, given that this is the portal to the service.
    Of course, given Adobe I doubt I'll get satisfactory answers – a little jaded with their responses. Anyway, thought I'd at least post the questions.  

    Questapo I am sorry you have been facing difficulties with your Creative Cloud experience.  I don't believe it will be possible to address all of your concerns in one discussion.  I would not be surprised if at least three of the issues are related to the same root cause.
    First in order for the applications to be visible are you regularly needing to delete the OPM.db file?  You can find this listed as solution 2 in CC desktop lists applications as "Up to Date" when not installed - http://helpx.adobe.com/creative-cloud/kb/aam-lists-removed-apps-date.html.

  • Why Doesn't the XIRR function work?

    In Excel if you have 5 records from a1:b5 then XIRR looks like this:
    XIRR(A1:A5, B1:B5) you get a nice neat answer like .35
    Here is a version that works in Crystal that would require manual entry of any new quarters numbers and dates-- and if you plug this into Crystal it works:
    (XIRR([1000000,-100000,-100000,-100000,-100000,10277.49,-100000], [DateValue(1999,2,1),DateValue(1999,3,1),DateValue(1999,6,1), DateValue(1999,12,1),DateValue(2000,3,1),DateValue(2000,6,1),DateValue(2000,9,1)]))*.100
    You do get a nice answer of something like .035.  But this is all done manually and I need it to function automatically when the report is refreshed.
    So you have a range for the currency and a range for the date fields. I need to simulate this in crystal. Crystal has an XIRR function, and with the arrays, I should be able to fill in the range, but am having difficulty getting it to work.
    After building the arrays, I made another formula to combine the arrays in the XIRR formula.
    I am down to this error now after getting this formula this far  --
    "Numerical method did not converge; try another value for guess."
    Here's where I am with the formula, although they don't want to use the guess part of the XIRR function - they just want to use the XIRR(number/currency, date).  Maybe you could pass this on too?:
    /{@Reset} for the group header (NOT using a repeated group header):
    whileprintingrecords;
    numbervar array x := 0;
    datevar array y := date(0,0,0);
    numbervar i := 0;
    numbervar j := 0;
    //{@accum} for the detail section:
    whileprintingrecords;
    numbervar array x;
    datevar array y;
    numbervar i := i + 1;
    numbervar j := count({table.groupfield},{table.groupfield});
    if i <= j then (
    redim preserve x[j];
    redim preserve y[j];
    x<i> := tonumber({table.currency});
    y<i> := datevalue({table.datetime})
    //{@xirr calc} to be placed in the group footer:
    whileprintingrecords;
    numbervar array x;
    datevar array y;
    xirr(x,y)
    The array works correctly.  So why do I get that error and why doesn't the XIRR formula work like they say it should?  Has anyone used this successfully in Crystal--maybe you could shed some light?
    Thanks!

    Hi,
    I am receiving that same error when the last item in the array is 0, otherwise all works perfectly.
    When I run the same group of numbers and dates in Excel it returns without issue.
    -265500.00,-690000.00,-570000.00,16814.25,-855000.00,-619500.00,55293.46,30411.40,15183.76,  0.00
    01-25-2007,03-06-2007,05-02-2007,06-29-2007,08-01-2007,08-24-2007,09-17-2007,03-14-2008,05-28-2008,03-31-2010
    =XIRR(A2:J2,A1:J1,-0.1)
    Is there a known bug in Crystal's XIRR function when the last value is 0? 
    Or a hot-fix that will repair this?
    Thanks in advance,
    Gary
    PS. I am using Crystal XI Product Version: 11.0.0.2495

  • Why doesn't the location information in an iCal meeting show up on my iPhone, when it does show up in iCal?

    Why doesn't the location information show up on my iPhone 4S calendar when I accept an invitation to a meeting?
    The other information such as the date, time and invitees all show up on the iPhone, while in iCal all the information including the Location field is showing up.

    If it won't charge you probably need to try a different dock to usb cable.  More information on troubshooting this is available here:  http://support.apple.com/kb/TS1591.

  • Why doesn't the weather icon display the current temperature in your "local" city?

    Why doesn't the current temperature display in the weather icon on the home screen?

    The application works and I do have location-based services on. I'm just wondering why the icon itself does not show the current temperature. Sort of how they upgraded the clock after show the current time.

  • Why doesn't the "back" button work all the time in Safari?

    Why doesn't the "back" button work all the time in Safari?

    thanks for the reply. You prompted me to check and I realised it wasn't installed on my test laptop and opening in Microsoft Reader. I've installed it now and it works.
    My next challenge is to prevent the mailto: command opening Microsoft Mail instead of Outlook, but I guess every user will have a different default.
    Thank you

  • Why doesn't the repair box in Aperture show up? and if it does, once you choose repair does it give you an indication it is working?

    Why doesn't the repair box in Aperture show up? and if it does, once you choose repair does it give you an indication it is working?

    Winterwren22 wrote:
    Why doesn't the repair box in Aperture show up?
    Without more information, speculation is a waste of time.  At the least tell us what you do, what the results are, and how that result differs from your expectation.  Then tell us what steps you have taken to resolve the issue, and what the results of taking those steps was.
    once you choose repair does it give you an indication it is working?
    Adjustments you make are shown live.
    Instructions on the use of Repair Brush are here (ignore "Spot & Patch" -- it is superannuated).

  • I have Adobe Reader X on Windows Vista. Why doesn't the Printer for Adobe PDF show up in my Hardware/printers? Or can I add it to the Hardware/Printers?

    I have Adobe Reader X in Windows Vista. Why doesn't the Adobe PDF show up as a printer in my Hardware/Printers? Or can I add it to the Hardware/Printers? And how?

    Adobe Reader - FREE - reads PDF files, prints them, fills forms.
    Adobe Acrobat - $$ - as Reader, and makes PDFs and edits them, sorta, and does a bunch more.
    So if you had Acrobat try and find the license to avoid having to pay again...

  • Why doesn't the sd card or the usb connect to my mac ????

    why doesn't the sd card or the usb connect to my mac plzzzz help meeeeeeeeeee

    The reason no one has replied is that you posted in a little-viewed forum for an obsolete Apple software suite. I am asking the Hosts to move you to the MacBook Air forum.
    Also these forums are NOT chat rooms so you will seldom get an instant answer. No one here works for Apple; we are end users like you who volunteer on a time-available basis to help our fellow Mac users. Patience is required here.

  • Why doesn't the rotate gesture on my trackpad behave as it used to for switching between tabs when it works the same with all other programs besides Firefox?

    Why doesn't the rotate gesture on my trackpad behave as it used to for switching between tabs when it works the same with all other programs besides Firefox?
    I changed the about:config to make the rotate gesture move between tabs, and it worked great for well over a year. Now it behaves erratically as described here:
    https://bugzilla.mozilla.org/show_bug.cgi?id=877598
    It used to be very controllable to move one tab over, now it is erratic and moves quickly and to unexpected tabs regardless of how slow I do the rotate gesture. This is the main reason I use Firefox over Chrome and I would like to continue to do so unless there is no fix to this.

    Just set the browser.gesture.twist.threshold to something around 15-25.

  • HT1368 Why doesn't the Add to Wish List icon appear on my iPhone 5? I have iOS 7...frustrating

    Why doesn't the Add to Wish List icon appear on my iPhone 5? I have iOS 7...frustrating

    If it's not a free item then you should get the 'add to wish list' icon via the share button - is it a free or paid-for item that you are looking at ?

  • Why doesn't the role of IP in portal exit?

    Hi,
      Why doesn't the role named 'pcdportal_contentcom.sap.pctplatform_add_onscom.sap.ip.biRolescom.sap.ip.bi.bi_showcase' for IP exit in portal. But the t-code RSPLAN workds.
      Any suggetions are appreciated.
    B.R
    Gerald

    It's due to the JCO configration.

  • HT1853 Why doesn't the iTunes show your device?

    Why doesn't the new iTunes show your device? I want to sync new albums..

    when I try and put songs on it it will sit their for hours saying updating files on zachs iphone preparing to update and it never changes and never updates

Maybe you are looking for

  • Inspection lot using WM movement

    Dear All, I am using QM with WM. I want to create an inspection lot when I am move stock from one bin to another. Is it possible using a WM movement type and no IM movement type?

  • Does the docking port need to be recognized???

    I cannot charge my iPod shuffle 2nd generation, when I plug it into the docking port the shuffle gets recognized fine by iTunes and starts to flash amber for about 20 seconds. After this 20 seconds of flashing the light stops and doesn't come back on

  • Checking for errors in application item computation (at page level)

    I have a conditional computation used to set the value of an application-level item. In the condition, I need to check to see if the action (button-press to initiate DML processing) generated an error (eg cannot delete row because of child records).

  • Which way for 7x5 paper in D7160?

    I'm trying to print a landscape format photo onto 7x5 paper in my Photosmart D7160 using Photoshop Elements 6. I've tried putting the paper in the Main tray in both landscape and portrait positions, aliginging it to the right-hand side (as you look a

  • How to extend ringer time

    I'm pretty sure the iPhone, like all phones, is set at a default ring time of 20 seconds. Is there any way on my own that I can manually extend the ring time to the maximum 30 seconds or do I have to call Apple customer service. If so what is the num