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 8572805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:06:31+00:00 2026-06-11T19:06:31+00:00

Buffer* b_create(int init_capacity, char inc_factor,char o_mode){ Buffer* buffer=NULL; /*local pointer to the buffer structure

  • 0
Buffer* b_create(int init_capacity, char inc_factor,char o_mode){

Buffer* buffer=NULL;    /*local pointer to the buffer structure on heap*/ 

if (o_mode=='f' || inc_factor==0){
    o_mode=0;
    inc_factor=0;
}else if (o_mode=='f' && inc_factor!=0){
    o_mode=0;
    inc_factor=0;
}else if (o_mode=='a' && 1<=inc_factor<=255){
    o_mode=1;
    inc_factor=inc_factor;
}else if (o_mode=='m' && 1<=inc_factor<=100){
    o_mode=-1;
    inc_factor=inc_factor;
}
else{
    buffer=NULL;
    return buffer;
}

    //throw a more meaningful error??

    //cleanup :: how about i just call the ca_destroy() ??
    /*
    free(buffer->ca_head);
    buffer->ca_head=NULL;
    free(buffer);
    buffer=NULL;
*/

buffer = (Buffer*)malloc(sizeof(Buffer));
buffer->ca_head=(char*)malloc(sizeof(char)*init_capacity);
buffer->mode=o_mode;
buffer->inc_factor=inc_factor;

buffer->capacity=init_capacity;
buffer->addc_offset=0;
buffer->mark_offset=0;
buffer->r_flag=0;

return buffer;

}

I have the above method. I am trying to get it to work. First of all, I check and set a couple of variables. If none of the cases apply, then I am setting the buffer to NULL and attempting to return the value to the calling function.

else{
    buffer=NULL;
    return buffer;
}

However after that return, the function continues and goes ahead and creates the buffer structure anyways. I was hoping that the function would exit once it sees a return statement. Why does it continue executing ??

  • 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-11T19:06:32+00:00Added an answer on June 11, 2026 at 7:06 pm

    This condition

    1<=inc_factor<=255
    

    is always true. It is parsed as (1 <= inc_factor) <= 255, and 1 <= inc_factor evaluates to either 0 or 1. So if o_mode is one of 'a' or 'm', one of the earlier conditions holds, even if inc_factor is 0 or negative.

    Looking at the condition-chain, we find:

    if (o_mode=='f' || inc_factor==0){
        o_mode=0;
        inc_factor=0;
    }else if (o_mode=='f' && inc_factor!=0){ // This is never true, because if o_mode == 'f',
        o_mode=0;                            // the first one already was true
        inc_factor=0;
    }else if (o_mode=='a' && 1<=inc_factor<=255){ // This is just o_mode == 'a'
        o_mode=1;
        inc_factor=inc_factor;
    }else if (o_mode=='m' && 1<=inc_factor<=100){ // This is just o_mode == 'm'
        o_mode=-1;
        inc_factor=inc_factor;
    }
    else{ // This is only reached if o_mode is none of 'f', 'a', 'm' and inc_factor is != 0
        buffer=NULL;
        return buffer;
    }
    

    but if o_mode is none of 'f', 'a', 'm' and inc_factor != 0, then the final else is reached and NULL is returned. There is no implementation broken enough to ignore a return statement.

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

Sidebar

Related Questions

scala> import scala.collection.mutable.Buffer import scala.collection.mutable.Buffer scala> val b = Buffer(1, 2, 3) b: scala.collection.mutable.Buffer[Int]
void function(int a, int b, int c) { char buffer1[5]; char buffer2[10]; int *ret;
void function(int a, int b, int c) { char buffer1[5]; char buffer2[10]; } We
I have a large buffer: char *buf = malloc(1000000000); // 1GB If I forked
Is it possible to somehow adapt a c-style string/buffer ( char* or wchar_t* )
I have a really irritating problem. This is the buffer structure i am using
Protocol Buffer documentation warns that ... You should never add behaviour to the generated
I read the input buffer from a console application (CMD) like this: var pBuffer
I have buffer/payload of integers where every 4 bytes is a field which i
I create an audio buffer and want to edit this before I play this

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.