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

  • Home
  • SEARCH
  • 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 6379109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:05:44+00:00 2026-05-25T02:05:44+00:00

I have been struggling with pointer and memory allocation in c for a while.

  • 0

I have been struggling with pointer and memory allocation in c for a while.

Here’s my implementation for the max subarray problem. It seems to work fine (maybe have bugs). But I have a question about the memory storage for tuple struct object. As you can see, tuple is declared in the global storage. Later in the findMaxSubArray() function, three pointers to Tuple struct are declared. My question is we didn’t declare Tuple struct object instances that the pointers (left, right, cross) are addressing how come the pointer dereferences (i.e., left->sum, etc) work. Does the GNU c compiler automatically allocate storage for them? (I don’t understand x86 assembly code) Can someone please explain what’s going on here? Much appreciated.

#include <iostream>
using namespace std;

#define NEGINFINITY -2 << 31

typedef struct {
  int lowPosition;
  int highPosition;
  int sum;
} Tuple;

Tuple tuple;

Tuple* findMaxCrossingSubArray(int a[], int low, int mid, int high) {
  int leftSum, rightSum;
  int leftMax, rightMax;
  int sum;

  leftSum = rightSum = NEGINFINITY; 
  sum = 0; 
  for (int i = mid; i >= low; --i) {
    sum += a[i]; 
    if (sum > leftSum) {
      leftSum = sum;
      leftMax = i;
    } 
  }

  sum = 0;
  for (int j = mid + 1; j <= high; ++j) {
    sum += a[j];
    if (sum > rightSum) {
      rightSum = sum;
      rightMax = j;
    }
  }

  tuple.lowPosition = leftMax;
  tuple.highPosition = rightMax;
  tuple.sum = leftSum + rightSum;
  return &tuple;
}

Tuple* findMaxSubArray(int* array, int low, int high) {
  Tuple *left, *right, *cross;
  if (high == low) {
    // base case
    tuple.lowPosition = low;
    tuple.highPosition = high;
    tuple.sum = array[low];
    return &tuple;
  } 
  else {
    int mid = (low + high) / 2; 
    left = findMaxSubArray(array, low, mid);
    right = findMaxSubArray(array, mid + 1, high);
    cross = findMaxCrossingSubArray(array, low, mid, high);

    if (left->sum > right->sum && left->sum > cross->sum)
      return left;
    else if (right->sum > left->sum && right->sum > cross->sum)
      return right;
    else
      return cross;
  }
}

int main() {
  Tuple *result;
  int data[] = {1, -2, 3, 10, -4, 7, 2, -5};
  result = findMaxSubArray(data, 0, 7);
  for (int i = 0; i < 8; ++i)
    cout << data[i] << " ";
  cout << endl;
  cout << "The sum of max subarray is " << result->sum 
       << " Starting at index " << result->lowPosition 
       << " ending at index " << result->highPosition << endl;

}
  • 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-05-25T02:05:44+00:00Added an answer on May 25, 2026 at 2:05 am

    The global variable tuple is the only actual Tuple in this program. Memory for global variables is managed by the compiler.

    In main, Tuple *result is just a pointer which, when you declared it, contains a random number (whatever happened to previously be in the space it now occupies) and thus result points to garbage (not a valid Tuple object).

    Then you assign the result of findMaxSubArray to tuple. Since findMaxSubArray returns &tuple (in one way or another) which is a global variable, result points to the global variable tuple. So when you do result->sum, it’s the same as doing tuple.sum.

    In findMaxSubArray, the line Tuple *left, *right, *cross; declares three pointers to Tuples which contain a garbage value. In one branch of the if you don’t use them and just return &tuple, the address of the global variable tuple. In the other branch, you set left, right, and cross to either findMaxCrossingSubArray or findMaxSubArray, which both return &tuple one way or the other.

    I do suggest reading a book on C++ and forgetting everything you know about C while using C++ (but remember it all again when you program C again). They are not the same language. This code is riddled with things you learned from your C training (such as #define and typedef struct ... Tuple) for which C++ offers better facilities.

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

Sidebar

Related Questions

I have been struggling a while with this problem and read a lot but
Have been struggling with Javascript closure for a while trying to wrap brain around
I have been struggling with this seeminly easy problem for 48 hours, and I
For a while I have been struggling to integrate scala with java methods that
This is a question I have been struggling with for a while. What is
I have been struggling quite a bit to get this JQgrid to work with
I have been struggling with a problem that only happens when the database has
I have been struggling with this for a while and could really use some
I have been struggling with what I perceive to be a simple problem: Working
Have been struggling all day trying to make this simple example work using socket.io.

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.