我是C的初学者,我正在尝试for循环,我遇到了一个无限循环,它不应该是一个无限循环,任何人都可以帮助我理解为什么它是一个无限循环
void main()
{
int i;
for(i=1,printf("Initialization");i++ <=5,printf("\nCondition");printf("%d",i))
printf("\nInside the loop :");
}
Run Code Online (Sandbox Code Playgroud)
虽然这不是一个无限循环
void main()
{
int i;
for(i=1,printf("Intialization");printf("\nCondition"),i++<= 5;printf("%d",i))
printf("\nInside the loop\n");
}
Run Code Online (Sandbox Code Playgroud) 我有以下问题:我正在使用我的项目自定义池分配器,并且每次我要分配任何具有placement new的对象时,析构函数也会隐式调用该对象.
这是源代码:
测试对象:
class Obj {
public:
Obj(const std::string& s) {
std::cout << "Constructor Called" << std::endl;
}
~Obj() {
std::cout << "Destructor Called" << std::endl;
}
};
Run Code Online (Sandbox Code Playgroud)
主要:
int main()
{
void *pmemory;
pmemory = malloc(ONEGIG_SIZE);
PoolAllocator* poolAllocator = new PoolAllocator(sizeof(Obj), __alignof(Obj), ONEGIG_SIZE, pmemory);
Obj *obj1 = allocator::allocateNew(*poolAllocator, Obj("Hello")); //<-- const and dest is called
//......
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这里是Allocator:AllocateNew函数的源代码:
template <class T> T* allocateNew(Allocator& allocator, const T& t)
{
return new (allocator.allocate(sizeof(T), __alignof(T))) T(t);
}
Run Code Online (Sandbox Code Playgroud)
泳池分配器:
PoolAllocator::PoolAllocator(size_t …Run Code Online (Sandbox Code Playgroud) 我必须使用I2C为STM32F4发现和pcf8574连接代码.
我不能使用任何库函数.我尝试了一些我没做过的事.我在init代码后写了.
我的初始化代码
RCC->APB1ENR|=RCC_APB1ENR_I2C1EN ; // enable APB1 peripheral clock for I2C1
RCC->AHB1ENR|=RCC_AHB1ENR_GPIOBEN; // enable clock for SCL and SDA pins
//SCL on PB6 and SDA on PB7
GPIOB->MODER|=GPIO_MODER_MODER6; // set pin to alternate function
GPIOB->MODER|=GPIO_MODER_MODER7; // set pin to alternate function
GPIOB->OSPEEDR |=GPIO_OSPEEDER_OSPEEDR6; //set GPIO speed
GPIOB->OSPEEDR |=GPIO_OSPEEDER_OSPEEDR7; //set GPIO speed
GPIOB-> OTYPER |= GPIO_OTYPER_OT_6; // set output to open drain --> the line has to be only pulled low, not driven high
GPIOB-> OTYPER |= GPIO_OTYPER_OT_7; // set …Run Code Online (Sandbox Code Playgroud) 我有2-D阵列
char arr[2][3]={"sam","ali"}
Run Code Online (Sandbox Code Playgroud)
和指向此数组的指针
char(*ptr)[3]=arr;
Run Code Online (Sandbox Code Playgroud)
如何使用此指针打印arr[2][2]在这种情况下i.我已经尝试* (*(ptr+1)+2)过与处理数组相同的方式,但没有工作,所以可以任何一个帮助并告诉我如何处理指向数组的指针,在这种情况下打印元素[2][2].
我试着比较switch语句和查找表的性能如下.
这是使用switch语句的代码
#include <stdio.h>
int main()
{
int n = 3;
for (long i = 0; i < 10000000; ++i) {
switch (n) {
case 0:
printf("Alpha");
break;
case 1:
printf("Beta");
break;
case 2:
printf("Gamma");
break;
case 3:
printf("Delta");
break;
default:
break;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以下是使用查找表的代码:
#include <stdio.h>
static char const * const Greek[4] = {
"Alpha",
"Beta",
"Gamma",
"Delta"
};
int main()
{
int n = 3;
for (long i = 0; i …Run Code Online (Sandbox Code Playgroud) 我试着找出,我的代码中的错误在哪里:
public void service(HttpServletRequest request, HttpServletResponse response){
try{
user1 = request.getParameter("nameLog");
pass1 = request.getParameter("passLog");
String userid = null;
String passdb = null;
String query = "select user, pass from login where `user` = ? and `pass`=?";
PreparedStatement pst = conn.prepareStatement(query);
ResultSet rs = pst.executeQuery();
pst.setString(1, user1);
pst.setString(2, pass1);
while(rs.next()){
userid = rs.getString("user1");
passdb = rs.getString("pass1");
pst.executeQuery();
}
if(userid.equals(user1)&& passdb.equals(pass1)){
response.sendRedirect("/WebContent/login/Main.html");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试登录到我的应用程序(db列名称是user和pass)但我得到一个错误:
java.sql.SQLException: No value specified for parameter 1
Run Code Online (Sandbox Code Playgroud)
在这一排
ResultSet …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 sublime text Javascript Beautify plugin,它说我需要更改为以下设置:
{
"indent_size": 4,
"indent_char": " ",
"indent_level": 0,
"indent_with_tabs": false,
"preserve_newlines": true,
"max_preserve_newlines": 10,
"jslint_happy": false,
"brace_style": "collapse",
"keep_array_indentation": false,
"keep_function_indentation": false,
"space_before_conditional": true,
"break_chained_methods": false,
"eval_code": false,
"unescape_strings": false,
"wrap_line_length": 0,
// jsbeautify options
"format_on_save": true
}
Run Code Online (Sandbox Code Playgroud)
在 sublime 文本中,我究竟需要在哪里进行这种更改?
我有一个简单的项目,使用 CubeMX 创建用于外围设备初始化。
SPI 处于从机模式,并且似乎已正确初始化,但是当我对 8 位数据进行计时时,不会调用中断。
这是代码
/* SPI1 init function */
static void MX_SPI1_Init(void)
{
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_SLAVE;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
}
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(hspi->Instance==SPI1)
{
/* USER CODE BEGIN SPI1_MspInit 0 */
/* …Run Code Online (Sandbox Code Playgroud) 我有一个类,它有一些存储在向量中的数据和一个应该选择随机元素并返回它的方法,但是当我运行它时,它每次都返回相同的元素.
这是一个基于我的代码的简化示例:
#include <iostream>
#include <random>
#include <vector>
class MyObj{
private:
std::vector<int> set_data;
public:
MyObj(int num_elements){
for (int i = 0; i < num_elements; ++i){
set_data.push_back(i); // just so that there is some data in there
}
};
int getRandomElement(std::mt19937 rng){
std::uniform_int_distribution<int> uni(0,set_data.size()-1);
int idx = uni(rng);
return set_data[idx];
};
};
int main()
{
std::random_device r;
std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
std::mt19937 rng = std::mt19937(seed);
MyObj temp(50);
for (int i = 0; i < 20; i++){ …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何在Nucleo板上切换LED,我只是看不到用户LED切换.在网上看来,这就是你要做的一切.还有其他人遇到过这个问题吗?
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
int main(void)
{
int counter = 0;
SystemInit();
GPIO_InitTypeDef temp;
temp.GPIO_Mode = GPIO_Mode_OUT;
temp.GPIO_OType = GPIO_OType_PP; // Push Pull
temp.GPIO_Pin = GPIO_Pin_5;
temp.GPIO_Speed = GPIO_Low_Speed;
temp.GPIO_PuPd = GPIO_PuPd_NOPULL;
RCC_APB2PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_Init( GPIOA, &temp );
while( 1 )
{
if ( counter++ > 10000 )
{
GPIO_ToggleBits( GPIOA, GPIO_Pin_5 );
counter = 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)