Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8320669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:44:07+00:00 2026-06-08T22:44:07+00:00

I have what looks to me an innocent cycle which iterates on elements of

  • 0

I have what looks to me an innocent cycle which iterates on elements of an array whose type is unknown at compile time; my array is named mesh->vertices and is a pointer to void. Depending on the truth value of mesh->textured I need to consider the array differently. Incidentally, the code in the if and the else in the code segment below is similar, but I do need to distinguish two cases.

void TransformMesh(struct Mesh *mesh, struct Matrix4 *t)
{
    for (int i = 0; i < mesh->nVertices; ++i)
    {
        if (mesh->textured)
        {
            struct TexturedVertex *ptr = ((struct TexturedVertex *)mesh->vertices) + i;

            ptr[i].position = MatrixPointMultiply3(t, &ptr->position);
            ptr[i].normal = MatrixPointMultiply3(t, &ptr->normal);
        }
        else
        {
            struct Vertex *ptr = ((struct Vertex *)mesh->vertices) + i;

            ptr[i].position = MatrixPointMultiply3(t, &ptr->position);
            ptr[i].normal = MatrixPointMultiply3(t, &ptr->normal);
        }
    }
}

I guess I created the project with the Automatic Reference Counting option, thinking that it would not have affected C code, but now I feel like I’m wrong (by the way, how can I check which option I chose?).

Well, it looks like this function is doing something wrong with another array, called mesh->triangles, probably freeing it. When I try to use the vector I get an EXC_BAD_ACCESS error:

glDrawElements(GL_TRIANGLES, mesh->nTriangles * 3, GL_UNSIGNED_INT, mesh->triangles);

It looks like iterating on the mesh->vertices elements, casting them and doing the pointer arithmetic, is corrupting the memory. I think my problem is ARC, so I tried to do what described here but with no luck.

EDIT:

The code above was wrong, as pointed out by Conrad Shultz; the following is correct:

            ptr->position = MatrixPointMultiply3(t, &ptr->position);
            ptr->normal = MatrixPointMultiply3(t, &ptr->normal);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:44:09+00:00Added an answer on June 8, 2026 at 10:44 pm

    I seriously doubt ARC has anything to do with this – ARC only manages Objective-C objects. (It doesn’t even know how to handle Core Foundation types, which leads to the requirement for using the __bridge... keywords.)

    I’m struggling to understand your code. Admittedly, I don’t do a great deal of straight C programming, but I don’t get what you’re trying to do by adding i to ptr, which is presumably the pointer arithmetic of which you speak.

    Are you trying to just access the ith struct TexturedVertex in mesh->vertices? If so, just use your ptr[i] construct as written.

    It looks to me like you are doing arithmetic such that ptr ends up pointing to the ith struct TexturedVertex, then by accessing ptr[i] you are reading i elements past the ith struct TexturedVertex. If nVertices refers to the count of vertices (as would seem logical, given the name and C array conventions), you are then reading past the end of vertices, a classic buffer overflow error, which would unsurprisingly lead to EXC_BAD_ACCESS and all sorts of other fun errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have XML that looks like this: <Parameter Name=parameter name Value=parameter value /> which
Let's say we have code looks like this module TM def aa puts aa
I have code that looks more or less like the code below but it
I have html that looks like this with several items <div class=item> <p class=price>$388.00</p>
I have code that looks like this: obj.foo(); // obj might, or might not
I have class that looks like this: class A { public: class variables_map vm
I have code that looks like this: template<class T> class list { public: class
I have something that looks like the following: var someList = MockRepository.GenerateStub<IList<ISomething>>(); someList.Add(MockRepository.GenerateStub<ISomething>()); The
I have something that looks like the following: [CoolnessFactor] interface IThing {} class Tester
I have code that looks like the following: //unrelated code snipped resolver.reset(new tcp::resolver(iosvc)); tcp::resolver::query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.